【发布时间】:2022-01-08 23:57:03
【问题描述】:
Java 11,这里是 Spring Security。我的@RestController 中有以下端点/方法:
@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() 返回一个Authentication 看起来完全没问题的实例。当token.getCredentials() 在底部被调用时,我得到一个 NPE。
我知道这是一个不完整的代码 sn-p,我很乐意提供安全代码的其他部分(WebSecurityConfig 等)但是我想知道:什么会导致 SecurityContextHolder.getContext().getAuthentication() 不null,但没有要注入的token?
【问题讨论】:
-
(那个)匿名用户!? (例如,当端点是:
authenticated()...permitAll())
标签: java spring-security