san*_*yav 7 spring spring-security oauth-2.0 spring-boot spring-security-oauth2
我需要使用两个不同的授权服务器(两个 Okta 实例)来验证来自作为后端 REST API 层的单个 Spring Boot 应用程序中的两个不同 Web 应用程序的身份验证令牌。
目前我有一台资源服务器使用以下配置:
@Configuration
@EnableWebSecurity
public class ResourceServerSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception{
http
.authorizeRequests().antMatchers("/public/**").permitAll()
.anyRequest().authenticated()
.and()
.oauth2ResourceServer().jwt();
}
}
Run Code Online (Sandbox Code Playgroud)
spring.security.oauth2.resourceserver.jwt.issuer-uri=https://dev-X.okta.com/oauth2/default
spring.security.oauth2.resourceserver.jwt.jwk-set-uri=https://dev-X.okta.com/oauth2/default/v1/keys
Run Code Online (Sandbox Code Playgroud)
并具有依赖项spring-security-oauth2-resource-server和spring-security-oauth2-jose在我的 Spring Boot 应用程序中(版本 2.2.4.RELEASE)
我想要进入的最终状态是,根据请求中设置的自定义 HTTP 标头,我想选择我的 Spring Boot 应用程序使用哪个 Okta 实例来解码和验证 JWT 令牌。
理想情况下,我的配置文件中有两个属性,如下所示:
jwkSetUri.X=https://dev-X.okta.com/oauth2/default/v1/keys
jwtIssuerUri.X=https://dev-X.okta.com/oauth2/default
jwkSetUri.Y=https://dev-Y.okta.com/oauth2/default/v1/keys
jwtIssuerUri.Y=https://dev-Y.okta.com/oauth2/default
Run Code Online (Sandbox Code Playgroud)
我应该能够使用 aRequestHeaderRequestMatcher来匹配安全配置中的标头值。我无法锻炼的是如何使用oauth2ResourceServer与安全配置相匹配的两个不同实例。
从 Spring security 5.3+ 开始,可以使用 JwtIssuerAuthenticationManagerResolver 对象
覆盖configure(HttpSecurity http)扩展的配置类内部WebSecurityConfigurerAdapter
JwtIssuerAuthenticationManagerResolver authenticationManagerResolver = new JwtIssuerAuthenticationManagerResolver(
"http://localhost:8080/auth/realms/SpringBootKeyClock",
"https://accounts.google.com/o/oauth2/auth",
"https://<subdomain>.okta.com/oauth2/default"
);
http.cors()
.and()
.authorizeRequests()
.antMatchers(HttpMethod.GET, "/user/info", "/api/foos/**")
.hasAnyAuthority("SCOPE_email")
.antMatchers(HttpMethod.POST, "/api/foos")
.hasAuthority("SCOPE_profile")
.anyRequest()
.authenticated()
.and()
.oauth2ResourceServer(oauth2 -> oauth2.authenticationManagerResolver(authenticationManagerResolver));
Run Code Online (Sandbox Code Playgroud)
小智 6
使用弹簧靴,现在不可能开箱即用。Spring Security 5.3 提供了执行此操作的功能(spring boot 2.2.6 仍然不支持 spring security 5.3)。请看以下问题:
https://github.com/spring-projects/spring-security/issues/7857
https://github.com/spring-projects/spring-security/pull/7887
通过遵循我提供的链接,可以手动配置资源服务器以使用多个身份提供者。提供的链接主要是针对spring boot webflux的开发。对于基本的 Spring Boot Web 开发,请观看此视频:
https://www.youtube.com/watch?v=ke13w8nab-k
| 归档时间: |
|
| 查看次数: |
4638 次 |
| 最近记录: |