2014-09-24 3 views
1

다음과 같은 문제가 있습니다. 일부 타사 OAuth2에서 승인 될 때까지 URI를 보호하고 싶습니다. http://docs.spring.io/spring-security/oauth/apidocs/org/springframework/security/oauth2/config/annotation/web/configuration/EnableOAuth2Client.html을 바탕으로, 나는이 있습니다스프링에서 적어도 하나의 매핑이 필요합니다.

@Configuration 
@EnableOAuth2Client 
public class OAuth2Client extends OAuth2ClientConfiguration { 

    @Bean 
    public Filter filter() { 
     DelegatingFilterProxy f = new DelegatingFilterProxy(); 
     f.setTargetBeanName("oauth2ClientContextFilter"); 
     return f; 
    } 

    @Resource 
    @Qualifier("oauth2ClientContextFilter") 
    private OAuth2ClientContextFilter oauth2ClientContextFilter; 

    @Resource 
    @Qualifier("accessTokenRequest") 
    private AccessTokenRequest accessTokenRequest; 

    @Bean 
    public OAuth2ProtectedResourceDetails remote() { 
     AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails(); 
     details.setUserAuthorizationUri("http://localhost2/oauth/authorize"); 
     return details; 
    } 

    @Bean 
    public OAuth2RestOperations restTemplate() { 
     return new OAuth2RestTemplate(remote(), new DefaultOAuth2ClientContext(
       accessTokenRequest)); 
    } 

} 

그리고

@Configuration 
@EnableResourceServer 
public class ResourceServerConfig extends ResourceServerConfigurerAdapter { 
    // Empty for now... 
} 

그리고 마지막으로

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 
    @Override 
    protected void configure(AuthenticationManagerBuilder auth) 
      throws Exception { 
     super.configure(auth); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.requestMatchers().antMatchers("/protectedUri").and() 
       .authorizeRequests().requestMatchers() 
       .hasRole("#oauth2.hasScope('read')"); 

    } 
} 

하지만이 제공 :

java.lang.IllegalStateException을 : 적어도 하나의 매핑이 필요합니다. (즉, . authorizeRequests() anyRequest.authenticated()) 나는 아무 소용에 HttpSecurity 빌더의 꽤 많은 조합을 시도했습니다

- 도움, 또는이 방법이 완전히 해제 기지?

답변

0

이 접근법은 완전히 기본입니까?

예. 비어있는 ResourceServerConfigurerAdapter은 도움이되지 않습니다. 보호 된 경로를 구성해야합니다 (예 :

@Override 
public void configure(HttpSecurity http) throws Exception { 
    http.authorizeRequests().antMatchers("/protectedUri").authenticated(); 
} 

(사용자의 WebSecurityConfigurerAdapter은 제외).

상단의 클라이언트 구성도 잘못되었지만 보호 된 리소스와 관련이 없습니다 (클라이언트 구성 방법을 알고 싶다면 새로운 질문을 시작하십시오).

+0

예 클라이언트 구성은 나중에 사용하도록 설정했습니다. 하지만 이제는 WebSEcurityConfigurerAdapter를 제거하고 ResourceServerConfigurerAdapter에 구성 호출을 추가했습니다. 이름이 'oauth2ClientContextFilter'인 빈을 만드는 동안 오류가 발생했습니다. 요청한 bean이 현재 작성 중입니다. 해결할 수없는 순환 참조가 있습니까? –

+0

나중에 떠난 이유가 아직 남아 있습니까? –

+0

나는 나중에 그것을 위해 계산을 떠났다 - 그것이 중요 하냐? –

관련 문제