一,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)的更多相关文章

  1. 曹工说Spring Boot源码系列开讲了(1)-- Bean Definition到底是什么,附spring思维导图分享

    写在前面的话&&About me 网上写spring的文章多如牛毛,为什么还要写呢,因为,很简单,那是人家写的:网上都鼓励你不要造轮子,为什么你还要造呢,因为,那不是你造的. 我不是要 ...

  2. 基于spring boot2.0+spring security +oauth2.0+ jwt微服务架构

    github地址:https://github.com/hankuikuide/microservice-spring-security-oauth2 项目介绍 该项目是一个演示项目,主要演示了,基于 ...

  3. 0.spring cloud目录

    1. 微服务架构概述 1.0. 单体架构是什么 1.1. 单体应用架构存在的问题 1.2. 如何解决单体应用架构存在的问题 1.3. 什么是微服务 1.4. 微服务架构的优点与挑战 1.4.1. 微服 ...

  4. spring boot系列03--spring security (基于数据库)登录和权限控制(下)

    (接上篇) 后台 先说一下AuthConfig.java Spring Security的主要配置文件之一 AuthConfig 1 @Configuration 2 @EnableWebSecuri ...

  5. spring boot:swagger3的安全配置(swagger 3.0.0 / spring security / spring boot 2.3.3)

    一,swagger有哪些环节需要注意安全? 1,生产环境中,要关闭swagger application.properties中配置: springfox.documentation.swagger- ...

  6. spring boot系列03--spring security (基于数据库)登录和权限控制(上)

    这篇打算写一下登陆权限验证相关 说起来也都是泪,之前涉及权限的比较少所以这次准备起来就比较困难. 踩了好几个大坑,还好最终都一一消化掉(这是废话你没解决你写个什么劲

  7. spring boot中实现security错误信息本地化

    一.修改messages.properties 找源码中的messages.properties,复制一份放在classpath下,修改你要修改的内容 AbstractUserDetailsAuthe ...

  8. 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个错误修复.文档改进和依赖项升级). 这两个版本均为缺陷修复版本 ...

  9. Spring Boot中采用Mockito来mock所测试的类的依赖(避免加载spring bean,避免启动服务器)

    最近试用了一下Mockito,感觉真的挺方便的.举几个应用实例: 1,需要测试的service中注入的有一个dao,而我并不需要去测试这个dao的逻辑,只需要对service进行测试.这个时候怎么办呢 ...

随机推荐

  1. 别再眼高手低了! 这些Linq方法都清楚地掌握了吗?

    不要再眼高手低了,这些Enumerable之常见Linq扩展方法都清楚掌握了吗?其实这是对我自己来说的! 例如:一个人这个技术掌握了一点那个技术也懂一点,其他的好像也了解一些,感觉自己啥都会一点,又觉 ...

  2. 【Flutter 实战】菜单(Menu)功能

    老孟导读:今天介绍下Flutter中的菜单功能. PopupMenuButton 使用PopupMenuButton,点击时弹出菜单,用法如下: PopupMenuButton<String&g ...

  3. Idea没安装几款好用的插件,怎么风骚的写代码???

    ​ 工欲善其事,必先利其器,好的工具可以提升我们的开发效率,越来越多的Java程序员从Eclipse转到了Jetbrains家的Idea.今天给大家介绍的是我常用的十几款Idea必装的插件. ​ Ti ...

  4. JS -- 操作符和数组

    一.Javascript常用操作符 <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...

  5. Redis之命令详解

    Redis命令手册:http://doc.redisfans.com/

  6. php post数据丢失

    from的enctype="multipart/form-data" php版本5.6.6 问题:部分POST数据接收不到 追源代码发现是php中max_input_vars配置造 ...

  7. Netty之ChannelOption的各种参数之EpollChannelOption.SO_REUSEPORT

    socket选项 SO_REUSEPORT 转 miffa 发布于 2015/03/24 17:21 字数 3383 阅读 6076 收藏 6 点赞 1 评论 0 开发十年,就只剩下这套Java开发体 ...

  8. (转)CrudRepository JpaRepository PagingAndSortingRepository之间的区别

    1. 简介 本文介绍三种不同的Spring Data repository和它们的功能,包含以下三种: CrudRepository PagingAndSortingRepository JpaRep ...

  9. 在VS2019使用MASM编写汇编程序

    具体的配置步骤可以参考: 汇编环境搭建 Windows10 VS2019 MASM32 本文主要是入门向的教程,VS2019中要调用C语言函数需要加上 includelib ucrt.lib incl ...

  10. Java11新特性

    局部变量类型推断增强 Java11中可以在lambda表达式的形参中使用var,好处是可以在形参上加注解 使用示例 (@Deprecated var x, @Nullable var y)->x ...