Several implementation methods of login interception

Several implementation methods of login interception

1. The filter realizes the interception of login

@Component
@WebFilter(filterName = "TestFilter",urlPatterns = "/*")
@Order(1) //Order(1) Filter 
public class LoginFiilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest servletRequest1=(HttpServletRequest) request;
       //URL
        if (servletRequest1.getRequestURI().equals("/user/login")){
            filterChain.doFilter(request,response);
        }
        Object user = servletRequest1.getSession().getAttribute("user");
        if (user==null){
            servletRequest1.getRequestDispatcher("/login.jsp").forward(request,response);
        }else {
           //
            filterChain.doFilter(request,response);
        }
    }
    @Override
    public void destroy() {
    }
}
 

2. Realize the interception function based on Spring Aop

(1) Custom annotations (2) Customize the aspect @Aspect and configure it (3) Add custom annotations on the method that needs to be logged in verification