(四)Eureka Server用户认证(双节点为例)
程序员文章站
2024-03-23 20:18:52
...
1.添加依赖
在microservice-discovery-eureka的pom.xml中添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2.application.xml修改
1.在microservice-discovery-eureka的application.xml中添加账户名和密码:
security:
basic:
enabled: true # 开启基于HTTP basic的认证
user:
name: user
password: password # 配置登录账号和密码
2.修改 microservice-discovery-eureka的application.yml中的eureka.client.serviceUrl.defaultZone为以下形式http://user:aaa@qq.com_HOST:EUREKA_PORT/eureka/
3.application.yml如下:
spring:
application:
name: microservice-discovery-eureka
# 用户认证配置
security:
basic:
enabled: true
user:
name: user
password: password
---
spring:
profiles: peer1
server:
port: 8761
eureka:
instance:
# 指定profile=peer1时,主机名是peer1
hostname: peer1
client:
service-url:
# defaultZone: http://localhost:8762/eureka/
defaultZone: http://user:aaa@qq.com:8762/eureka/
---
spring:
profiles: peer2
server:
port: 8762
eureka:
instance:
# 指定profile=peer2时,主机名是peer2
hostname: peer2
client:
service-url:
# defaultZone: http://localhost:8761/eureka/
defaultZone: http://user:aaa@qq.com:8761/eureka/
3.测试
启动 microservice-discovery-eureka,访问http://localhost:8761需要身份验证,输入用户名和密码即可登录(user/password)
4.微服务注册到需要认证的Eureka Server
将microservice-provider-user的application.xml中的defaultZone修改为以下形式
http://user:aaa@qq.com_HOST:EUREKA_PORT/eureka/
application.xml文件如下:
server:
port: 8000
spring:
application:
name: microservice-provider-user
jpa:
generate-ddl: false
show-sql: true
hibernate:
ddl-auto: none
datasource: # 指定数据源
platform: h2 # 指定数据源类型
schema: classpath:schema.sql # 指定h2数据库的建表脚本
data: classpath:data.sql # 指定h2数据库的数据脚本
logging: # 配置日志级别,让hibernate打印出执行的SQL
level:
root: INFO
org.hibernate: INFO
org.hibernate.type.descriptor.sql.BasicBinder: TRACE
org.hibernate.type.descriptor.sql.BasicExtractor: TRACE
eureka:
client:
serviceUrl:
defaultZone:http://user:aaa@qq.com:8761/eureka/,http://user:aaa@qq.com:8762/eureka/
instance:
prefer-ip-address: true
management:
security:
enabled: false
info:
app:
name: @aaa@qq.com
encoding: @aaa@qq.com
java:
source: @aaa@qq.com
target: @aaa@qq.com
5.访问登录即可
上一篇: Mac上php环境配置