【问题标题】:Authorization Server授权服务器
【发布时间】:2017-04-13 04:09:04
【问题描述】:

我在我的项目中使用 Spring Rest + OAUTH2 + React。为了创建授权服务器,我从一个示例中获得了一些代码。但问题是我无法理解代码。谁能解释一下这段代码:

@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private AuthenticationManager authenticationManager;

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

    /**
     * This method configure client details service by using inMemory implementation. JDBC Implementation can also used
     */
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient("acme") // client id
                .secret("acmesecret") // required for trusted clients
                .authorizedGrantTypes("authorization_code", "refresh_token",
                        "password") // Grant types that are authorized for the client to use
                .scopes("openid") // scope to which the client is limited
                .autoApprove(true);
    }

    /**
     *  This method configure the grant types. By default all grant types are supported except password
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints)
            throws Exception {
        endpoints.authenticationManager(authenticationManager).accessTokenConverter(
                jwtAccessTokenConverter());
    }

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

【问题讨论】:

    标签: spring rest security authentication oauth-2.0


    【解决方案1】:

    Spring Boot 支持自动配置。您在这里看到的是有人扩展了一个 spring 自动配置类,以便根据他的需要对其进行自定义。

    TL;DR:

    他们设置了一个基于 JWT 的 oauth2 授权服务器。

    详细回答:

    在这种情况下,通过组合@EnableAuthorizationServer 和扩展AuthorizationServerConfigurerAdapter,您可以启用、操作和修改您的授权服务器。

    1. 在此示例中,他们希望使用 JWT,而不是使用普通的字符串标记。因此,第一个初始化的 bean 是JwtAccessTokenConverterMore on JWT
    2. configure(ClientDetailsServiceConfigurer clients) - 他们配置一个内存客户端以在应用程序中使用。
    3. configure(AuthorizationServerEndpointsConfigurer endpoints) - 他们将默认的 authenticationManager 配置为由 spring 初始化并注入到配置类的顶部,并将 accessTokenConverter 设置为使用 #1 中提到的 jwtAccessTokenConverter。这样做将允许他们在查询新令牌时生成 JWT 令牌。
    4. configure(AuthorizationServerSecurityConfigurer oauthServer) - 他们将所有端点设置为允许在有令牌认证用户 (oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()");) 时访问所有内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-31
      • 2013-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-08
      • 1970-01-01
      • 2020-06-23
      相关资源
      最近更新 更多