Spring引导oauth2管理httpbasic认证

时间:2016-03-14 18:04:52

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

我有一个使用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和管理端点工作的正确方法是什么?

4 个答案:

答案 0 :(得分:3)

问题在于@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

答案 1 :(得分:0)

application.yml中的security.oauth2.resource.filter-order = 3将会起到作用

答案 2 :(得分:-1)

使用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配置这样做)

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

(但是认为你不能通过普通的spring-boot配置来做到这一点。)

答案 3 :(得分:-1)

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

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

@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();
    }
}