前言
Github:https://github.com/yihonglei/thinking-in-springcloud
Eureka安全认证注册中心:eureka-server-security
客户端通过安全认证方式进行服务注册:eureka-provider-order
一 eureka安全认证
eureka安全中心可以增加用户名和密码进行安全验证访问。
通过spring-boot-starter-security模块实现。
二 eureka-server-security
1、项目结构
2、application.properties配置
spring.security.user.name=root
spring.security.user.password=123456
# 服务名称和端口
spring.application.name=eureka-server-security
server.port=9000
# 定义Instance ID 的hostname
eureka.instance.hostname=localhost
# 自我注册禁用(在默认情况下,服务注册中心也会将自己作为客户端来尝试注册它自己,设置为false)
eureka.client.register-with-eureka=false
# 是否检索服务(由于注册中心的职责就是维护服务实例,所以就不需要去检索服务,设置为false)
eureka.client.fetch-registry=false
# 安全认证,客户端注册也需要用户名和密码
eureka.client.service-url.defaultZone=http://${spring.security.user.name}:${spring.security.user.password}@localhost:9000/eureka/
# 日志文件
# logging.file=${spring.application.name}.log
# 是否开启注册中心自我保护机制(true开,false关,默认为开)
eureka.server.enable-self-preservation=true
通过eureka.client.service-url.defaultZone
=http://${spring.security.user.name}:${spring.security.user.password}@localhost:9000/eureka/
设置认证的用户名和密码。
3、WebSecurityConfig
关闭csrf,开启认证。
package com.jpeony.eureka.config;
import org.springframework.context.annotation.Configuration;
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;
/**
* 安全认证
*
* @author yihonglei
*/
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
// 关闭csrf
http.csrf().disable();
// 开启认证
http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
}
}
4、EurekaServerApplication
应用启动。
package com.jpeony.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* -- @SpringBootApplication 启动一个Spring Boot应用程序
* -- @EnableEurekaServer 注解启动一个服务注册中心提供给其他应用进行会话
*
* @author yihonglei
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
5、启动访问
http://localhost:9000回车,然后就进入认证登录页面,输入用户名和密码访问。
三 eureka-provider-order
客户端安全认证注册需要指定用户名和密码,源码参考github。
application.properties配置。
spring.security.user.name=root
spring.security.user.password=123456
# 注册到eureka服务端的微服务名称
spring.application.name=eureka-provider-order
# 服务提供端口
server.port=8001
# 注册到eureka服务端的地址
#eureka.client.service-url.defaultZone=http://localhost:9000/eureka/
# 安全认证注册地址,eureka服务端设置了用户名和密码,客户端注册时需要设置对应的用户名和密码
eureka.client.service-url.defaultZone=http://${spring.security.user.name}:${spring.security.user.password}@localhost:9000/eureka/
# 显示指定微服务的名称,默认ip:应用名称:端口(192.168.1.7:eureka-provider-order:8001)
eureka.instance.instance-id=eureka-provider-order-8001
eureka.instance.prefer-ip-address=true
访问注册中心,可以看到服务已经注册成功。
四 总结
1、如果在eureka服务端增加上安全认证,客户端无法注册成功,先看看有没有WebSecurityConfig。
2、客户端也需要用户名和密码认证注册的,服务端改成安全认证,客户端不要忘了改。
3、如果服务端是安全认证的集群服务,客户端注册时每个地址都需要用户名和密码安全认证。