springboot-admin监控

  • Post author:
  • Post category:其他


server端pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>springboot-admin</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>2.1.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-autoconfigure</artifactId>-->
<!--            <version>2.7.6</version>-->
<!--        </dependency>-->

    </dependencies>

    <build>
        <finalName>oa</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.1.6.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

application.yml

server:
  port: 9020

spring:
  security:
    user:
      name: admin
      password: 123456
      roles: SBA_ADMIN

添加springSecurity账号密码登录

package com.bessky.admin.config;

import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
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;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import java.util.UUID;

/**
 * @author Luo-ping
 * @title: AdminServerSecurityConfig
 * @projectName springboot-admin
 * @description: TODO
 * @date 2023/4/1411:38
 */
@Configuration
public class AdminServerSecurityConfig extends WebSecurityConfigurerAdapter {
    private final AdminServerProperties adminServer;

    /**
     * Instantiates a new Security secure config.
     *
     * @param adminServer the admin server
     */
    public AdminServerSecurityConfig(AdminServerProperties adminServer) {
        this.adminServer = adminServer;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");
        final String adminServerContextPath = this.adminServer.getContextPath();
        successHandler.setDefaultTargetUrl(adminServerContextPath +"/");

        http.authorizeRequests()
            .antMatchers(adminServerContextPath +  "/assets/**").permitAll() // <1>
            .antMatchers(adminServerContextPath

                + "/login").permitAll()
            .anyRequest().authenticated() // <2>
            .and()
            .formLogin().loginPage(adminServerContextPath  + "/login").successHandler(successHandler).and() // <3>
            .logout().logoutUrl(adminServerContextPath +  "/logout").and()
            .httpBasic().and() // <4>
            .csrf()
            .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) // <5>
            .ignoringRequestMatchers(
                new AntPathRequestMatcher(adminServerContextPath +  "/instances", HttpMethod.POST.toString()),  // <6>
                new AntPathRequestMatcher(adminServerContextPath +  "/instances/*", HttpMethod.DELETE.toString()),  // <6>
                new AntPathRequestMatcher(adminServerContextPath  + "/actuator/**")  // <7>
            )
            .and()
            .rememberMe().key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600);

    }

}

client端

被监控的springboot中添加依赖

        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>2.1.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

添加配置

spring.boot.admin.client.url=http://10.10.1.101:9020
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=123456
spring.boot.admin.client.instance.prefer-ip=true
# 开启监控所有项
management.endpoints.web.exposure.include=*
management.endpoints.enabled-by-default=true
management.endpoints.health.sensitive=false
management.endpoint.health.show-details=ALWAYS



版权声明:本文为qq_30920479原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。