目录
一、 为EurekaServer配置Httpbasic验证
为了保证服务的安全性,我们为EurekaServer配置Httpbasic验证,只有知道username和password的服务示例才能注册到EurekaServer。那接下来我们修改一下eureka_server项目,配置httpbasic验证,然后为product_server和consume_server配置eureka_server的username和password。
1. 引入spring-boot-starter-security依赖
org.springframework.boot
spring-boot-starter-security
2. 配置security
首先需要禁用csrf(cross site request forgery),当spring security在classpath路径下,它将要求每个客户端请求带上csrf token,eureka客户端通常不会拥有一个有效的csrf token,我们需要在配置中禁用对/eureka/**这个端点进行csrf验证。然后我们还需要开启httpbasic验证。此时我们便可以通过url中配置username和password,去验证客户端的可靠性。为了简单起见,我们在启动类中配置。
package com.yshmsoft;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
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;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
@EnableWebSecurity
static class WebSecurityConfigure extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// 在/eureka/**端点忽略csrf验证
http.csrf().ignoringAntMatchers(