2015-10-16 96 views
0

我在WebSecurityConfigurerAdaptor如下:啓用春天的OAuth2 ResourceServer禁用UsernamePasswordAuthenticationFilter

protected void configure(HttpSecurity http) throws Exception { 
    http 
     .authorizeRequests() 
      .antMatchers("/login.jsp").permitAll() 
      .anyRequest().authenticated() 
      .and() 
     .exceptionHandling() 
      .accessDeniedPage("/login.jsp?authorization_error=true") 
      .and() 
     .csrf() 
      .disable() 
     .formLogin() 
      .loginProcessingUrl("/login") 
      .loginPage("/login.jsp") 
      .failureUrl("/login.jsp?authentication_error=true"); 
} 

我有一個OAuth2用戶端(PHP寫的),並得到一個有效的訪問令牌,然後才能被重定向到/登錄。

然後我試着讓我的ResourceServerConfigurerAdapter

@Configuration 
@EnableResourceServer 
protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter { 

    @Override 
    public void configure(ResourceServerSecurityConfigurer resources) 
      throws Exception { 
     resources.resourceId("myResource").stateless(false); 
    } 

    @Override 
    public void configure(HttpSecurity http) throws Exception { 
     http 
      .sessionManagement() 
       .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED) 
       .and() 
      .requestMatchers() 
       .antMatchers("/me") 
       .and() 
      .authorizeRequests() 
       .antMatchers("/me").access("#oauth2.hasScope('email')"); 
    } 

} 

一旦ResourceServer啓用,則永遠不會調用的UsernamePasswordAuthenticationFilter,和去/登錄返回HTTP 404錯誤。

我讀了很多次Spring OAuth2應用程序sparklr2和tonr2的示例,但我無法弄清楚我的代碼有什麼問題。

回答

2

這是一個非常難以追查的錯誤。

我的代碼最初使用Spring Security版本4.0.2.RELEASE和Spring版本4.2.2.RELEASE。一旦我改變我的maven pom.xml以使用Spring Security版本3.2.8.RELEASE和Spring版本4.1.8.RELEASE,我的代碼就可以工作。

它可能與以下錯誤有關: resource server security custom request matching ignored