春季启动安全性JWT hasauthority无法正常工作
我使用 Auth0 和 JWT 保护我们的 Spring Boot API。
当我解码 JWT 时,结果是:
{
"iss": "https://****.auth0.com/",
"sub": "auth0|62483c747a2519006fd75914",
"aud": [
"https://***.com/",
"https://***.com/userinfo"
],
"iat": 1648903665,
"exp": 1648990065,
"azp": "ufoZPRxuRqAeulM5u7R5RdlddXvPnOTn",
"scope": "openid profile email",
"permissions": [
"read:brands"
]
}
在权限数组中有 read:brands
。但是,当我尝试限制 API 时,它不起作用并返回 GET /admin/v1/brands
的 403 状态。
这是安全配置:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Value("${auth0.audience}")
private String audience;
@Value("${spring.security.oauth2.resourceserver.jwt.issuer-uri}")
private String issuer;
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers(
"/v2/api-docs",
"/configuration/ui",
"/swagger-resources/**",
"/configuration/security",
"/swagger-ui/**",
"/webjars/**",
"/v3/api-docs/**", "/swagger-ui/**", "/swagger-ui.html"
);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors().and()
.csrf().disable()
.authorizeRequests()
.antMatchers("/v3/api-docs/**", "/swagger-ui/**", "/swagger-ui.html").permitAll()
.antMatchers(HttpMethod.GET, "/admin/v1/brands").hasAuthority("read:brands")
.anyRequest().authenticated()
.and()
.oauth2ResourceServer()
.jwt()
.decoder(jwtDecoder());
}
JwtDecoder jwtDecoder() {
OAuth2TokenValidator<Jwt> withAudience = new AudienceValidator(audience);
OAuth2TokenValidator<Jwt> withIssuer = JwtValidators.createDefaultWithIssuer(issuer);
OAuth2TokenValidator<Jwt> validator = new DelegatingOAuth2TokenValidator<>(withAudience, withIssuer);
NimbusJwtDecoder jwtDecoder = (NimbusJwtDecoder) JwtDecoders.fromOidcIssuerLocation(issuer);
jwtDecoder.setJwtValidator(validator);
return jwtDecoder;
}
@Bean
CorsConfigurationSource corsConfigurationSource() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
var config = new CorsConfiguration().applyPermitDefaultValues();
config.setAllowedMethods(List.of(
HttpMethod.GET.name(),
HttpMethod.PUT.name(),
HttpMethod.POST.name(),
HttpMethod.DELETE.name()
));
source.registerCorsConfiguration("/**", config);
return source;
}
}
I secure our Spring Boot API with Auth0 and JWT.
When I decode JWT the result is:
{
"iss": "https://****.auth0.com/",
"sub": "auth0|62483c747a2519006fd75914",
"aud": [
"https://***.com/",
"https://***.com/userinfo"
],
"iat": 1648903665,
"exp": 1648990065,
"azp": "ufoZPRxuRqAeulM5u7R5RdlddXvPnOTn",
"scope": "openid profile email",
"permissions": [
"read:brands"
]
}
In the permission array there is read:brands
. But when I try to limit API it's not working and returns 403 status for GET /admin/v1/brands
.
Here is security config:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Value("${auth0.audience}")
private String audience;
@Value("${spring.security.oauth2.resourceserver.jwt.issuer-uri}")
private String issuer;
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers(
"/v2/api-docs",
"/configuration/ui",
"/swagger-resources/**",
"/configuration/security",
"/swagger-ui/**",
"/webjars/**",
"/v3/api-docs/**", "/swagger-ui/**", "/swagger-ui.html"
);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors().and()
.csrf().disable()
.authorizeRequests()
.antMatchers("/v3/api-docs/**", "/swagger-ui/**", "/swagger-ui.html").permitAll()
.antMatchers(HttpMethod.GET, "/admin/v1/brands").hasAuthority("read:brands")
.anyRequest().authenticated()
.and()
.oauth2ResourceServer()
.jwt()
.decoder(jwtDecoder());
}
JwtDecoder jwtDecoder() {
OAuth2TokenValidator<Jwt> withAudience = new AudienceValidator(audience);
OAuth2TokenValidator<Jwt> withIssuer = JwtValidators.createDefaultWithIssuer(issuer);
OAuth2TokenValidator<Jwt> validator = new DelegatingOAuth2TokenValidator<>(withAudience, withIssuer);
NimbusJwtDecoder jwtDecoder = (NimbusJwtDecoder) JwtDecoders.fromOidcIssuerLocation(issuer);
jwtDecoder.setJwtValidator(validator);
return jwtDecoder;
}
@Bean
CorsConfigurationSource corsConfigurationSource() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
var config = new CorsConfiguration().applyPermitDefaultValues();
config.setAllowedMethods(List.of(
HttpMethod.GET.name(),
HttpMethod.PUT.name(),
HttpMethod.POST.name(),
HttpMethod.DELETE.name()
));
source.registerCorsConfiguration("/**", config);
return source;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到了!
我必须添加
jwtauthenticationconverter
Found it!
I had to add
jwtAuthenticationConverter