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: 少なくとも 1 つのマッピングが必要です (つまり、authorizeRequests().anyRequest.authenticated())

私は HttpSecurity ビルダーのかなりの数の組み合わせを試しましたが、役に立ちませんでした。それとも、このアプローチは完全にベースから外れていますか?

4

1 に答える 1

1

このアプローチは完全にベースから外れていますか?

はい。空っぽResourceServerConfigurerAdapterでは役に立ちません。保護されたパスを構成する必要があります。

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

(そしてあなたのを除外しますWebSecurityConfigurerAdapter)。

上部のクライアント構成も間違っているように見えますが、保護されたリソースには関係ありません (クライアントの構成方法を知りたい場合は、新しい質問を開始してください)。

于 2014-09-24T09:22:39.210 に答える