eureka添加security验证之后,client注册失败

高版本,以下配置已弃用

security:
  basic:
    enabled: true

所以需要自定义security配置开启basic认证,参考我的配置类

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
        http.csrf().disable();
        //注意:为了可以使用 http://${user}:${password}@${host}:${port}/eureka/ 这种方式登录,所以必须是httpBasic,如果是form方式,不能使用url格式登录
        http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication().passwordEncoder(new SystemPasswordEncoder())
            //admin
            .withUser("admin").password("123456").roles("EUREKA-CLIENT").and()
            //eureka-security-client
            .withUser("eureka-security-client").password("eureka-security-client").roles("EUREKA-CLIENT")
        ;
    }
}
posted @ 2019-08-07 09:25  星朝  阅读(445)  评论(0编辑  收藏  举报