【问题标题】:HttpSecurity configuration problems in spring-security-oauth2spring-security-oauth2中的HttpSecurity配置问题
【发布时间】:2016-03-14 20:57:38
【问题描述】:

我对 Spring 很陌生,我正在尝试使用 spring-security-oauth2 构建一个 OAuth 服务器。

我主要参考了spring.io给出的示例和教程。

https://github.com/spring-projects/spring-security-oauth/tree/master/samples/oauth2 http://spring.io/guides/tutorials/spring-boot-oauth2/

但是,我在HttpSecurity 配置方面遇到了一些问题。

我的文件夹结构如下。

├─java
│  └─com
│      └─example
│              Greeting.java
│              GreetingController.java
│              MvcConfig.java
│              OAuth2ServerConfig.java
│              SecurityConfig.java
│              SocialApplication.java
│
└─resources
    │  application.yml
    │
    └─templates
            hello.html
            home.html
            login.html

我在SecurityConfig.java中添加了一些HttpSecurity配置

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .authorizeRequests()
            .antMatchers("/", "/home").permitAll()
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/login")
            .permitAll()
            .and()
        .logout()
            .permitAll();
    }
}

好的,它工作正常。但是,我想保护Resource Server 中的greeting api(这是我刚刚从另一个演示中复制的一个简单的rest api)。 所以我在OAuth2ServerConfig.java中添加了一些HttpSecurity配置

@Configuration
@EnableResourceServer
protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        resources.resourceId(RESOURCE_ID);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        http
                .requestMatchers().antMatchers("/greeting")
                .and()
                .authorizeRequests()
                .anyRequest().access("#oauth2.hasScope('scope1')");
        // @formatter:on
    }

}

看来我只保护/greeting。但是,完成此操作后,我什至无法访问//login。它说Full authentication is required to access this resource

我是否错过了某些配置或做错了什么?

【问题讨论】:

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


    【解决方案1】:

    好吧,我找到了解决方案。 这个issue中提到了这种情况。 这是 Spring-Security 4.0.3 中的一个错误。

    解决方法是在配置Resource Server 时使用requestMatcher() 而不是requestMatchers()。 这是一个例子。

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                  .requestMatcher(
                      new OrRequestMatcher(
                          new AntPathRequestMatcher("/greeting"),
                          new AntPathRequestMatcher("/greeting2")
                      )
                  )
                  .authorizeRequests()
                  .antMatchers("/greeting").access("#oauth2.hasScope('scope1')")
                  .antMatchers("/greeting2").access("#oauth2.hasScope('scope2')");
    
    }
    

    【讨论】:

      【解决方案2】:

      我以前也遇到过同样的问题。

      为了解决这个问题,我用antMatcher(...)替换了requestMatchers().antMatchers(...).and()

      请尝试以下配置:

      public void configure(HttpSecurity http) throws Exception {
          // @formatter:off
          http
              .antMatcher("/greeting")
              .authorizeRequests()
                  .anyRequest().access("#oauth2.hasScope('scope1')");
          // @formatter:on
      }
      

      【讨论】:

      • 哦,它确实有效!所以如果我想保护很多资源,我只需要在每个.antMatcher()之间使用.and()
      【解决方案3】:

      由于anyRequest().access("#oauth2.hasScope('scope1')"); 在您的ResourceServerConfiguration 中,每个请求都需要使用OAuth 范围进行身份验证,包括//login(因为OAuth 安全配置是在Spring 安全链中的正常配置之后进行的) .如果您想使用 OAuth 保护 /greeting,您应该在您的 ResourceServerConfiguration 中添加这样的内容:

      @Override
      public void configure(HttpSecurity http) throws Exception {
          // @formatter:off
          http
              .authorizeRequests()
                  .antMatchers("/greeting").authenticated();
          // @formatter:on
      }
      

      【讨论】:

      • 我按照你说的改了代码。 /greeting 现在受到保护,但我在 SecurityConfig.java 中的配置不起作用。我无需登录即可访问/hello
      • 在添加 OAuth 配置之前您不能这样做?
      • 我不能,当我尝试在没有登录的情况下访问/hello 时,它应该重定向到/login,正如我在SecurityConfig.java 中配置的那样。
      【解决方案4】:

      我遇到了同样的问题,接受的答案确实解决了这个问题。但是,当前的解决方案应该是使用spring-security-oauth2 版本2.0.9.RELEASE。这个版本包括a fix,它应该在 Spring Security 3.2 和 4.0 中都可以工作。

      【讨论】:

      • 这应该是答案下的评论,但感谢您让社区知道。
      猜你喜欢
      • 2020-03-04
      • 2017-10-18
      • 2016-08-08
      • 2021-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-06
      • 2015-07-02
      相关资源
      最近更新 更多