使用OAuth2作为具有外部授权服务器的资源服务器的Spring Boot

时间:2019-08-27 10:08:32

标签: spring-boot oauth-2.0

我的理解是,我可以拥有一个完全在Spring之外使用单独的授权服务器的资源服务器。

要实现这一点,我为资源服务器实现提供了以下代码:

@Configuration
@EnableResourceServer
public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter {

    @Value("${oauth2.public-key-resource}")
    private String publicKeyResourcePath;

    @Override
    public void configure(ResourceServerSecurityConfigurer config) {
        config.tokenServices(tokenServices());
    }

    @Bean
    public TokenStore tokenStore() {
        return new JwtTokenStore(accessTokenConverter());
    }

    @Bean
    public JwtAccessTokenConverter accessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        Resource resource = new ClassPathResource(publicKeyResourcePath);
        String publicKey = null;
        try {
            publicKey = IOUtils.toString(resource.getInputStream());
        } catch (final IOException e) {
            throw new RuntimeException(e);
        }
        converter.setVerifierKey(publicKey);
        return converter;
    }

    @Bean
    @Primary
    public DefaultTokenServices tokenServices() {
        DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
        defaultTokenServices.setTokenStore(tokenStore());
        return defaultTokenServices;
    }
}

然后在我的应用程序启动类中,这样启用OAuth:

@SpringBootApplication
@EnableOAuth2Sso
public class MyApplication {

并添加了以下属性:

security.oauth2.client.client-id=...
security.oauth2.client.client-secret=...
security.oauth2.client.access-token-uri=...
security.oauth2.client.user-authorization-uri=...
security.oauth2.client.token-name=...

但是,我似乎无法使呼叫从外部服务器获得要使用的令牌。我是否缺少设置的一部分,或者我做错了什么?

0 个答案:

没有答案