首页 > 解决方案 > Spring security OAuth2 令牌自省

问题描述

我尝试通过自省使用 Spring 安全性来验证 OAuth2 令牌。实际上,当我调用控制器时,我的应用程序不会尝试访问 OAuth 服务器进行自省并返回 403。

我的自信:

spring.security.oauth2.resourceserver.opaquetoken.introspection-uri=https://example.net/introspection
spring.security.oauth2.resourceserver.opaquetoken.client-id=clientId
spring.security.oauth2.resourceserver.opaquetoken.client-secret=clientSecret

网络安全:

@EnableWebSecurity
public class WebServerConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.oauth2ResourceServer().opaqueToken();
    }
}

我的控制器:

@RestController
public class Controller {

    @PostMapping(value = "/foo", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.CREATED)
    public void uploadMedia(BearerTokenAuthentication bearerTokenAuthentication,
                        @RequestHeader(value = "Authorization") String bearerToken){

        System.out.println(bearerTokenAuthentication.getToken().getTokenValue());
    }
}

如何通过自省使用弹簧安全来验证 OAuth 令牌?

马修

标签: springsecurityoauthtokenintrospection

解决方案


尝试添加以下依赖项

<dependency>
    <groupId>org.springframework.security.oauth.boot</groupId>
    <artifactId>spring-security-oauth2-autoconfigure</artifactId>
    <version>2.1.8.RELEASE</version>
</dependency>

在应用程序属性中,您可以指定

security.oauth2.resource.token-info-uri=http://localhost:8080/oauth/check_token.

现在,核心类 - RemoteTokenServices.loadAuthentication 将用于调用授权服务器。你可以把它放在调试中。

security.oauth2.client.client-id=client1
security.oauth2.client.client-secret=secret1

推荐阅读