java(16)--session 을 이용 하여 중복 로그 인 방지
13400 단어 자바
<!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);
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.