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

后端 未结 6 1038
灰色年华
灰色年华 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.

提交回复
热议问题