Spring Cloud (18) | 给Eureka Server加上安全验证

来源:互联网 发布:设计公司logo软件 编辑:程序博客网 时间:2024/06/16 11:02

启动Spring Cloud Eureka Server项目工程的时候,直接输入localhost:8761,就可以看到所有注册服务,这样在生产环境是极不安全,如何解决这个问题呢?
首先,在eureka server项目中pom.xml文件中加入依赖:

        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-security</artifactId>        </dependency>

然后,在application.yml中加入安全验证,用户名和密码:
用户名:jack.ma
密码:1qaz2wsx

server:    port: 8761 # 安全认证的配置  security:    basic:      enabled: true    user:      name: jack.ma  # 用户名      password: 1qaz2wsx   # 用户密码  eureka:    client:      register-with-eureka: false      fetch-registry: false      service-url:        defaultZone: http://${security.user.name}:${security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/  # 安全的注册地址 

最后,在浏览器中输入:http://localhost:8761, 就会弹出登录验证窗口:
这里写图片描述
输入用户名和密码,回车之后,就可以看到服务注册页面!

注册到Eureka Server的微服务中的defaultZone要改成如下:

eureka:  client:    serviceUrl:      defaultZone: http://jack.ma:1qaz2wsx@localhost:8761/eureka/