简介
在网络世界中,任何网络中的服务都是不安全的,为了使我们的 Eureka 服务更加安全,我们可以添加各种各样的认证方式,以使客户端在提供相应的证明之后才能够注册到 Eureka 中。而这次我们就添加一个最基本的 Http Basic 认证到 Eureka 中。 HTTP Basic 是简单的用户名密码认证,客户端在发送注册请求时,会附带用户名和密码一起发送到 Eureka Server,这种传输方式也属于不太安全的一种。
项目源码
配置 Eureka Server
打开远程 git 仓库中的 eureka-server.yml
文件,添加如下配置:
---
spring:profiles: peer1security:user:name: testpassword: 123456roles: USER
server:port: 8761
eureka: instance:hostname: peer1client: register-with-eureka: falsefetch-registry: false# serviceUrl:# defaultZone: http://peer2:8762/eureka
---
为了简化服务注册,我们这次测试只使用 peer1 这个 profile,并且把 register-with-eureka
和 fetch-registry
设置为了 false 以关闭自身注册。然后我们在 spring
下配置了 security.user.name
,password
, roles
,分别用来指定可以登录的用户名,密码,和用户组。
在我们的 registry 项目中创建一个 Java 类 cn.zxuqian.configurations.WebSecurityConfig
,并添加如下代码:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {private static Logger log = LoggerFactory.getLogger(WebSecurityConfig.class);@Overrideprotected void configure(HttpSecurity http) throws Exception {http.csrf().disable().httpBasic();}@Beanpublic 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
文件,添加如下配置:
eureka:client:serviceUrl:defaultZone: http://test:123456@peer1:8761/eureka/
这里在 defaultZone
指定的 Url 中添加了 [username]:[password]@host:port/eureka/
形式的地址,此为 curl 发送用户名和密码的方式。
测试
首先运行 Config Server,然后使用 mvn spring-boot:run -Dspring-boot.run.profiles=peer1
运行 Eureka Server,最后运行 product-service
,稍等片刻就会看到 product-service
注册成功,而 Eureka Server
的 log 中会有如下字样(需设置 log level 为 debug):
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'
欢迎访问我的博客:zxuqian.cn/# 学习计划安排
我一共划分了六个阶段,但并不是说你得学完全部才能上手工作,对于一些初级岗位,学到第三四个阶段就足矣~
这里我整合并且整理成了一份【282G】的网络安全从零基础入门到进阶资料包,需要的小伙伴可以扫描下方优快云官方合作二维码免费领取哦,无偿分享!!!
如果你对网络安全入门感兴趣,那么你需要的话可以
点击这里👉网络安全重磅福利:入门&进阶全套282G学习资源包免费分享!
①网络安全学习路线
②上百份渗透测试电子书
③安全攻防357页笔记
④50份安全攻防面试指南
⑤安全红队渗透工具包
⑥HW护网行动经验总结
⑦100个漏洞实战案例
⑧安全大厂内部视频资源
⑨历年CTF夺旗赛题解析
