【问题标题】:spring-security-oauth2 authorization endpoint gets blocked by authorizationserverspring-security-oauth2 授权端点被授权服务器阻止
【发布时间】:2018-09-26 05:14:32
【问题描述】:

我已经进行了大量研究和调试,但我无法弄清楚以下破坏我的授权端点的行为......

我创建了一个简单的授权服务器配置,如下所示:

@EnableWebSecurity
@Configuration
@Order(4)
public class OpenIdConnectConfiguration extends WebSecurityConfigurerAdapter
{
  /**
   * {@inheritDoc}
   */
  @Override
  protected void configure(HttpSecurity http) throws Exception
  {
    http.csrf()
        .disable()
        .authorizeRequests()
        .antMatchers(ContextPaths.LOGIN,
                     ContextPaths.AUTHORIZATION_ENDPOINT,
                     ContextPaths.TOKEN_ENDPOINT,
                     SamlRedirectContextPaths.SAML_CONSUMER_URL,
                     SamlRedirectContextPaths.USER_REDIRECT_ENDPOINT)
        .permitAll()
        .and()
        .formLogin()
        .loginPage(ContextPaths.LOGIN + "?npa");
  }

  /**
   * authorization server configuration for Open ID Connect
   */
  @Configuration
  @EnableAuthorizationServer
  protected static class AuthorisationServerConfiguration extends AuthorizationServerConfigurerAdapter
  {
    ...

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception
    {
      super.configure(security);
      security.sslOnly();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception
    {
      super.configure(clients);
      clients.withClientDetails(customClientDetails);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception
    {
      endpoints.authorizationCodeServices(new CustomInMemoryAuthCodeService(cacheManager));
      endpoints.tokenStore(new CustomTokenStore(cacheManager));
      endpoints.pathMapping("/oauth/authorize", ContextPaths.AUTHORIZATION_ENDPOINT);
      endpoints.pathMapping("/oauth/token", ContextPaths.TOKEN_ENDPOINT);
    }
  }
  ...
}

使用此配置一切正常,可以使用授权码获取我的授权码和访问令牌。但是,如果我现在启用资源服务器,我的应用程序会因添加到安全过滤器链中的新过滤器而中断,我无法弄清楚问题是什么。

我的资源服务器配置如下:

  @EnableResourceServer
  @Order(5)
  @Configuration
  protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter
  {

    @Override
    public void configure(HttpSecurity http) throws Exception
    {
      http.authorizeRequests()
          .antMatchers("/oic/user-info")
          .authenticated();
    }
  }

这样,我可以毫无问题地为用户执行身份验证机制,但情况是 AuthorizationEndpoint 获得了 AnonymousAuthenticationToken。我正在将登录用户手动放入 spring 安全上下文中:

SecurityContextHolder.getContext().setAuthentication(authentication);

如果我在“org.springframework.security.oauth2.provider.endpoint.AuthorizationEndpoint”中进行一些调试

我可以看到我的 CustomAuthentication 不再在此安全上下文中。它被删除并替换为 AnonymousAuthenticationToken。如果我启用资源服务器,任何人都知道为什么这个 authenticationObject 会被覆盖?

【问题讨论】:

    标签: spring-security-oauth2


    【解决方案1】:

    经过大量调试,我找到了解决方案。 ResourceServerConfiguration 需要一个额外的条目:

    /**
     * the resource server configuration for the user-info endpoint
     */
    @Override
    public void configure(HttpSecurity http) throws Exception
    {
      http.sessionManagement()
          .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
          .and()
          .authorizeRequests()
          .antMatchers(ContextPaths.USER_INFO_ENDPOINT)
          .authenticated();
    }
    

    这就是诀窍

    【讨论】:

      猜你喜欢
      • 2015-05-22
      • 2015-03-31
      • 2021-10-29
      • 2020-03-14
      • 2014-05-10
      • 2013-12-01
      • 1970-01-01
      • 2018-11-13
      • 2020-07-23
      相关资源
      最近更新 更多