简介
在网络世界中,任何网络中的服务都是不安全的,为了使我们的 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'
网络安全成长路线图
这个方向初期比较容易入门一些,掌握一些基本技术,拿起各种现成的工具就可以开黑了。不过,要想从脚本小子变成hei客大神,这个方向越往后,需要学习和掌握的东西就会越来越多,以下是学习网络安全需要走的方向:
![](https://img-blog.csdnimg.cn/img_convert/674f60a9d162bdd3d54af3ab6dd21ad3.png)
![](https://img-blog.csdnimg.cn/img_convert/1283160785b844277cbd0777e262a87a.png)
# 网络安全学习方法
上面介绍了技术分类和学习路线,这里来谈一下学习方法:
## 视频学习
无论你是去B站或者是油管上面都有很多网络安全的相关视频可以学习,当然如果你还不知道选择那套学习,我这里也整理了一套和上述成长路线图挂钩的视频教程,完整版的视频已经上传至CSDN官方,朋友们如果需要可以点击这个链接免费领取。网络安全重磅福利:入门&进阶全套282G学习资源包免费分享!