java(16)--session 을 이용 하여 중복 로그 인 방지

13400 단어 자바
html 로그 인 페이지 는 자바 script 을 사용 하여 중복 작업 을 방지 할 수 있 지만 사용자 브 라 우 저 에서 js 사용 을 금지 할 경우 배경 에서 방지 할 방법 을 강구 해 야 합 니 다.
<!DOCTYPE html>
<html>
  <head>
    <title>form.html</title>

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">

    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
    <script type="text/javascript"> var iscommitted=false; function dosubmit(){ if(!iscommitted){ iscommitted = true; return true; }else{ return false; } } </script>
  </head>

  <body>
   <form action="/aDay07/servlet/FormServlet" method="post" onsubmit="return dosubmit()"><input type="text" name="username">
        <input type="submit" value="  ">
    </form>
  </body>
</html>

백 스테이지 FormServlet
import java.io.IOException;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sun.misc.BASE64Encoder;
//  form servlet
public class FormServlet extends HttpServlet {


    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String token = TokenProcessor.getInstance().generateToken();
        request.getSession().setAttribute("token", token);

        request.getRequestDispatcher("/form.jsp").forward(request, response);

        /* out.print("<form action='/aDay07/servlet/DoFormServlet' method='post'>"); out.print("<input type='hidden' name='token' value='"+token+"'>"); out.print("<input type='text' name='username'>"); out.print("<input type='submit' value='  '>"); out.print("</form>");*/

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}
//      
class TokenProcessor{

    private TokenProcessor(){};
    public static TokenProcessor instance = new TokenProcessor();
    public static TokenProcessor getInstance(){
        return instance;
    }

    public String generateToken(){

        //return UUID.randomUUID().toString();//    :  uuid       

        // 3847834873 822382382382738238 828323
        String token = System.currentTimeMillis() + new Random().nextInt(1000000) + "";

        try {
            MessageDigest md = MessageDigest.getInstance("md5");
            byte md5[] = md.digest(token.getBytes());  //[99239,23939,]
            //base64
            BASE64Encoder encoder = new BASE64Encoder();
            return encoder.encode(md5);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }
}

form.jsp 페이지 는 token 을 클 라 이언 트 에 전송 하여 나중에 검사 할 수 있 도록 합 니 다.
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'form.jsp' starting page</title>
  </head>  
  <body>   
    <form action="/aDay07/servlet/DoFormServlet" method="post">
        <input type="hidden" name="token" value="<%=session.getAttribute("token") %>">
           :<input type="text" name="username">
        <input type="submit" value="  ">
    </form>   
  </body>
</html>

DoFormServlet 페이지
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DoFormServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        boolean b = isTokenValid(request);
        if (!b) {
            System.out.println("      !!");
            return;
        }

        request.getSession().removeAttribute("token");
        System.out.println("    !!!");
    }

    //            
    private boolean isTokenValid(HttpServletRequest request) {

        String c_token = request.getParameter("token");
        if (c_token == null) {
            return false;
        }
        String s_token = (String) request.getSession().getAttribute("token");
        if (s_token == null) {
            return false;
        }

        if (!c_token.equals(s_token)) {
            return false;
        }
        return true;
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}

좋은 웹페이지 즐겨찾기