如何将<oauth:authorization-server>转换为Java Config?

时间:2015-12-11 20:52:36

标签: java spring spring-security spring-security-oauth2 spring-java-config

我相信所有这些都只是使用默认值,我们使用jdbc作为我们的令牌存储,客户端详细信息是默认值。看起来我们有自定义的userApprovalHandler。

<oauth:authorization-server 
    client-details-service-ref="clientDetails"
    token-services-ref="tokenServices"
    user-approval-handler-ref="userApprovalHandler">
    <oauth:client-credentials />
    <oauth:password authentication-manager-ref="authenticationManager"/>
</oauth:authorization-server>

我知道我们必须添加@EnableAuthorizationServer,但我不确定我是否真的需要实施AuthorizationServerConfigurer,或者如果因为春天可以弄明白这些都有注释(它确实如此)有时)?我也不确定设置其中一些的正确方法是什么。例如,我找不到设置client-credentialspassword的位置。我不确定他们是如何翻译的。

这是我到目前为止所提出的

@Configuration
@EnableAuthorizationServer
public class OAuth2Config implements AuthorizationServerConfigurer
{
    @Autowired private ClientDetailsService clientDetails;
    @Autowired private AuthorizationServerTokenServices tokenServices;


    @Override
    public void configure( final AuthorizationServerSecurityConfigurer security ) throws Exception
    {

    }

    @Override
    public void configure( final ClientDetailsServiceConfigurer clients ) throws Exception
    {
        clients.withClientDetails( clientDetails );
    }

    @Override
    public void configure( final AuthorizationServerEndpointsConfigurer endpoints ) throws Exception
    {
        endpoints.tokenServices( tokenServices );
    }
}

因为这看起来像一个小片段而且java配置不应该大得多,如果你能提供一个完整的配置类示例,这些选项会很棒。

1 个答案:

答案 0 :(得分:0)

请在下面找到示例代码: -

    @Configuration
    @EnableAuthorizationServer
    public class OAuth2CustomConfig extends
            AuthorizationServerConfigurerAdapter {

        @Autowired
        private AuthenticationManager authenticationManager;

        @Bean
        public JwtAccessTokenConverter jwtAccessTokenConverter() {
            JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
            KeyPair keyPair = new KeyStoreKeyFactory(new ClassPathResource(
                    "keystore.jks"), "foo".toCharArray()).getKeyPair("bar");
            converter.setKeyPair(keyPair);
            return converter;
        }

        @Override
        public void configure(AuthorizationServerSecurityConfigurer oauthServer)
                throws Exception {
            oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess(
                    "isAuthenticated()");
        }

        @Override
        public void configure(ClientDetailsServiceConfigurer clients)
                throws Exception {
            clients.inMemory()
                    .withClient("xxxx")
                    .secret("xxxxsecret")
                    .authorizedGrantTypes("authorization_code",
                            "refresh_token", "password").scopes("openid");
        }

        @Override
        public void configure(AuthorizationServerEndpointsConfigurer endpoints)
                throws Exception {
            endpoints.authenticationManager(authenticationManager)
                    .accessTokenConverter(jwtAccessTokenConverter());
        }

    }