spring boot:用spring security加强spring boot admin的安全(spring boot admin 2.3.0 / spring boot 2.3.3)
一,spring boot admin的安全环节:
1,修改context-path,默认时首页就是admin,
我们修改这个地址可以更安全
2,配置ip地址白名单,有ip限制才安全,
我们使用了spring security,
可以在防火墙中也配置上ip限制
3,登录用户有相应的role授权才能访问
4,actuator端也要配置ip/路径/权限
说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,演示项目的相关信息
1,项目地址:
https://github.com/liuhongdi/bootadmin
2,项目功能说明:
演示了spring boot admin 服务端和actuator客户端的安全配置
3,项目结构;如图:


三,配置文件说明
1,admin模块的pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--admin sever-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.3.0</version>
</dependency>
<!-- spring security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2,actuator模块的pom.xml
<!--admin client-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.3.0</version>
</dependency>
<!--actuator begin-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- spring security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
3,admin模块的application.properties
#admin context-path
spring.boot.admin.context-path=/lhdadmin
#admin white ip list
spring.boot.admin.access.iplist=192.168.3.1,127.0.0.1
#admin status intertal
spring.boot.admin.monitor.status-interval=60000ms
spring.boot.admin.monitor.status-lifetime=60000ms #error
server.error.include-stacktrace=always
#error
logging.level.org.springframework.web=trace
4,actuator模块的application.properties
#admin url
spring.boot.admin.client.url=http://localhost:8080/lhdadmin
spring.boot.admin.client.username=lhdadmin
spring.boot.admin.client.password=123456
spring.boot.admin.client.connect-timeout=5000ms
spring.boot.admin.client.period=60000ms
spring.boot.admin.client.instance.metadata.user.name=lhdadmin
spring.boot.admin.client.instance.metadata.user.password=123456
#port
server.port=8081
#exposure
management.endpoints.web.exposure.include=*
#路径映射
management.endpoints.web.base-path=/lhdmon
#health显示
management.endpoint.health.show-details=always
#允许访问的ip列表
management.access.iplist = 127.0.0.1,192.168.1.100,192.168.2.3/24,192.168.1.6,localhost
#error
server.error.include-stacktrace=always
#error
logging.level.org.springframework.web=trace
说明:
spring.boot.admin.client.username=lhdadmin
spring.boot.admin.client.password=123456
这两项是用来访问server的账号
spring.boot.admin.client.instance.metadata.user.name=lhdadmin
spring.boot.admin.client.instance.metadata.user.password=123456
这两项是供server访问actuator时使用
spring.boot.admin.client.url=http://localhost:8080/lhdadmin
此处注意使用服务端设置的context-path
四,java代码说明
1,admin模块的application:DemoApplication.java
@SpringBootApplication
@EnableAdminServer
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
注意添加了@EnableAdminServer注解,用来启动admin sever
2,admin模块的SecurityConfig.java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter { @Value("${spring.boot.admin.access.iplist}")
private String iplist; @Override
protected void configure(HttpSecurity http) throws Exception { //得到iplist列表
String iprule = "";
//hasIpAddress('10.0.0.0/16') or hasIpAddress('127.0.0.1/32')
String[] splitAddress=iplist.split(",");
for(String ip : splitAddress){
if (iprule.equals("")) {
iprule = "hasIpAddress('"+ip+"')";
} else {
iprule += " or hasIpAddress('"+ip+"')";
}
}
String adminRule = "hasAnyRole('ADMIN','DEV') and ("+iprule+")";
//login和logout
http.formLogin()
.loginPage("/lhdadmin/login")
.defaultSuccessUrl("/lhdadmin/wallboard")
.failureUrl("/login-error.html")
.permitAll()
.and()
.logout().logoutUrl("/lhdadmin/logout").permitAll()
.and()
.httpBasic(); http.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.ignoringAntMatchers(
"/lhdadmin/**",
"/actuator/**"
);
//匹配的页面,符合限制才可访问
http.authorizeRequests()
.antMatchers("/lhdadmin/login/**","/lhdadmin/assets/**").access(iprule)
.antMatchers("/lhdadmin/**").access(adminRule);
//剩下的页面,允许访问
http.authorizeRequests().anyRequest().permitAll();
} @Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
//添加两个账号用来做测试
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("lhdadmin")
.password(new BCryptPasswordEncoder().encode("123456"))
.roles("ADMIN","USER")
.and()
.withUser("lhduser")
.password(new BCryptPasswordEncoder().encode("123456"))
.roles("USER");
}
}
3,actuator模块的SecurityConfig.java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter { @Value("${management.access.iplist}")
private String iplist; @Override
protected void configure(HttpSecurity http) throws Exception { //得到iplist列表
String iprule = "";
String[] splitAddress=iplist.split(",");
for(String ip : splitAddress){
if (iprule.equals("")) {
iprule = "hasIpAddress('"+ip+"')";
} else {
iprule += " or hasIpAddress('"+ip+"')";
}
}
String actuatorRule = "hasAnyRole('ADMIN','DEV') and ("+iprule+")"; //login和logout
http.formLogin()
.defaultSuccessUrl("/lhdmon")
.failureUrl("/login-error.html")
.permitAll()
.and()
.logout()
.and()
.httpBasic();
//匹配的页面,符合限制才可访问
http.authorizeRequests()
.antMatchers("/lhdmon/**").access(actuatorRule);
//剩下的页面,允许访问
http.authorizeRequests().anyRequest().permitAll();
} @Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
//添加两个账号用来做测试
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("lhdadmin")
.password(new BCryptPasswordEncoder().encode("123456"))
.roles("ADMIN","USER")
.and()
.withUser("lhduser")
.password(new BCryptPasswordEncoder().encode("123456"))
.roles("USER");
}
}
五,测试效果
1,spring boot admin 非授权ip地址访问
http://192.168.3.182:8080/lhdadmin/wallboard
登录后返回:

2,spring boot admin 非授权账号访问
http://127.0.0.1:8080/lhdadmin/login
页面:用lhduser登录

lhduser这个账号无权访问

3,spring boot admin从授权ip用有授权账号登录:
http://127.0.0.1:8080/lhdadmin/login
用lhdadmin这个账号登录:

跳转到了wallboard

点击进入:

六,查看spring boot的版本
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.3.RELEASE)
spring boot:用spring security加强spring boot admin的安全(spring boot admin 2.3.0 / spring boot 2.3.3)的更多相关文章
- 曹工说Spring Boot源码系列开讲了(1)-- Bean Definition到底是什么,附spring思维导图分享
写在前面的话&&About me 网上写spring的文章多如牛毛,为什么还要写呢,因为,很简单,那是人家写的:网上都鼓励你不要造轮子,为什么你还要造呢,因为,那不是你造的. 我不是要 ...
- 基于spring boot2.0+spring security +oauth2.0+ jwt微服务架构
github地址:https://github.com/hankuikuide/microservice-spring-security-oauth2 项目介绍 该项目是一个演示项目,主要演示了,基于 ...
- 0.spring cloud目录
1. 微服务架构概述 1.0. 单体架构是什么 1.1. 单体应用架构存在的问题 1.2. 如何解决单体应用架构存在的问题 1.3. 什么是微服务 1.4. 微服务架构的优点与挑战 1.4.1. 微服 ...
- spring boot系列03--spring security (基于数据库)登录和权限控制(下)
(接上篇) 后台 先说一下AuthConfig.java Spring Security的主要配置文件之一 AuthConfig 1 @Configuration 2 @EnableWebSecuri ...
- spring boot:swagger3的安全配置(swagger 3.0.0 / spring security / spring boot 2.3.3)
一,swagger有哪些环节需要注意安全? 1,生产环境中,要关闭swagger application.properties中配置: springfox.documentation.swagger- ...
- spring boot系列03--spring security (基于数据库)登录和权限控制(上)
这篇打算写一下登陆权限验证相关 说起来也都是泪,之前涉及权限的比较少所以这次准备起来就比较困难. 踩了好几个大坑,还好最终都一一消化掉(这是废话你没解决你写个什么劲
- spring boot中实现security错误信息本地化
一.修改messages.properties 找源码中的messages.properties,复制一份放在classpath下,修改你要修改的内容 AbstractUserDetailsAuthe ...
- Spring Boot发布2.6.2、2.5.8:升级log4j2到2.17.0
12月22日,Spring官方发布了Spring Boot 2.5.8(包括46个错误修复.文档改进和依赖项升级)和2.6.2(包括55个错误修复.文档改进和依赖项升级). 这两个版本均为缺陷修复版本 ...
- Spring Boot中采用Mockito来mock所测试的类的依赖(避免加载spring bean,避免启动服务器)
最近试用了一下Mockito,感觉真的挺方便的.举几个应用实例: 1,需要测试的service中注入的有一个dao,而我并不需要去测试这个dao的逻辑,只需要对service进行测试.这个时候怎么办呢 ...
随机推荐
- C enum(枚举)
C enum(枚举) 枚举是 C 语言中的一种基本数据类型,它可以让数据更简洁,更易读. 枚举语法定义格式为: enum 枚举名 {枚举元素1,枚举元素2,……}; 接下来我们举个例子,比如:一星期有 ...
- JS中条件判断语句
用pycharm敲代码时,在.js文件中敲,敲完之后复制代码到浏览器的console中去回车执行 1.if( ){} 2.switch(a){} 3.for循环 循环列表 循环字典 循环字符串 另外 ...
- 再不迁移到Material Design Components 就out啦
翻译自国外文档加自己理解 原文 我们最近宣布了 Material Design Components(MDC)1.1.0 ,这是一个库更新,为您的 Android 应用程序带来了 Material T ...
- [ArcGIS]高程地图-把DEM栅格数据(.tif)转换为TIN矢量数据,并储存TIN数据。
把DEM数据(.tif)获得栅格数据对应的经纬度及高程信息,存到地理数据库 一.预处理工作 栅格数据的合并--目的:将原始4张Dem(.tif)数据合并为一张Dem(.tif) https://wen ...
- JavaGuide易错点总结
基础知识易错点 1. object.equals("str") 容易报空指针异常,应使用"str".equals(object); 还可以使用JDK7引入的工具 ...
- 学习 | css3基本动画之demo篇
移动端使用的框架是zepto,但是zepto的内置对象没有传统的animate这个方法,效果都是需要css3来实现的,zepto也不支持fadeIn和fadeOut等一些基本的动画,基于这一现状,我自 ...
- jekins的搭建和踩过的坑
https://www.cnblogs.com/jwrwst/p/6520114.html 主要按照上面的来装的 中间遇到过一些问题 https://wiki.jenkins.io/display/J ...
- Socket 核心原理分享
Socket 的个人故事.希望通过这篇文章让你弄懂什么是 Socket,明白 TCP 和 UDP 协议的通讯,明白长连接和短连接的优缺点,明白 BIO.NIO.AIO的区别. Socket 大家好,我 ...
- MySQL 10w+数据 insert 优化
由于业务原因,遇到了如题所述的业务问题,事务执行时间在30s~50s 不等,效果非常不理想 方案1. jdbc批处理 5w+ 数据测试,分别使用了mybatis insert()()(拼接xml) ...
- vue学习03 v-html
vue学习03v-html v-html指令的作用是:设置元素的内部html链接 内容有html 的结构会被解析为标签 v-text指令无论内容是什么,只会解析文本 解析文本使用v-text,需要解析 ...