Spring 安全将 null @AuthenticatedPrincipal 注入控制器
Spring Security injecting null @AuthenticatedPrincipal into controllers
Java11、Spring这里是保安。我的 @RestController
中有以下 endpoint/method:
@GetMapping("/centerPoint")
public void centerPoint(@AuthenticationPrincipal ExpiringUsernameAuthenticationToken token,
HttpServletResponse response) throws IOException {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth == null) {
LOGGER.warn("Current authentication instance from security context is null");
response.sendRedirect("some redirect url");
return;
}
SAMLCredential attributes = ((SAMLCredential) token.getCredentials());
// ...rest of code omitted for brevity
}
当我在此方法中设置断点并登录到我的应用程序时,token
是 null
(这意味着它没有作为 @AuthenticatedPrincipal
正确注入)然而 SecurityContextHolder.getContext().getAuthentication()
returns 一个看起来完全没问题的Authentication
实例。当 token.getCredentials()
在底部被调用时,我得到一个 NPE。
我知道这是一个不完整的代码片段,我很乐意提供安全代码的其他部分(WebSecurityConfig
,等等)但是我想知道:什么会导致 SecurityContextHolder.getContext().getAuthentication()
非空,但没有 token
注入?
AuthenticationPrincipalArgumentResolver
中的 javadoc 说明如下:
Will resolve the CustomUser argument using Authentication.getPrincipal() from the
SecurityContextHolder. If the Authentication or
Authentication.getPrincipal() is null, it will return null. If the
types do not match, null will be returned unless
AuthenticationPrincipal.errorOnInvalidType() is true in which case a
ClassCastException will be thrown.
所以你应该确保 Authentication.getPrincipal()
类型是 ExpiringUsernameAuthenticationToken
.
调试您的应用程序并查看 returns 来自 SecurityContextHolder.getContext().getAuthentication()
和 authentication.getPrincipal()
的类型
Java11、Spring这里是保安。我的 @RestController
中有以下 endpoint/method:
@GetMapping("/centerPoint")
public void centerPoint(@AuthenticationPrincipal ExpiringUsernameAuthenticationToken token,
HttpServletResponse response) throws IOException {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth == null) {
LOGGER.warn("Current authentication instance from security context is null");
response.sendRedirect("some redirect url");
return;
}
SAMLCredential attributes = ((SAMLCredential) token.getCredentials());
// ...rest of code omitted for brevity
}
当我在此方法中设置断点并登录到我的应用程序时,token
是 null
(这意味着它没有作为 @AuthenticatedPrincipal
正确注入)然而 SecurityContextHolder.getContext().getAuthentication()
returns 一个看起来完全没问题的Authentication
实例。当 token.getCredentials()
在底部被调用时,我得到一个 NPE。
我知道这是一个不完整的代码片段,我很乐意提供安全代码的其他部分(WebSecurityConfig
,等等)但是我想知道:什么会导致 SecurityContextHolder.getContext().getAuthentication()
非空,但没有 token
注入?
AuthenticationPrincipalArgumentResolver
中的 javadoc 说明如下:
Will resolve the CustomUser argument using Authentication.getPrincipal() from the SecurityContextHolder. If the Authentication or Authentication.getPrincipal() is null, it will return null. If the types do not match, null will be returned unless AuthenticationPrincipal.errorOnInvalidType() is true in which case a ClassCastException will be thrown.
所以你应该确保 Authentication.getPrincipal()
类型是 ExpiringUsernameAuthenticationToken
.
调试您的应用程序并查看 returns 来自 SecurityContextHolder.getContext().getAuthentication()
和 authentication.getPrincipal()