我得到了在这个项目上实现jwks的任务。在我们的项目中,我们使用oauth2实现了令牌验证检查。我们使用jks格式证书来获取公钥。私钥不在我们的项目中使用,因为我们需要检查令牌的有效性。我们的目标是摆脱.jks文件。对于jwk来说,资源太少了,因此有些地方不清楚。如果我正确理解,那么jwks就意味着在资源中有一个带有键的jwks.json文件,我们从令牌头中选择了这个文件。基于这些文档,还不清楚它是什么样的文件,以及它是如何加载以供孩子检查的,也就是说,在什么时候,它happens.Does任何人都有一个可以用作示例的项目?提前感谢
发布于 2021-06-10 17:54:29
您可以使用spring引导资源服务器实现。
首先,您需要的是向项目添加以下依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
其次,您需要添加身份验证服务器配置。您提到的JSON文件必须位于身份验证服务器上,或者可以使用身份验证服务器的JWKs。属性文件中应该有这样的配置。
spring.security.oauth2.resourceserver.jwt.jwk-set-uri=https:/example.com/.well-known/openid-configuration/jwks
spring.security.oauth2.resourceserver.jwt.issuer-uri=https:/example.com
最后,您需要遵循自然的spring安全API配置。你需要的是以下几点。
@Configuration
@EnableWebSecurity
public class SecureSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Value("${spring.security.oauth2.resourceserver.jwt.jwk-set-uri}")
private String jwtSetUri;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.requiresChannel().anyRequest().requiresInsecure().and().cors()
.and().csrf().disable()
.authorizeRequests()
.antMatchers(HttpMethod.GET, "some path1").permitAll()
.antMatchers(HttpMethod.POST, "some path2").permitAll()
.antMatchers(HttpMethod.GET, "some path3").permitAll()
.antMatchers("/**").hasAuthority("some scope") // if you need this scope.
.anyRequest()
.authenticated()
.and()
.oauth2ResourceServer()
.jwt().decoder(jwtDecoder());
}
@Bean
CorsConfigurationSource corsConfigurationSource() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration().applyPermitDefaultValues();
config.addAllowedMethod("PUT");
config.addAllowedMethod("DELETE");
source.registerCorsConfiguration("/**", config);
return source;
}
private JwtDecoder jwtDecoder() {
return NimbusJwtDecoder.withJwkSetUri(jwtSetUri)
.jwtProcessorCustomizer(p -> p.setJWSTypeVerifier(
new DefaultJOSEObjectTypeVerifier<>(new JOSEObjectType("at+jwt")))).build();
}
}
在此之后,对API的每个请求都应该由Spring通过使用身份验证服务器自动验证。
https://stackoverflow.com/questions/67924811
复制相似问题