SpringBoot实战(十四):Spring Boot Admin 集成安全模块

简介: SpringBoot实战(十四):Spring Boot Admin 集成安全模块

强烈推荐一个大神的人工智能的教程:http://www.captainai.net/zhanghan


【前言】


      Spring Boot Admin做为生产级的监控工具,必然不能随便让人去操作以免误操作导致线上问题,所以有必要集成Security组件;Spring Boot Admin可以十分简单的集成这安全组件;已集成项目中,在此与大家共享;


【集成安全模块】


        一、集成安全(Security)模块


                1、Spring Boot Admin服务端集成(以zh-monitor为例)


                     (1)Pom中增加Security依赖


<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-security</artifactId>
</dependency>

                     (2)配置文件(application.properties)中增加用户名和密码的设置


#spring boot default user.name='user'
spring.security.user.name=admin
#spring boot dafault user.password 在项目启动时打印在控制台中
spring.security.user.password=admin

                     (3)增加SecuritySecureConfig配置类


/*
 * Copyright (c) 2019. zhanghan_java@163.com All Rights Reserved.
 * 项目名称:实战SpringBoot
 * 类名称:SecuritySecureConfig.java
 * 创建人:张晗
 * 联系方式:zhanghan_java@163.com
 * 开源地址: https://github.com/dangnianchuntian/springboot
 * 博客地址: https://zhanghan.blog.csdn.net
 */
package com.zhanghan.zhmonitor.config;
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
    private final String adminContextPath;
    public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
        this.adminContextPath = adminServerProperties.getContextPath();
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");
        successHandler.setDefaultTargetUrl(adminContextPath + "/");
        http.authorizeRequests()
                .antMatchers(adminContextPath + "/assets/**").permitAll()
                .antMatchers(adminContextPath + "/login").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
                .logout().logoutUrl(adminContextPath + "/logout").and()
                .httpBasic().and()
                .csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                .ignoringAntMatchers(
                        adminContextPath + "/instances",
                        adminContextPath + "/actuator/**");
        // @formatter:on
    }
}


                2、Spring Boot Admin客户端集成(以zh-boot为例)


                     (1)在配置文件(application.properties)中增加用户名和密码


#Security
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=admin

                3、查看效果


                     (1)启动zh-monitor


                     (2)启动zh-boot


                     (3)访问zh-monitor(http://localhost:8081)


                              a.跳转至登录页面


43f0505cf2c61bd7bdcb081cda0e08e9_watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly96aGFuZ2hhbi5ibG9nLmNzZG4ubmV0,size_16,color_FFFFFF,t_70.png


                              b.输入admin的用户名和密码登录


8132d2aa97b8fc470354b3dfc0877e6d_watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly96aGFuZ2hhbi5ibG9nLmNzZG4ubmV0,size_16,color_FFFFFF,t_70.png


        三、项目地址:


                1、地址:https://github.com/dangnianchuntian/springboot


                2、代码版本:1.6.0-Release


【总结】


        1、安全猛于虎,没有安全设置相当于裸奔,一般线上环境的Spring Boot Admin应该由运维统一控制,开发只能查看,如果需要更改日志级别等操作等应由技术leader批准运维统一执行;


        2、接下来会为大家共享多关于SpringBootAdmin集成告警模块。


相关文章
|
2天前
|
Java 应用服务中间件 测试技术
深入探索Spring Boot Web应用源码及实战应用
【5月更文挑战第11天】本文将详细解析Spring Boot Web应用的源码架构,并通过一个实际案例,展示如何构建一个基于Spring Boot的Web应用。本文旨在帮助读者更好地理解Spring Boot的内部工作机制,以及如何利用这些机制优化自己的Web应用开发。
29 3
|
2天前
|
安全 Java 开发者
深入理解Spring Boot配置绑定及其实战应用
【4月更文挑战第10天】本文详细探讨了Spring Boot中配置绑定的核心概念,并结合实战示例,展示了如何在项目中有效地使用这些技术来管理和绑定配置属性。
14 1
|
2天前
|
Java 开发者 Spring
springboot DDD的概念以及实战
【5月更文挑战第15天】领域驱动设计(Domain-Driven Design,简称DDD)是一种软件设计方法论,它强调基于业务领域的复杂性来构建软件
9 2
|
2天前
|
开发框架 监控 Java
深入探索Spring Boot的监控、管理和测试功能及实战应用
【5月更文挑战第14天】Spring Boot是一个快速开发框架,提供了一系列的功能模块,包括监控、管理和测试等。本文将深入探讨Spring Boot中监控、管理和测试功能的原理与应用,并提供实际应用场景的示例。
15 2
|
2天前
|
NoSQL Java MongoDB
【MongoDB 专栏】MongoDB 与 Spring Boot 的集成实践
【5月更文挑战第11天】本文介绍了如何将非关系型数据库MongoDB与Spring Boot框架集成,以实现高效灵活的数据管理。Spring Boot简化了Spring应用的构建和部署,MongoDB则以其对灵活数据结构的处理能力受到青睐。集成步骤包括:添加MongoDB依赖、配置连接信息、创建数据访问对象(DAO)以及进行数据操作。通过这种方式,开发者可以充分利用两者优势,应对各种数据需求。在实际应用中,结合微服务架构等技术,可以构建高性能、可扩展的系统。掌握MongoDB与Spring Boot集成对于提升开发效率和项目质量至关重要,未来有望在更多领域得到广泛应用。
【MongoDB 专栏】MongoDB 与 Spring Boot 的集成实践
|
2天前
|
消息中间件 JSON Java
RabbitMQ的springboot项目集成使用-01
RabbitMQ的springboot项目集成使用-01
|
2天前
|
搜索推荐 Java 数据库
springboot集成ElasticSearch的具体操作(系统全文检索)
springboot集成ElasticSearch的具体操作(系统全文检索)
|
2天前
|
消息中间件 Java Spring
Springboot 集成Rabbitmq之延时队列
Springboot 集成Rabbitmq之延时队列
6 0
|
2天前
|
网络协议 Java Spring
Springboot 集成websocket
Springboot 集成websocket
11 0
|
2天前
|
XML Java 数据库
springboot集成flowable
springboot集成flowable