Spring security Redirection issue for already logged-in users

后端 未结 2 1421
没有蜡笔的小新
没有蜡笔的小新 2021-01-16 12:21

While implementing spring security with my GWT based web application. I found that. Everything is working fine as expected, except the below fact:

I opened login.jsp

2条回答
  •  耶瑟儿~
    2021-01-16 12:59

    Alternatively you can create a Servlet Filter:

    public class LoginPageFilter implements Filter
    {
       public void init(FilterConfig filterConfig) throws ServletException   {
       }
    
       public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,   FilterChain filterChain) throws IOException, ServletException
       {
           HttpServletRequest request = (HttpServletRequest) servletRequest;
           HttpServletResponse response = (HttpServletResponse) servletResponse;
    
           if(request.getUserPrincipal() != null){ //If user is already authenticated
               response.sendRedirect("");// or, forward using RequestDispatcher
           } else{
               filterChain.doFilter(servletRequest, servletResponse);
           }
       }
    
       public void destroy() {
       }
    }
    

    web.xml:

    LoginPageFilter com.xxx.xx.LoginPageFilter

    
        LoginPageFilter
        /login
    
    

提交回复
热议问题