Springboot-登录功能-拦截器-保持登录状态
在原先的简单方法后,完善了功能
前文登录功能:https://blog.csdn.net/weixin_45910779/article/details/113576610
本人博客园:https://www.cnblogs.com/djhzzl/p/14135784.html
在原先代码上添加拦截器和session,保持登录状态
拦截器实现:
使用拦截器,保证部分功能需要先登录,才能访问
新建拦截器
·自定义拦截器,需要继承HandlerInterceptorAdapter并重写preHandle方法
·同时需要设置不拦截页面静态资源,否则会加载不到图片、css等静态资源
·还需要获取用户登录信息,如果获取不到,则跳转到登录界面。
·代码如下:
package com.hut.maoyanmovie.interceptor;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* @author HP
* @data 2020-12-14
*
*新建拦截器
*
* 自定义拦截器
*/
public class AuthInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 拦截处理代码
//静态资源不被拦截器拦截
String uri = request.getRequestURI();
if (uri.endsWith("js")||uri.endsWith("css")||uri.endsWith("jpg")||uri.endsWith("svg