本题中的获取session,判断session中是否有用户数据,请问如何获取
我已经在登录页面设置了sessionStorage变量,但是如何在Java拦截器中获取呢?
老师顺便看看我这样做的对不对?
拦截器代码
public class LoginInterceptor implements HandlerInterceptor { Logger logger = LoggerFactory.getLogger(LoginInterceptor.class); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("preHandle拦截已开启"); return true; } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("after拦截以开启"); StringBuffer info = new StringBuffer(); info.append(request.getRemoteAddr()); info.append("|"); info.append(request.getRequestURL()); info.append("|"); info.append(request.getHeader("user-agent")); logger.info(info.toString()); } }
登录后的页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> 当前用户:${user.username} <a href="login.jsp">退出</a> </body> <script> let username = "${user.username}"; let password = "${user.password}"; sessionStorage.setItem('username', username); sessionStorage.setItem('password', password); </script> </html>
登录页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <div id="show">${info}</div> <form action="/login" method="post"> 用户名:<input type="text" name="username"><br> 密 码:<input type="password" name="password"><br> <input type="submit" value="登录"> </form> </body> </html>
Controller
@Controller public class LoginController { @PostMapping("/login") public ModelAndView view(User user) { ModelAndView modelAndView = new ModelAndView(); if (user.getUsername().equals("admin") && user.getPassword().equals("admin")) { modelAndView.setViewName("/main.jsp"); modelAndView.addObject("user", user); } else { modelAndView.setViewName("/login.jsp"); modelAndView.addObject("info", "用户名或密码错误,请重新登录!"); } return modelAndView; } }
35
收起
正在回答
1回答
同学你好,1、一般情况下,并不会在前端页面设置session。建议同学在LoginController中设置session,然后在main.jsp页面中进行获取。
2、在拦截器中可以获取session信息的,同学可以先通过session判断用户是否登录,如果已登录,则将用户信息写入到日志中。参考代码如下所示:
import com.imooc.restful.entity.User; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.servlet.HandlerInterceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class LoginInterceptor implements HandlerInterceptor { //1.创建日志对象,并加载当前类 Logger logger= LoggerFactory.getLogger(LoginInterceptor.class); //拦截器, 对登录的用户拦截判断,已登录的放行,未登录的响应回去先登录 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println(request.getRequestURI()); User user =(User) request.getSession().getAttribute("user"); if(user!=null){ //登录成功,将session中的账号信息存放到log日志中 logger.info(user.getName()+":"+user.getPassword()); System.out.println("成功"); return true; }else { response.sendRedirect("/login.jsp"); return false; } } }
祝学习愉快!
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星