0

독립 실행 형 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"; 
} 

이 경우 베어러 토큰이없는 원시 컬 호출이 성공합니다.

두 번째 경우로 다음 코드를 추가하고 위의 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(); 
    } 
} 

답변

0

귀하의 코드가

를 추가 한 후 나를 위해 작동 '공용 클래스 AppSecurityInitializer이 AbstractSecurityWebApplicationInitializer를 확장 {

}`

빈 클래스는 봄 보안

수 있도록 확장