【问题标题】:Enabling Spring OAuth2 ResourceServer disables UsernamePasswordAuthenticationFilter启用 Spring OAuth2 ResourceServer 会禁用 UsernamePasswordAuthenticationFilter
【发布时间】:2016-01-15 01:56:18
【问题描述】:

我的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 会返回 HTTP 404 错误。

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

【问题讨论】:

    标签: authentication spring-security spring-security-oauth2


    【解决方案1】:

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

    我的代码最初使用 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

    【讨论】:

      猜你喜欢
      • 2017-10-17
      • 2021-05-25
      • 2017-02-09
      • 2015-04-01
      • 1970-01-01
      • 2018-09-09
      • 1970-01-01
      • 2019-12-28
      • 1970-01-01
      相关资源
      最近更新 更多