2016-08-23 4 views
0

@PreAuthorize 주석을 사용하여 컨트롤러 메소드를 보호하여 OAuth2 공급자 보안을 강화하려고합니다. 나는 또한 이 oauth와 함께 작동 할 수 있도록 @EnableGlobalMethodSecurity을 추가했습니다. 나는 @PreAuthorize 표기법을 제거 할 수 있지만 내가 그들을 추가 할 때, 컴파일러 autowire가 예외 주입 누락 의 톤을 발생하고, 정말 할 수없는 경우Spring Oauth2 제공 업체 PreAuthorize가 자동 실행되지 않음

@Configuration 
@ComponentScan 
@EnableAutoConfiguration 
@EnableResourceServer 
@RestController 
public class Application { 

    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 

    @RequestMapping("/") 
    public String home() { 
     return "Hello World"; 
    } 

    @PreAuthorize("#oauth2.clientHasRole('ROLE_CUSTOM')") 
    @RequestMapping("/a") 
    public String a() { 
     return "foo"; 
    } 

    @PreAuthorize("#oauth2.clientHasRole('ROLE_TRUSTED_CLIENT')") 
    @RequestMapping("/b") 
    public String b() { 
     return "bar"; 
    } 

    // So that @PreAuthorize notations would work 
    @Configuration 
    @EnableGlobalMethodSecurity(prePostEnabled = true) 
    public static class MethodSecurityConfig extends GlobalMethodSecurityConfiguration { 
     @Override 
     protected MethodSecurityExpressionHandler createExpressionHandler() { 
      return new OAuth2MethodSecurityExpressionHandler(); 
     } 
    } 

    @Configuration 
    @EnableWebSecurity 
    public static class SecurityConfiguration extends WebSecurityConfigurerAdapter { 
    } 

    @Configuration 
    @EnableAuthorizationServer 
    protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter { 

     @Autowired 
     private AuthenticationManager authenticationManager; 

     @Override 
     public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { 
      endpoints.authenticationManager(authenticationManager).tokenStore(tokenStore()); 
     } 

     @Bean 
     public ApprovalStore approvalStore() throws Exception { 
      TokenApprovalStore store = new TokenApprovalStore(); 
      store.setTokenStore(tokenStore()); 
      return store; 
     } 

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

     @Override 
     public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
      // @formatter:off 
      clients.inMemory() 
       .withClient("my-trusted-client") 
        .authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit") 
        .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT") 
        .scopes("read", "write", "trust") 
        .resourceIds("oauth2-resource") 
        .accessTokenValiditySeconds(60) 
      .and() 
       .withClient("my-client-with-registered-redirect") 
        .authorizedGrantTypes("authorization_code") 
        .authorities("ROLE_CLIENT") 
        .scopes("read", "trust") 
        .resourceIds("oauth2-resource") 
        .redirectUris("http://anywhere?key=value") 
      .and() 
       .withClient("my-client-with-secret") 
        .authorizedGrantTypes("client_credentials", "password") 
        .authorities("ROLE_CLIENT","ROLE_CUSTOM") 
        .scopes("read") 
        .resourceIds("oauth2-resource") 
        .secret("secret"); 
     // @formatter:on 
     } 
    } 
} 

의미가 있습니다 :

여기에 내 현재 설정입니다 실제로 문제를 일으키는 부분을 찾아 내거나 찾아내는 일이 없습니다.

Here's my output

가 나는 종류의이 시점에서 붙어 추가 출력 또는 연구를 제공 할 수 없습니다 죄송합니다.

+0

설정의 나머지 부분에서'@ RestController'를 추출해 보셨습니까? 유지 관리가 엉망이라는 사실 외에도, Spring은 동일한 클래스 내에서 메소드 보안을 구성하는 데 문제가있을 수 있습니다. –

+0

BTW 귀하의 게시물 내에 스택 추적을 추가하십시오, 당신이 사용하는 붙여 넣기 상자가 이미 unavailble입니다. 또한 동일한 예외를 겪고있는 사용자가이 게시물을 찾을 수 있습니다. –

+1

@MichaelTecourt 나는 그것을 그렇게했다. 축하해, 원한다면 대답을 게시 할 수 있습니다. – Benedictus

답변

1

나머지 구성에서 @RestController 클래스를 추출해보십시오.

do-it-all 구성 클래스가 유지하기가 쉽지 않다는 점을 제외하면 Spring은 메소드 보안을 구성하고 같은 클래스 내에서 사용하는 데 문제가있을 수 있습니다.