为 Eureka 添加 Http Basic 认证

简介

在网络世界中,任何网络中的服务都是不安全的,为了使咱们的 Eureka 服务更加安全,咱们能够添加各类各样的认证方式,以使客户端在提供相应的证实以后才可以注册到 Eureka 中。而此次咱们就添加一个最基本的 Http Basic 认证到 Eureka 中。 HTTP Basic 是简单的用户名密码认证,客户端在发送注册请求时,会附带用户名和密码一块儿发送到 Eureka Server,这种传输方式也属于不太安全的一种。java

项目源码

Gitee码云git

配置 Eureka Server

打开远程 git 仓库中的 eureka-server.yml 文件,添加以下配置:web

---
spring:
  profiles: peer1
  security:
    user:
      name: test
      password: 123456
      roles: USER
server:
  port: 8761
eureka: 
  instance:
    hostname: peer1
  client: 
    register-with-eureka: false
    fetch-registry: false
    # serviceUrl:
    #   defaultZone: http://peer2:8762/eureka

---

为了简化服务注册,咱们此次测试只使用 peer1 这个 profile,而且把 register-with-eurekafetch-registry 设置为了 false 以关闭自身注册。而后咱们在 spring 下配置了 security.user.namepassword, roles,分别用来指定能够登陆的用户名,密码,和用户组。spring

在咱们的 registry 项目中建立一个 Java 类 cn.zxuqian.configurations.WebSecurityConfig,并添加以下代码:安全

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    private static Logger log = LoggerFactory.getLogger(WebSecurityConfig.class);

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().httpBasic();
    }


    @Bean
    public UserDetailsService userDetailsService() {
        User.UserBuilder builder = User.withDefaultPasswordEncoder();
        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
        manager.createUser(builder.username("test").password("123456").roles("USER").build());
        return manager;
    }

}

这里覆盖了 WebSecurityConfigurerAdapter 中的 configure() 方法,用来停用 CSRF 保护,由于咱们的 Eureka Server 使用了 peer 作为 hostname,而稍后测试的 product-service 使用了 localhost,会被禁止访问 Eureka 资源。而后在 userDetailsService() 方法中添加了一个 test 用户用于认证。网络

Product-service

打开远程 git 仓库中的 product-service.yml 文件,添加以下配置:curl

eureka:
  client:
    serviceUrl:
      defaultZone: http://test:123456@peer1:8761/eureka/

这里在 defaultZone 指定的 Url 中添加了 [username]:[password]@host:port/eureka/ 形式的地址,此为 curl 发送用户名和密码的方式。ide

测试

首先运行 Config Server,而后使用 mvn spring-boot:run -Dspring-boot.run.profiles=peer1 运行 Eureka Server,最后运行 product-service,稍等片刻就会看到 product-service 注册成功,而 Eureka Server 的 log 中会有以下字样(需设置 log level 为 debug):spring-boot

2018-05-19 18:16:45.278 DEBUG 19055 --- [nio-8761-exec-9] w.c.HttpSessionSecurityContextRepository : Obtained a valid SecurityContext from SPRING_SECURITY_CONTEXT: 'org.springframework.security.core.context.SecurityContextImpl@442bd3dc: Authentication: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@442bd3dc: Principal: org.springframework.security.core.userdetails.User@364492: Username: test; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_USER; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@957e: RemoteIpAddress: 127.0.0.1; SessionId: null; Granted Authorities: ROLE_USER'

欢迎访问个人博客:http://zxuqian.cn/测试

相关文章
相关标签/搜索