【发布时间】:2022-08-03 09:12:34
【问题描述】:
我需要使用 Keycloak,并且更喜欢在我的 Spring Boot 应用程序中使用无状态 JWT 令牌。我可以让它在会话中正常运行,但是在转换它时我需要帮助
- 强制 Spring Boot Security 检查登录
- 允许 /logout URL(转到 Keycloak)
我的代码“运行”,但是当我点击初始页面时,我看到的日志消息似乎表明它没有检测到任何登录迹象。发生这种情况时,我想强制使用 Spring启动以重定向到登录页面,就像这是一个有状态的应用程序一样。
org.springframework.web.servlet.FrameworkServlet: Failed to complete request: java.lang.NullPointerException: Cannot invoke \"org.springframework.security.authentication.AbstractAuthenticationToken.getName()\" because \"authenticationToken\" is null
org.springframework.security.web.context.HttpSessionSecurityContextRepository$SaveToSessionResponseWrapper: Did not store anonymous SecurityContext
org.springframework.security.web.context.SecurityContextPersistenceFilter: Cleared SecurityContextHolder to complete request
org.apache.juli.logging.DirectJDKLog: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException: Cannot invoke \"org.springframework.security.authentication.AbstractAuthenticationToken.getName()\" because \"authenticationToken\" is null] with root cause
java.lang.NullPointerException: Cannot invoke \"org.springframework.security.authentication.AbstractAuthenticationToken.getName()\" because \"authenticationToken\" is null
这是我的 HttpSecurity sn-p:
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http
.csrf()
// .disable().exceptionHandling().accessDeniedPage(\"/access-denied\")
.and()
.authorizeRequests()
.antMatchers(\"/sso/**\").permitAll()
.antMatchers(\"/error/**\").permitAll()
.antMatchers(\"/css/**\",\"/contact-us\",\"/actuator/**\",\"/isalive/**\").permitAll()
.anyRequest().authenticated()
.and()
.oauth2Login()
.defaultSuccessUrl(\"/myfirstpage\",true)
.and().exceptionHandling().authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))
.and()
.oauth2ResourceServer().jwt();
}
我知道我错过了一些东西,但我认为 Keycloak 提供了很多这样的东西 OOTB。初始 URL 是 /。我曾希望 .authenticated() 会强制它针对所有不允许的模式进行身份验证,但我可能错了。我错过了什么?
请注意,互联网上充斥着 Spring Boot + Keycloak 的示例(有些甚至很好)。它还有很多 Spring Boot + OAuth + Stateless JWT。它没有(我可以说)很多 Spring Boot + Keycloak + Stateless JWT。我从这个 JHipster repo 中得到了我能找到的一点点,但我觉得我错过了一些伟大的神奇步骤。
-
您能否评论一下为什么答案不令人满意?否则,你会接受吗?
标签: spring-boot spring-security keycloak spring-security-oauth2