【问题标题】:Spring boot oauth2 management httpbasic authenticationspring boot oauth2管理httpbasic认证
【发布时间】:2016-06-29 22:05:54
【问题描述】:

我有一个使用 oauth2 进行身份验证的 Spring Boot 应用程序。 oauth2 机制正在运行,客户端可以进行身份​​验证并接收他们的访问令牌。

我想通过 httpbasic 身份验证来保护执行器端点,即不要求用户首先使用 oauth2 进行身份验证,然后访问执行器端点。 到目前为止我所做的是在属性文件中设置以下内容:

management.context-path=/admin/actuators
management.security.enabled=true
management.security.role=ADMIN

security.user.name=admin
security.user.password=password

我尝试了各种方法来使用 ResourceServerConfigurerAdapter 和 WebSecurityConfigurerAdapter 设置配置。

我的尝试都没有奏效,它一直在告诉我

<oauth>
<error_description>
Full authentication is required to access this resource
</error_description>
<error>unauthorized</error>
</oauth>

让 OAUTH2 和管理端点工作的正确方法是什么?

【问题讨论】:

    标签: java spring security spring-boot spring-security-oauth2


    【解决方案1】:

    application.yml 中的security.oauth2.resource.filter-order = 3 可以解决问题

    【讨论】:

      【解决方案2】:

      问题在于@EnableResourceServer 导入了ResourceServerConfiguration,其阶数为3,远远优于ManagementServerProperties.ACCESS_OVERRIDE_ORDER
      请参阅有关执行器安全性和排序配置类的 Spring Boot 文档:http://docs.spring.io/spring-boot/docs/1.4.3.RELEASE/reference/htmlsingle/#boot-features-security-actuator

      默认执行器安全配置比仅允许访问 /health 端点并阻止其余部分要聪明得多,它实际上会根据 management.portmanagement.contextPath 而变化,而且很难找到正确的管理端点 URL,不会在您的安全性中留下巨大的漏洞或弄乱您自己的资源。

      如果你想保持自动配置管理安全的好处,有两个选择:

      编辑:a) 使用 BeanPostProcessor 降低 ResourceServerConfiguration 顺序

      @dsyer 在 github 线程上提出了这项改进:

      @Component
      @Slf4j
      public class ResourceServerConfigurationPostProcessor implements BeanPostProcessor {
      
          @Override
          public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
              if (bean instanceof ResourceServerConfiguration) {
                  LOGGER.debug("Lowering order of ResourceServerConfiguration bean : {}", beanName);
                  ResourceServerConfiguration config = (ResourceServerConfiguration) bean;
                  config.setOrder(SecurityProperties.ACCESS_OVERRIDE_ORDER);
              }
              return bean;
          }
      
          @Override
          public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
              return bean;
          }
      
      }
      

      我刚刚用这个类替换了下面的代码,它运行良好。


      编辑:b) 手动覆盖 ResourceServerConfiguration 顺序

      如果你因为某种原因不喜欢后处理器,你可以用另一个配置类替换@EnableResourceServer,它的顺序将在默认管理安全之后:

      /** 
       * Extend the default resource server config class, and downgrade its order
       */
      public class ResourceServerLowPrecedenceConfiguration extends ResourceServerConfiguration {
      
           /**
           * This is enough to override Spring Boot's default resource security,
           * but it does not takes over the management.
           */
          @Override
          public int getOrder() {
              return SecurityProperties.ACCESS_OVERRIDE_ORDER;
          }
      }
      

      还有你自己的配置类:

      /** @EnableResourceServer is replaced by @Import using the low precedence config */
      @Configuration
      @Import(ResourceServerLowPrecedenceConfiguration.class)
      public class YourOwnOAuth2Config extends ResourceServerConfigurerAdapter {
          @Override
          public void configure(final HttpSecurity http) throws Exception {
              // Secure your resources using OAuth 2.0 here
          }
      }
      

      编辑:您还可以重写自己的 @EnableResourceServer 注释以快捷方式 @Import

      @Target(ElementType.TYPE)
      @Retention(RetentionPolicy.RUNTIME)
      @Documented
      @Import(ResourceServerLowPrecedenceConfiguration.class)
      public @interface EnableResourceServer {
      }
      

      恕我直言,当 spring-security-oauth 在类路径上时,这应该是默认行为。
      请参阅关于 GitHub 问题的讨论: https://github.com/spring-projects/spring-boot/issues/5072

      【讨论】:

        【解决方案3】:

        好的,使用下面的 java 配置让它工作。

        端点 /admin/actuators/health 可供任何人访问,并且所有其他 /admin/actuators/* 端点都经过身份验证。

        @Configuration
        @Order(1)
        protected static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
            protected void configure(HttpSecurity http) throws Exception {
        
                http
                        .authorizeRequests()
                        .antMatchers(HttpMethod.GET, "/admin/actuators/health").permitAll()
                    .and()
                        .antMatcher("/admin/actuators/**")
                        .authorizeRequests()
                        .anyRequest()
                        .hasRole("ADMIN")
                        .and()
                        .httpBasic();
            }
        }
        

        【讨论】:

        • 这个解决方案很脆弱,因为执行器 contextPath 可以通过配置属性进行更改。此外,默认情况下,此设置已由框架以更健壮的方式完成。看我的回答。
        【解决方案4】:

        使用 Spring-Security,您可以拥有 Multiple HttpSecurity 配置。

        <http pattern="/actuators/**/*" request-matcher="ant" authentication-manager-ref="basicAuthManager">
            <security:intercept-url pattern="/**" access="isAuthenticated()" />
            <http-basic />
        <http>
        <http use-expressions="false">
           ... your oauth config
        </http>
        
        <authentication-manager id="basicAuthManager">
            <authentication-provider>
                <user-service>
                    <user name="user1" password="user1Pass" authorities="ROLE_USER" />
                </user-service>
            </authentication-provider>
        </authentication-manager>
        
        ... your oath config stuff
        

        (我更喜欢 xml,但你也可以使用 java config 来做到这一点)

        @见http://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#nsa-http

        (但认为您无法通过简单的 spring-boot 配置来做到这一点。)

        【讨论】:

        • 谢谢,你有java config的样本吗?
        • 你访问过我发布的第一个链接(指向 Spring 安全参考的那个),它包含一些 java doc 配置
        • 哦,我看到你找到了,然后发布你自己的答案
        • 这个解决方案很脆弱,原因与操作相同
        • 我不会因为偏好而对答案投反对票。主要问题是,如果您不为管理端点使用特定的 contextPath,此解决方案会变得很危险。此外,它通过替换已经由框架配置的身份验证管理器增加了复杂性。 user1user1Pass 在此处不可配置,而 security.user.namesecurity.user.password 是有据可查的配置属性。
        猜你喜欢
        • 2021-06-22
        • 2020-03-25
        • 2018-09-08
        • 2021-10-08
        • 2018-09-04
        • 2012-05-01
        • 1970-01-01
        • 2017-02-12
        • 2016-05-10
        相关资源
        最近更新 更多