【SpringCloud】Eureka上添加认证

【SpringCloud】Eureka上添加认证


创建一个eureka工程

【SpringCloud】Eureka上添加认证_第1张图片


具体实现


pom.xml



  4.0.0
  com.itmuch.cloud
  microservice-discovery-eureka-authenticating
  0.0.1-SNAPSHOT
  jar

  
  
    org.springframework.boot
    spring-boot-starter-parent
    1.4.3.RELEASE
  

  
    UTF-8
    1.8
  

  
    
      org.springframework.cloud
      spring-cloud-starter-eureka-server
    
    
      org.springframework.boot
      spring-boot-starter-security
    
  

  
  
    
      
        org.springframework.cloud
        spring-cloud-dependencies
        Camden.SR4
        pom
        import
      
    
  

  
  
    
      
        org.springframework.boot
        spring-boot-maven-plugin
      
    
  


EurekaApplication

package com.itmuch.cloud.study;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * 使用Eureka做服务发现.
 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
  public static void main(String[] args) {
    SpringApplication.run(EurekaApplication.class, args);
  }
}

application.yml

security:
  basic:
    enabled: true               # 开启基于HTTP basic的认证
  user:
    name: user                  # 配置登录的账号是user
    password: password123       # 配置登录的密码是password123
server:
  port: 8761                    # 指定该Eureka实例的端口
eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

# 参考文档:http://projects.spring.io/spring-cloud/docs/1.0.3/spring-cloud.html#_standalone_mode
# 参考文档:http://my.oschina.net/buwei/blog/618756

访问测试

http://localhost:8761/

需要输入用户名和密码;

【SpringCloud】Eureka上添加认证_第2张图片

【SpringCloud】Eureka上添加认证_第3张图片


服务提供者修改


修改配置文件-application.yml


eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
改成
eureka:
  client:
    serviceUrl:
      defaultZone: http://user:password123@localhost:8761/eureka/
  instance:
    prefer-ip-address: true

启动工程

1)microservice-discovery-eureka

2)microservice-provider-user


访问测试

http://localhost:8761/

【SpringCloud】Eureka上添加认证_第4张图片

http://localhost:8000/1

【SpringCloud】Eureka上添加认证_第5张图片

修改后启动访问还是和之前一样;


你可能感兴趣的:(【微服务】【Spring,Cloud】)