Java SpringBoot2+OAuth2:为令牌配置身份验证代码的交换
我按照本教程介绍了如何配置OAuth2客户机。不幸的是,一旦用户使用Idp Okta进行身份验证,就会发生代码重定向,导致重定向循环:/login->/authorize…->/登录…->/登录 Firefox检测到服务器正在以一种永远无法完成的方式重定向对此地址的请求 有人知道问题是什么或可能是什么以及如何解决吗?详情如下 Okta配置: 登录重定向URI: 注销重定向URI: 登录发起人:仅应用程序 启动登录URI: 配置属性包括:Java SpringBoot2+OAuth2:为令牌配置身份验证代码的交换,java,spring-boot,spring-security,spring-security-oauth2,okta,Java,Spring Boot,Spring Security,Spring Security Oauth2,Okta,我按照本教程介绍了如何配置OAuth2客户机。不幸的是,一旦用户使用Idp Okta进行身份验证,就会发生代码重定向,导致重定向循环:/login->/authorize…->/登录…->/登录 Firefox检测到服务器正在以一种永远无法完成的方式重定向对此地址的请求 有人知道问题是什么或可能是什么以及如何解决吗?详情如下 Okta配置: 登录重定向URI: 注销重定向URI: 登录发起人:仅应用程序 启动登录URI: 配置属性包括: okta: oauth2: client:
okta:
oauth2:
client:
client-id: clientId
client-secret: clientSecret
scope: openid profile email
client-authentication-scheme: form
access-token-uri: https://mydomain.oktapreview.com/oauth2/myapp/v1/token
user-authorization-uri: https://mydomain.oktapreview.com/oauth2/myapp/v1/authorize
resource:
user-info-uri: https://mydomain.oktapreview.com/oauth2/myapp/v1/userinfo
过滤器是:
private Filter filter() {
OAuth2ClientAuthenticationProcessingFilter filter = new OAuth2ClientAuthenticationProcessingFilter(
"/login");
OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(oktaClient(), oauth2ClientContext);
filter.setRestTemplate(restTemplate);
UserInfoTokenServices tokenServices = new UserInfoTokenServices(oktaResource().getUserInfoUri(),
oktaClient().getClientId());
tokenServices.setRestTemplate(restTemplate);
filter.setTokenServices(tokenServices);
return filter;
}
WebSecurityConfigureAdapter配置为:
@Configuration
@EnableOAuth2Client
public class WebSecConfig extends WebSecurityConfigurerAdapter {
....
@Override
public void configure(HttpSecurity http) throws Exception {
http.antMatcher("/**").authorizeRequests()
.antMatchers("/", "/login**", "/logout**", "/v2/api-docs", "/configuration/ui",
"/configuration/security", "/swagger-resources/**", "/swagger-ui.html", "/webjars/**")
.permitAll()
.anyRequest().authenticated().and().exceptionHandling()
.authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/login")).and().csrf()
.csrfTokenRepository(
CookieCsrfTokenRepository.withHttpOnlyFalse()).and().addFilterBefore(filter(),
BasicAuthenticationFilter.class);
}
....
}
更新:
解决方案是将LoginUrlAuthenticationEntryPoint/login更改为LoginUrlAuthenticationEntryPoint/并重新创建授权服务器。您应该使用默认授权服务器或您创建的授权服务器。如果使用默认值,它应该类似于:
https://mydomain.oktapreview.com/oauth2/default
请使用main classI更新您的问题。我创建了一个新的身份验证服务器,该问题现在是一个重定向循环。关于问题/解决方案有什么想法吗?我建议使用Okta教程来解决你的问题。我写了几封信,也许能帮上忙。你的用例是什么?您是否只想使用Spring Boot 2/Spring Security 5登录Spring Boot应用程序?最终目标是使用Spring Security+OAuth2对单页应用程序的用户进行身份验证/授权。关于身份验证,我想让JWT返回SPA。对于每个请求,我都希望API能够使JWT有效,并通过JWT中包含的组声明验证授权。当JWT过期时,我希望通过刷新令牌来刷新令牌。我使用了一个带有a的令牌来通过@enableAuth2sso进行身份验证。当我对根路径执行GET请求时,即/我获取所有主体类内容,刷新令牌除外,即使它是在Okta的授权服务器中配置的。我还没有弄清楚如何通过JWT中包含的组声明来验证JWT和授权,或者如何刷新。Spring Security和Otka的Spring Boot starter旨在处理提交刷新令牌以获得新访问令牌的问题。就团体所声称的,你可以像我们在吉普斯特所做的那样: