An Authentication object was not found in the SecurityContext - Spring 3.2.2

后端 未结 6 1023
灰色年华
灰色年华 2020-12-03 04:26

I\'m trying to invoke a protected method from a class that implements the ApplicationListener interface on successful login (S

相关标签:
6条回答
  • 2020-12-03 04:55

    As pointed already by @Arun P Johny the root cause of the problem is that at the moment when AuthenticationSuccessEvent is processed SecurityContextHolder is not populated by Authentication object. So any declarative authorization checks (that must get user rights from SecurityContextHolder) will not work. I give you another idea how to solve this problem. There are two ways how you can run your custom code immidiately after successful authentication:

    1. Listen to AuthenticationSuccessEvent
    2. Provide your custom AuthenticationSuccessHandler implementation.

    AuthenticationSuccessHandler has one important advantage over first way: SecurityContextHolder will be already populated. So just move your stateService.rowCount() call into loginsuccesshandler.LoginSuccessHandler#onAuthenticationSuccess(...) method and the problem will go away.

    0 讨论(0)
  • 2020-12-03 04:58

    The security's authorization check part gets the authenticated object from SecurityContext, which will be set when a request gets through the spring security filter. My assumption here is that soon after the login this is not being set. You probably can use a hack as given below to set the value.

    try {
        SecurityContext ctx = SecurityContextHolder.createEmptyContext();
        SecurityContextHolder.setContext(ctx);
        ctx.setAuthentication(event.getAuthentication());
    
        //Do what ever you want to do
    
    } finally {
        SecurityContextHolder.clearContext();
    }
    

    Update:

    Also you can have a look at the InteractiveAuthenticationSuccessEvent which will be called once the SecurityContext is set.

    0 讨论(0)
  • 2020-12-03 05:01

    For me, the problem was a ContextRefreshedEvent handler. I was doing some data initilization but at that point in the application the Authentication had not been set. It was a catch 22 since the system needed an authentication to authorize and it needed authorization to get the authentication details :). I ended up loosening the authorization from a class level to a method level.

    0 讨论(0)
  • 2020-12-03 05:04

    There is similar issue. I added listener as given here

    https://stackoverflow.com/questions/3145936/spring-security-j-spring-security-logout-problem

    It worked for me adding below lines to web.xml. Posting it very late, should help someone looking for answer.

    <listener>
        <listener-class> org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
    </listener>
    
    0 讨论(0)
  • 2020-12-03 05:04

    I encountered the same error while using SpringBoot 2.1.4, along with Spring Security 5 (I believe). After one day of trying everything that Google had to offer, I discovered the cause of error in my case. I had a setup of micro-services, with the Auth server being different from the Resource Server. I had the following lines in my application.yml which prevented 'auto-configuration' despite of having included dependencies spring-boot-starter-security, spring-security-oauth2 and spring-security-jwt. I had included the following in the properties (during development) which caused the error.

    spring:
      autoconfigure:
        exclude: org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
    

    Commenting it out solved it for me.

    #spring:
    #  autoconfigure:
    #    exclude: org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
    

    Hope, it helps someone.

    0 讨论(0)
  • 2020-12-03 05:12

    This could also happens if you put a @PreAuthorize or @PostAuthorize in a Bean in creation. I would recommend to move such annotations to methods of interest.

    0 讨论(0)
提交回复
热议问题