eclipse搭建springclooud配置Eureka中心配置密码错误的问题

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/

完成

在次运行发现输入的账号密码就是你在配置文件的密码!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值