eclipse搭建springclooud配置Eureka中心配置密码错误的问题
问题描述
安装网上的教程,搭建了一个 注册中心Eureka,以及一个服务,服务注册到eureka上去。一切正常。
按照网上的教程
//pom文件添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>
// eureka的配置文件application.yml
security:
basic:
enabled: true
user:
name: admin
password: 123456
运行项目是可以的。是提示输入密码:输入 admin/123456,一直提示错误的。
问题发现
我使用的springboot是 2.X 版本的
Spring Cloud 2.0 以上的security默认启用了csrf检验,要在eureka server端配置security的csrf检验为false。
解决办法
step1 取消csrf校验
// eureka的配置文件application.yml
package eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
@EnableWebSecurity
static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http.csrf().disable();
}
}
}
step2 修改application.yml文件
'添加spring,以及修改defaultZone'
server:
port: 8761
spring:
security:
basic:
enabled: true
user:
name: admin
password: 123456
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false #false表示不向注册中心注册自己。
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/
完成
在次运行发现输入的账号密码就是你在配置文件的密码!