【问题标题】:Spring Security OAuth2, which decides security?Spring Security OAuth2,它决定了安全性?
【发布时间】:2015-04-16 17:07:11
【问题描述】:

我一直在尝试使用 Dave Syer 的指南和 JHipster 的一些灵感来实现 OAuth2 身份验证服务器。但我无法弄清楚这一切是如何协同工作的。

当我使用 ResourceServerConfigurerAdapter 时,使用 WebSecurityConfigurerAdapter 的安全设置似乎被覆盖了。

@Configuration
@EnableResourceServer
public class OAuth2ResourceConfig extends ResourceServerConfigurerAdapter {

    private TokenExtractor tokenExtractor = new BearerTokenExtractor();

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .addFilterAfter(contextClearer(), AbstractPreAuthenticatedProcessingFilter.class)
                .authorizeRequests()
                .anyRequest().authenticated().and().httpBasic();
    }

    private OncePerRequestFilter contextClearer() {
        return new OncePerRequestFilter() {
            @Override
            protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
                if (tokenExtractor.extract(request) == null) {
                    SecurityContextHolder.clearContext();
                }
                filterChain.doFilter(request, response);
            }
        };
    }

@Component
public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

    private final AuthenticationManager authenticationManager;

    @Autowired
    public CustomWebSecurityConfigurerAdapter(AuthenticationManager authenticationManager) {
        this.authenticationManager = authenticationManager;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .parentAuthenticationManager(authenticationManager);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .formLogin()
                    .loginPage("/login").permitAll()
                .and()
                    .authorizeRequests().antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
                .and()
                    .requestMatchers().antMatchers("/login", "/oauth/authorize", "/oauth/confirm_access")
                .and()
                    .authorizeRequests().anyRequest().authenticated();
    }
}

这是取自几个不同示例的代码,因此它们可能无法很好地混合。但是我找不到 OAuth2 的好的文档/示例列表(不像 Spring Boot,它有一个很棒的文档),所以我在理解它们如何组合在一起时遇到了问题。 如果我不将 loginForm 添加到 ResourceServerConfigurerAdapter,它只会让我未经授权。但我在 WebSecurityConfigurererAdapter 中将其定义为 permitAll()。

这是 AuthorizationServerConfigurerAdapter:

@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private JwtAccessTokenConverter jwtAccessTokenConverter;

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient("acme")
                .secret("acmesecret")
                .authorizedGrantTypes("authorization_code", "refresh_token",
                        "password").scopes("openid");
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints)  throws Exception {
        endpoints.authenticationManager(authenticationManager).accessTokenConverter(jwtAccessTokenConverter);
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
        oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()");
    }
}

我做错了什么?我是否必须在 ResourceServerConfigurerAdapter 中设置所有安全性?我还需要 WebSecurityConfigurerAdapter 吗?

如果有人知道任何指南、教程、博客或任何类似的东西可以帮助我了解它是如何工作的,那将不胜感激。

亲切的问候,肯尼斯。

【问题讨论】:

  • 据我所知,您的 OAuth2ResourceConfig 是多余的。只是说。
  • 有什么症状(你走的是什么路,你看到了什么)?使用 curl(-v 查看标题)和 Spring Security 的 DEBUG 日志记录应该告诉您您需要知道的一切。
  • 症状是它基本上忽略了WebSecurityConfigurerAdapter。但是在阅读了下面的解释之后,我对它的工作原理有了更多了解。
  • 我没有得到你的 CustomWebSecurityConfigurerAdapter 配置来让我进入登录表单,我有 404。你是如何测试它的?

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


【解决方案1】:

您需要WebSecurityConfigurerAdapter 来保护 /authorize 端点并为用户提供身份验证方式。 Spring Boot 应用程序会为您做到这一点(通过添加自己的 WebSecurityConfigurerAdapter 和 HTTP 基本身份验证)。它默认创建一个 order=0 的过滤器链,并保护所有资源,除非您提供请求匹配器。 @EnableResourceServer 做了类似的事情,但它添加的过滤器链默认为 order=3。 WebSecurityConfigurerAdapter 有一个 @Order(100) 注释。因此,首先将检查 ResourceServer(身份验证),然后检查您在 WebSecurityConfigureAdapter 扩展中的检查。

您的配置看起来很正常(登录链优先,但只匹配一小部分请求)。

【讨论】:

  • 我必须让我的 WebSecurityConfigurerAdapter order=2 才能让它工作。
  • 您是使用 Order 注释指定订单还是在哪里指定?我有同样的问题,用 Order 注释配置没有任何效果,ResourceServerConfigurerAdapter 是唯一使用的类,WebSecurityConfigurerAdapter 被完全忽略。
  • @Cenobyte321 implements Ordered@Order(1)@Order(2)
  • @DaveSyer,是否可以在每个之上使用两层安全(基本+承载)?
猜你喜欢
  • 1970-01-01
  • 2012-06-15
  • 2015-06-13
  • 1970-01-01
  • 2016-03-29
  • 2016-04-20
  • 2017-08-06
  • 2014-07-16
相关资源
最近更新 更多