之前用的Spring Cloud 是2.0以上的,开启eureka登录验证后,eureka客户端都能正常注册到eureka服务上
今天,升级Spring Cloud后,eureka客户端无法登录上。
查看日志:
原因分析
查资料了解到新版(Spring Cloud 2.0 以上)的security默认启用了csrf检验,要在eurekaServer端配置security的csrf检验为false
解决方法
- 添加一个继承 WebSecurityConfigurerAdapter 的类;
- 在类上添加 @EnableWebSecurity 注解;
- 覆盖父类的 configure(HttpSecurity http) 方法,关闭掉 csrf
示例代码
import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.w