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.跳转至登录页面

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

         三、项目地址:

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

                 2、代码版本:1.6.0-Release

【总结】

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

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

  • 13
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

当年的春天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值