Spring MVC - Checking if User is already logged in via Spring Security?

后端 未结 4 794
小蘑菇
小蘑菇 2020-12-04 06:59

I have a Spring MVC application.It uses its own custom Login page. Upon successful login, a \'LOGGED_IN_USER\' object is placed in the HTTPSession.

4条回答
  •  长情又很酷
    2020-12-04 07:31

    Many of the authentication providers will create a UserDetails object as the principal.

    Another way I found - using spring-security - is to check whether the return value of Authentication.getPrincipal() is an instance of UserDetails; the method returns "anonymousUser"(String) by default.

    boolean isUserLoggedIn(){
       return SecurityContextHolder.getContext().getAuthentication().getPrincipal() instanceof UserDetails
    }
    

提交回复
热议问题