簡體   English   中英

Spring引導oauth2管理httpbasic認證

[英]Spring boot oauth2 management httpbasic authentication

我有一個使用oauth2進行身份驗證的spring啟動應用程序。 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和管理端點工作的正確方法是什么?

問題是@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

application.yml中的security.oauth2.resource.filter-order = 3可以解決這個問題

使用Spring-Security,您可以擁有多個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配置這樣做)

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

(但是認為你不能通過普通的spring-boot配置來做到這一點。)

好的,使用以下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();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM