启用 Spring OAuth2 ResourceServer 禁用 UsernamePasswordAuthenticationFilter

Enabling Spring OAuth2 ResourceServer disables UsernamePasswordAuthenticationFilter

我的 WebSecurityConfigurerAdaptor 中有以下内容:

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 编写),可以在获取有效访问令牌之前重定向到 /login。

然后我尝试启用我的 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,并转到 /login returns HTTP 404 错误。

我多次阅读示例 Spring OAuth2 应用程序 sparklr2 和 tonr2,但我无法弄清楚我的代码有什么问题。

这是一个很难追踪的错误。

我的代码最初使用 Spring 安全版本 4.0.2.RELEASE 和 Spring 版本 4.2.2.RELEASE。一旦我将我的 Maven pom.xml 更改为使用 Spring 安全版本 3.2.8.RELEASE 和 Spring 版本 4.1.8.RELEASE,我的代码就可以工作了。

可能与以下错误有关: resource server security custom request matching ignored