10 Temmuz 2018 Salı

SpringSecurity OAuth2 AuthorizationServerEndpointsConfigurer Sınıfı

Giriş
AuthorizationServerConfigurerAdapter sınıfının configure metodu bu nesneyi verir. Authorization için kullanılack endpoint'lerin ayarlarını yapabilmeyi sağlar.

Endpoint'ler için TokenStore, AuthenticationManager  gibi ayarlarını yapabilmemizi sağlar.

İmzası şöyle.
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception;
accessTokenConverter metodu
Örnek
Şöyle yaparızz
@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationConfig extends AuthorizationServerConfigurerAdapter {

  @Autowired
  private AuthenticationManager authenticationManager;
  @Autowired
  private BCryptPasswordEncoder passwordEncoder;
  @Autowired
  private UserService userService;
  @Autowired
  private DataSource dataSource;

  @Override
  public void configure(AuthorizationServerEndpointsConfigurer endpoints){
    endpoints
      .allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST)
      .authenticationManager(authenticationManager)
      .accessTokenConverter(accessTokenConverter())
      .userDetailsService(userService)
      .tokenStore(tokenStore());
  }

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

  @Bean
  public JwtAccessTokenConverter accessTokenConverter() {
    JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
    //todo move the signing key to application.properties
    converter.setSigningKey("123");
    return converter;
  }
}
authenticationManager metodu
Örnek
Şöyle yaparız.
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

  @Autowired
  private AuthenticationManager authenticationManager;
  ...

  @Override
  public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
    endpoints.authenticationManager(authenticationManager);
  }
}
tokenStore metodu
JwtTokenStore,InMemoryTokenStore kullanılabilir.

Örnek
Şöyle yaparız.
@Configuration
public class OAuth2Config extends AuthorizationServerConfigurerAdapter {

  @Autowired
  @Qualifier("authenticationManagerBean")
  private AuthenticationManager authenticationManager;

  @Autowired
  private SecurityConfiguration securityConfig;

  @Bean
  public JwtAccessTokenConverter tokenEnhancer() {
    JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
    converter.setSigningKey(privateKey);
    converter.setVerifierKey(publicKey);
    return converter;
  }

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

  @Override
  public void configure(AuthorizationServerEndpointsConfigurer endpoints)
  throws Exception {
    endpoints.authenticationManager(authenticationManager).tokenStore(tokenStore())
      .accessTokenConverter(tokenEnhancer())
      .allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST);
  }
  ...
}
userApprovalHandler metodu
Şöyle yaparız. TokenStoreUserApprovalHandler kullanılabilir
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints)
throws Exception {
  endpoints.tokenStore(this.tokenStore)
    .userApprovalHandler(this.userApprovalHandler)
    .authenticationManager(this.authenticationManager);
}
userDetailsService metodu
Örnek
Şöyle yaparız.
@Autowired
private AuthenticationManager authenticationManager;

@Autowired
private UserDetailsService userDetailsService;


@Autowired
private TokenStore tokenStore;


endpoints.tokenStore(tokenStore)
  .authenticationManager(authenticationManager)
  .userDetailsService(userDetailsService);

Hiç yorum yok:

Yorum Gönder