(四)Eureka Server用户认证(双节点为例)

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:password@EUREKA_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:password@localhost: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:password@localhost:8761/eureka/

 

3.测试

启动 microservice-discovery-eureka,访问http://localhost:8761需要身份验证,输入用户名和密码即可登录(user/password

4.微服务注册到需要认证的Eureka Server

 将microservice-provider-user的application.xml中的defaultZone修改为以下形式

http://user:password@EUREKA_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:password@localhost:8761/eureka/,http://user:password@localhost:8762/eureka/
  instance:
    prefer-ip-address: true

management:
  security:
    enabled: false

info:
  app:
    name: @project.artifactId@
    encoding: @project.build.sourceEncoding@
    java:
      source: @java.version@
      target: @java.version@

5.访问登录即可

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值