[英]Documentation on creating a spring-security-oauth2 client using java config
[英]Spring Security OAuth Java Config
我有一個Spring應用程序 - 而不是 Spring Boot應用程序 - 我正在嘗試使用Spring Security聲明OAuth2保護的API部分。 當我在XML中定義http
元素時,我的授權和常規資源訪問配置很有效,但是當我嘗試使用ResourceServerConfigureAdapter#configure(HttpSecurity)
實現資源訪問塊時 - 使用@EnableResourceServer
等完成, configure
方法甚至不會觸發(端到端測試也失敗了)。 示例Java配置如下:
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.resourceId("oauth2/control");
}
@Override
public void configure(HttpSecurity http) throws Exception {
http.antMatcher("/api/**")
.authorizeRequests().anyRequest().hasRole("OAUTH_USER").and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
// before=PRE_AUTH_FILTER
.addFilterBefore(oAuth2AuthenticationProcessingFilter, AbstractPreAuthenticatedProcessingFilter.class)
.csrf().disable()
.anonymous().disable()
.exceptionHandling()
.accessDeniedHandler(oAuth2AccessDeniedHandler)
.authenticationEntryPoint(loginUrlAuthenticationEntryPoint);
}
我已經嘗試聲明了一些影響配置的不同方法,但沒有骰子。 也就是說,我嘗試了擴展ResourceServerConfigurerAdapter
頂級@Configuration
類和返回ResourceServerConfiguration
bean方法,如Dave Syer的例子中所示 ; 我也嘗試在適當的bean上顯式設置Ordered.HIGHEST_PRECEDENCE
順序。
值得注意的是,大多數應用程序的遺留安全配置都是通過上述XML元素定義的。 關於我的配置的另一個可能的紅色標志 - 為了讓令牌端點對基本的auth client_id進行身份驗證:client_secret我必須連接我自己的BasicAuthFilter(ProviderManager(DaoAuthProvider(ClientUserDetailsService(clientDetailsService)))))
,因為它是香草和'正確'的方式來驗證令牌端點,我希望spring配置默認為它。
無論如何,在任何情況下我都無法獲取ResourceServerConfigureAdapter#configure(HttpSecurity)
。 我的理論是:
關於前面的XML HTTP塊的一些事情阻止我的configure
方法甚至被調用
這只是一個Spring Boot功能 - 盡管我沒有看到這種效果的語言
在應用程序上下文中直接出現了一些我遺漏的對象(同樣,我在工作中的操作注釋 - 除了明顯的@Configuration
和@Bean
- 是@EnableWebSecurity
, @EnableAuthenticationServer
和@EnableResourceServer
)。
我做了一些配置錯誤(duh)。
任何例子都將非常感激
下面的示例OAuth2配置類(幾個不同的迭代之一):
@Configuration
@EnableWebSecurity
@EnableResourceServer
public class OAuth2Configuration {
... etc ...
@Bean
public ResourceServerConfiguration adminResources() {
// copped from https://github.com/spring-projects/spring-security-oauth/tree/master/tests/annotation/multi
ResourceServerConfiguration resource = new ResourceServerConfiguration() {
public void setConfigurers(List<ResourceServerConfigurer> configurers) {
super.setConfigurers(configurers);
}
};
resource.setConfigurers(Collections.singletonList(new ResourceServerConfigurerAdapter() {
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.resourceId("oauth2/control");
}
@Override
public void configure(HttpSecurity http) throws Exception {
http.antMatcher("/api/**")
.authorizeRequests().anyRequest().hasRole("OAUTH_USER").and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
// before=PRE_AUTH_FILTER
.addFilterBefore(oAuth2AuthenticationProcessingFilter, AbstractPreAuthenticatedProcessingFilter.class)
.csrf().disable()
.anonymous().disable()
.exceptionHandling()
.accessDeniedHandler(oAuth2AccessDeniedHandler)
.authenticationEntryPoint(loginUrlAuthenticationEntryPoint);
}
}));
resource.setOrder(Ordered.HIGHEST_PRECEDENCE);
return resource;
}
@Configuration
@EnableAuthorizationServer
public static class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
... etc ...
}
}
Analogogous <http>
即撈起配置:
<sec:http pattern="/api/**" create-session="stateless"
entry-point-ref="loginUrlAuthenticationEntryPoint">
<sec:anonymous enabled="false" />
<sec:csrf disabled="true"/> <!-- csrf tokens don't make sense for a 3rd party API -->
<sec:custom-filter ref="oauth2ProcessingFilter" before="PRE_AUTH_FILTER"/>
<sec:access-denied-handler ref="oauthAccessDeniedHandler" />
<sec:intercept-url pattern="/api/**" access="hasRole('OAUTH_USER')" />
</sec:http>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.