私はSpring Security OAuth2
を使用しています。基本的な構成は問題なく動作しています。私は今誰かがいくつかのエンドポイントにアクセスする権限を持っているかどうかを判断するカスタムロジックを含む別のWebSecurityConfigurerAdapter
を持っています。しかし、私が何を試しても実行されません。以下は私のOAuth2
構成とトピックに関する私の知見です。認証サーバ:OAuth2リソースサーバーと一緒にカスタムhttpセキュリティー構成
@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private TokenStore tokenStore;
@Autowired
private AuthenticationManagerBuilder authenticationManager;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints)throws Exception {
endpoints.authenticationManager(authentication -> authenticationManager.getOrBuild().authenticate(authentication)).tokenStore(tokenStore);
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory().withClient("CLIENT_NAME")...;
}
}
リソースサーバー:これまで
@Configuration
@EnableResourceServer
public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter {
@Autowired
private TokenStore tokenStore;
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().authenticated();
}
@Override
public void configure(final ResourceServerSecurityConfigurer resources) throws Exception {
resources.tokenStore(tokenStore);
}
}
とても良いです。カスタムWebSecurityConfigurerAdapter
が行動に入ると、私は問題を持ち始めます。 EnableResourceServer
注釈付きBeanは、でWebSecurityConfigurerAdapter
を作成するので、すべての要求で最初に実行され、ユーザーは正常に認証/承認されますが、WebSecurityConfiguration
のカスタムロジックは実行されません。一方、WebSecurityConfiguration
にOrder(2)
以下を設定すると、カスタムaccess
のルールが実行されますが、匿名ユーザーからのものであると常に言います(@EnableResourceServer
で作成されたBeanのルールは実行されないため)。ただ、サイドノートとして
@EnableWebSecurity
@Configuration
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Bean
public TokenStore tokenStore() {
return new InMemoryTokenStore();
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/...");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.authorizeRequests().antMatchers(HttpMethod.GET, "/path/**")
.access("@security.hasPermission(authentication, 'SOME', 'VALUE')");
http.authorizeRequests().anyRequest().authenticated();
}
}
は、
access
ルールで
@security
参照は、単純な命名のSpring Beanです:
@Component("security")
public class SecurityService {
public boolean hasPermission(Authentication authentication, String param, String anotherParam) { ... }
}
私はWebSecurityConfiguration
でカスタムアクセスルールを検証する統合テストを持っており、彼らが働きます(私はそこで認証をスキップするので)。私は、認証のためだけにリソースサーバーを使用し、次に承認のための私のカスタムHTTPセキュリティを使用できるようにしたいと思います。