java Filter를 사용하여 자동 로그인하는 방법
package com.learning.web.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.learning.domain.User;
import com.learning.service.UserService;
@WebServlet("/servlet/loginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
String autologin = request.getParameter("autologin");
UserService userService=new UserService();
User user = userService.findUser(username, password);
//user null,
if (user!=null) {
// cookie
Cookie cookie=new Cookie("user", user.getUsername()+"&"+user.getPassword());
cookie.setPath("/");
//autologin null,
if (autologin!=null) {
cookie.setMaxAge(1*60*60*24);//
}
else {
cookie.setMaxAge(0);
}
response.addCookie(cookie);
request.getSession().setAttribute("user", user);
request.getRequestDispatcher("/home.jsp").forward(request, response);
}else {
response.sendRedirect(request.getContextPath()+"/homeLogin.jsp");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package com.learning.web.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.jstl.core.Config;
import com.learning.domain.User;
import com.learning.service.UserService;
@WebFilter(urlPatterns="/*",initParams={@WebInitParam(name="autologin",value="login"),@WebInitParam(name="",value="")})
public class AutoFilter implements Filter{
private FilterConfig filterConfig;
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
//
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
//
String uri = httpServletRequest.getRequestURI();
String contextPath = httpServletRequest.getContextPath();
uri = uri.substring(contextPath.length() + 1);
//
String login = filterConfig.getInitParameter("autologin");
System.out.println(" :"+login);
// "login" (xxxlogin.jsp )
if (!uri.contains(login)) {
HttpSession session = httpServletRequest.getSession();
User u = (User) session.getAttribute("user");
if (u != null) {
System.out.println("session null");
chain.doFilter(request, response);
} else {
//
// 1. cookie User
String username = "";
String password = "";
UserService userService = new UserService();
Cookie[] cookies = httpServletRequest.getCookies();
for (int i = 0;cookies!=null&& i < cookies.length; i++) {
if ("user".equals(cookies[i].getName())) {
String string = cookies[i].getValue();
String[] values = string.split("&");
username = values[0];
password = values[1];
User user = userService.findUser(username, password);
// session
if (user != null) {
System.out.println(" ");
httpServletRequest.getSession().setAttribute("user", user);
}
}
}
}
}
// 2.
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig=filterConfig;
}
}
이상은 본문의 전체 내용입니다. 여러분의 학습에 도움이 되고 저희를 많이 응원해 주십시오.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
38. Java의 Leetcode 솔루션텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.