スタンドアロンのOAuth2リソースサーバーを設定していますが、要求を認証していないようですが、ベアラトークンなしでCURL呼び出しを使用しても成功するようです。また、私はグローバル・メソッドのセキュリティを設定しようとしましたが、すべての要求が@PreAuthorize('submitAll()')
は、当該要求に設定されている場合でも、エラーOAuth2リソースサーバーが要求を認証していない
An authentication object could not be found in the current security context
で拒否されます。私は、認証を機能させるための道筋(メソッドレベルのセキュリティー、またはトークンを検査する正しい設定)のいずれかを取ってうれしいです。
ここにコードがあります。
@Configuration
@EnableResourceServer
@EnableWebSecurity
class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
@Bean
public ResourceServerTokenServices tokenService() {
RemoteTokenServices tokenServices = new RemoteTokenServices();
tokenServices.setClientId("commsuite");
tokenServices.setClientSecret("secret");
tokenServices.setCheckTokenEndpointUrl("http://localhost:10386/oauth/check_token");
return tokenServices;
}
@Bean
public AuthenticationManager authenticationManager() {
final OAuth2AuthenticationManager oAuth2AuthenticationManager = new OAuth2AuthenticationManager();
oAuth2AuthenticationManager.setTokenServices(tokenService());
return oAuth2AuthenticationManager;
}
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.tokenServices(tokenService()).authenticationManager(authenticationManager());
}
@Override
public void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/api/2/**").authorizeRequests().anyRequest().authenticated();
}
}
/* Here is the api controller code with the call */
@RequestMapping(value="/test", method = RequestMethod.GET, produces = {"text/plain"})
@ResponseStatus(HttpStatus.OK)
public @ResponseBody
String testEndpoint() {
return "Gnarly Dudes\n";
}
この場合、ベアラトークンを持たないローカルカールコールは成功します。
2番目のケースでは、次のコードを追加して上記のtestEndpoint
メソッドに@PreAuthorize("permitAll()")
を追加して、メソッドレベルのセキュリティ注釈を使用しようとしました。この場合、生のCURL呼び出しと、別の認証サーバーから取得した有効なベアラトークンを使用した呼び出しの両方でAuthentication object could not be found
エラーが発生します。私はラインに沿って何かが足りないと思う。おかげで...
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
@Override
protected MethodSecurityExpressionHandler createExpressionHandler() {
return new OAuth2MethodSecurityExpressionHandler();
}
}