spring-cloud-config 使用安全认证后 刷新配置端点 /actuator/bus-refresh 返回403

spring-cloud-config 开启了spring-security后,导致自动刷新配置接口异常:
开始报

POST http://localhost:8888/actuator/bus-refresh
{
    "timestamp": "2018-07-01T11:48:52.417+0000",
    "status": 401,
    "error": "Unauthorized",
    "message": "Unauthorized",
    "path": "/actuator/bus-refresh"
}

然后我就加了Basic Auth,使用的是POSTMAN
加上正确的帐号密码后,报以下错误:

{
    "timestamp": "2018-07-01T11:48:52.417+0000",
    "status": 403,
    "error": "Forbidden",
    "message": "Forbidden",
    "path": "/actuator/bus-refresh"
}

下面说下解决方案:
首先我采用的是spring-cloud 2.0.3版本,
这里写图片描述
看了官网的描述,才知道,spring5.0以后,以及把spring.security.basic.enabled给去掉了,
所以我们推荐我们去创建一个自己的WebSecurityConfig类去实现WebSecurityConfigAdapter。
上代码:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    /**
     * 高版本的丢弃了 
     * 
     * security: 
     *   basic: 
     *    enabled: true 
     * 
     * 配置,应该使用以下方式开启
     *
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // Configure HttpSecurity as needed (e.g. enable http basic).
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
        http.csrf().disable();
        //注意:为了可以使用 http://${user}:${password}@${host}:${port}/eureka/ 这种方式登录,所以必须是httpBasic,
        // 如果是form方式,不能使用url格式登录
        http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
    }
}

然后再试刷新POST /actuator/bus-refresh ,刷新成功!
开始借鉴了其他同行的错误问题,得以解决思路,附上问题链接:

spring cloud config 使用用户认证后 刷新配置端点bus-refresh 403
https://www.oschina.net/question/3877992_2281549

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章