How to redirect to the homepage if the user accesses the login page after being logged in?

后端 未结 6 1363
一生所求
一生所求 2020-11-28 13:16

Here is my spring security config:




        
6条回答
  •  青春惊慌失措
    2020-11-28 14:12

    You could also restrict your login page to ROLE_ANONYMOUS and set an :

    
    
    

    And in your handler check if the user is already authenticated:

    @Service
    public class AccessDeniedHandler extends AccessDeniedHandlerImpl {
        private final String HOME_PAGE = "/index.html";
    
        @Override
        public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
            Authentication auth = SecurityContextHolder.getContext().getAuthentication();
            if (auth != null && !(auth instanceof AnonymousAuthenticationToken)) {
                response.sendRedirect(HOME_PAGE);
            }
    
            super.handle(request, response, e);
        }
    }
    

提交回复
热议问题