访问因令牌撤销而被拒绝
Access denied for token revoke
我正在尝试使用 Spring OAuth2 实现令牌撤销。我创建了这个端点:
@PostMapping("/oauth/revoke")
public ResponseEntity<String> revoke(@RequestParam Map<String, String> params) {
RevocationService revocationService = revocationServiceFactory
.create(params.get("token_type_hint"));
revocationService.revoke(params.get("token"));
return ResponseEntity.ok().build();
}
Github code
我尝试配置这些权限:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.requestMatchers().antMatchers("/oauth/revoke").and()
.httpBasic().and()
.authorizeRequests()
.anyRequest().authenticated()
.and()
.httpBasic()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
我可以成功生成 OAuth2 令牌,但我总是 Access denied
使用此请求:
curl --location --request POST 'http://localhost:8080/oauth/revoke' \
--header 'Authorization: Basic YWRtaW46cXdlcnR5' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'token=.......' \
--data-urlencode 'token_type_hint=access_token'
你知道如何解决这个问题吗?我想 HttpSecurity
没有正确配置,但我找不到解决这个问题的方法。
你能尝试在 WebSecurityConfigurerAdapter
的 class 水平上添加 @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
吗?
To override the access rules without changing any other
auto-configured features add a @Bean of type
WebSecurityConfigurerAdapter with
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) and configure it to
meet your needs.
来自https://docs.spring.io/spring-boot/docs/2.0.0.M5/reference/html/boot-features-security.html
.csrf()
.disable()
.requestMatchers().antMatchers("/oauth/revoke").permitAll().and()
.httpBasic().and()
将禁用端点 /oauth/revoke
所需的身份验证,并将摆脱访问被拒绝的错误响应。您没有指定请求撤销操作的用户是否必须经过身份验证。请注意,假定的解决方案必须先于对父路径的任何身份验证限制。如果有例如
.csrf()
.disable()
.requestMatchers().anyRequest().authenticated().and()
.requestMatchers().antMatchers("/oauth/revoke").permitAll().and()
.httpBasic().and()
它不会工作,因为父路径(“/”)需要身份验证。在 spring 中,安全身份验证要求必须位于任何未经身份验证的子路径之后。
编辑:
由于您使用的是 httpBasic,如果您希望在请求撤销操作时对用户进行身份验证,您可以尝试将 curl 与以下选项一起使用
curl -i --user user1:user1Pass
我正在尝试使用 Spring OAuth2 实现令牌撤销。我创建了这个端点:
@PostMapping("/oauth/revoke")
public ResponseEntity<String> revoke(@RequestParam Map<String, String> params) {
RevocationService revocationService = revocationServiceFactory
.create(params.get("token_type_hint"));
revocationService.revoke(params.get("token"));
return ResponseEntity.ok().build();
}
Github code
我尝试配置这些权限:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.requestMatchers().antMatchers("/oauth/revoke").and()
.httpBasic().and()
.authorizeRequests()
.anyRequest().authenticated()
.and()
.httpBasic()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
我可以成功生成 OAuth2 令牌,但我总是 Access denied
使用此请求:
curl --location --request POST 'http://localhost:8080/oauth/revoke' \
--header 'Authorization: Basic YWRtaW46cXdlcnR5' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'token=.......' \
--data-urlencode 'token_type_hint=access_token'
你知道如何解决这个问题吗?我想 HttpSecurity
没有正确配置,但我找不到解决这个问题的方法。
你能尝试在 WebSecurityConfigurerAdapter
的 class 水平上添加 @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
吗?
To override the access rules without changing any other auto-configured features add a @Bean of type WebSecurityConfigurerAdapter with @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) and configure it to meet your needs.
来自https://docs.spring.io/spring-boot/docs/2.0.0.M5/reference/html/boot-features-security.html
.csrf()
.disable()
.requestMatchers().antMatchers("/oauth/revoke").permitAll().and()
.httpBasic().and()
将禁用端点 /oauth/revoke
所需的身份验证,并将摆脱访问被拒绝的错误响应。您没有指定请求撤销操作的用户是否必须经过身份验证。请注意,假定的解决方案必须先于对父路径的任何身份验证限制。如果有例如
.csrf()
.disable()
.requestMatchers().anyRequest().authenticated().and()
.requestMatchers().antMatchers("/oauth/revoke").permitAll().and()
.httpBasic().and()
它不会工作,因为父路径(“/”)需要身份验证。在 spring 中,安全身份验证要求必须位于任何未经身份验证的子路径之后。
编辑: 由于您使用的是 httpBasic,如果您希望在请求撤销操作时对用户进行身份验证,您可以尝试将 curl 与以下选项一起使用
curl -i --user user1:user1Pass