为 Eureka 添加 Http Basic 认证
简介
在网络世界中,任何网络中的服务都是不安全的,为了使我们的 Eureka 服务更加安全,我们可以添加各种各样的认证方式,以使客户端在提供相应的证明之后才能够注册到 Eureka 中。而这次我们就添加一个最基本的 Http Basic 认证到 Eureka 中。 HTTP Basic 是简单的用户名密码认证,客户端在发送注册请求时,会附带用户名和密码一起发送到 Eureka Server,这种传输方式也属于不太安全的一种。
项目源码
配置 Eureka Server
打开远程 git 仓库中的 eureka-server.yml 文件,添加如下配置:
---
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-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);
@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 文件,添加如下配置:
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'
欢迎访问我的博客:http://zxuqian.cn/
为 Eureka 添加 Http Basic 认证的更多相关文章
- [Nginx]子目录反向代理kibana并添加basic认证
背景 服务器ip:192.168.1.2 安装软件 nginx kibana(默认端口5601) 实现方案:访问http://192.168.1.2/kibana 即可访问到kibana后端,同时需要 ...
- Basic认证时添加请求头
http Basic认证 http协议定义的一种认证方式,将客户端id和客户端密码按照"客户端ID:客户端密码"的格式拼接,并用base64编 码,放在header中请求服务端, ...
- iOS进行Basic认证与NTLM认证
一.iOS进行Basic认证 只需要在NSMutableURLRequest的Header中添加认证所需的Username和password. NSMutableURLRequest *webReq ...
- Burpsuite之Http Basic认证爆破
有的时候经常遇到401.今天正好朋友问怎么爆破,也顺便记录一下 怕忘记了 referer:http://www.2cto.com/Article/201303/194449.html 看到Burpsu ...
- 为Asp.Net Web Api添加Http基本认证
Asp.net Web Api提供了RESTFul web服务的编程接口.默认RESTFul 服务没有提供任何验证或者基于角色的验证,这显然不适合Put.Post.Delete这些操作.Aps.net ...
- squid添加用户名密码认证
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...
- angularjs+webapi2 跨域Basic 认证授权(二)
在上一篇中大概演示了 整个认证授权的过程.今天在这篇博客中将结合上一篇的例子继续在跨域的情况 我们用ionic 写一个简单的页面 值得注意的是 在ionic.bundle.js 里面集成了angula ...
- 【Http认证方式】——Basic认证
访问请求:http://192.168.2.113:8080/geoserver/rest/workspaces时,浏览器弹出窗口需要输入用户名和密码 ,并且,如果不输入或者输入错误,浏览器返回 ...
- eureka添加security验证之后,client注册失败
高版本,以下配置已弃用 security: basic: enabled: true 所以需要自定义security配置开启basic认证,参考我的配置类 @Configuration @Enable ...
随机推荐
- 给咱的服务器安装BBR脚本
yum -y install wget ##ContOS Yum 安装 wget apt-get install wget ##Debian Ubuntu 安装 wget 先给咱的服务器安装wget, ...
- DICOM 相关概念了解
前言: 正如前述文章中提到的,DICOM(Digitial Image Communications in Medicine)是所有从事医学影像处理的工作者需要了解的最基本的图像格式. 假 ...
- 记一次抓包和破解App接口
目录 第一章 · 起源 第二章 · 尝试 第三章 · 脱狱 第四章 · 柳暗花明 第五章 · 终结 第一章 · 起源 某日,想做个爬虫工具,爬某个网站上的数据已做实验之用.大家都知道爬pc网页上的数据 ...
- IDEA中项目的两种打包方式
本文主要介绍在IDEA中怎么打包,及可以用哪种方式打包. 若是有指正或补充的,欢迎留言~ ٩(●̮̃•)۶ 接下来进入正题: IDEA中打包需要先进行配置,so,我们先打开<abbr titl ...
- 安排上了!PC人脸识别登录,出乎意料的简单
本文收录在个人博客:www.chengxy-nds.top,技术资源共享. 之前不是做了个开源项目嘛,在做完GitHub登录后,想着再显得有逼格一点,说要再加个人脸识别登录,就我这佛系的开发进度,过了 ...
- 使用 you-get 下载免费电影或电视剧
安装 you-get 和 ffmpeg ffmpeg 主要是下载之后,合并音频和视频 pip install you-get -i http://pypi.douban.com/simple/ --t ...
- PHP sinh() 函数
实例 返回不同数的双曲正弦: <?phpecho(sinh(3) . "<br>");echo(sinh(-3) . "<br>" ...
- PHP vsprintf() 函数
实例 把格式化字符串写入变量中: <?php高佣联盟 www.cgewang.com$number = 9;$str = "Beijing";$txt = vsprintf( ...
- MediaDevices对象
mediaDevices 是 Navigator对象的一个 只读属性,返回一个 MediaDevices 对象,该对象可提供对相机和麦克风等媒体输入设备的连接访问,也包括屏幕共享. 语法 const ...
- 账本APP服务器端开发
账本APP开发 好好学习,天天向上 本文已收录至我的Github仓库DayDayUP:github.com/RobodLee/DayDayUP,欢迎Star,更多文章请前往:目录导航 上一篇文章我们聊 ...