springboot-admin监控

server端pom



    4.0.0

    org.example
    springboot-admin
    1.0-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.6.RELEASE
    

    
        
            org.springframework.boot
            spring-boot-starter-web
            
                
                    org.springframework.boot
                    spring-boot-starter-tomcat
                
            
        
        
            org.springframework.boot
            spring-boot-starter-jetty
        
        
            de.codecentric
            spring-boot-admin-starter-server
            2.1.6
        
        
            org.springframework.boot
            spring-boot-starter-security
        






    

    
        oa
        
            
                org.apache.maven.plugins
                maven-surefire-plugin
                2.18.1
                
                    true
                
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                2.1.6.RELEASE
                
                    
                        
                            repackage
                        
                    
                
            
        
    

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中添加依赖

        
            de.codecentric
            spring-boot-admin-starter-client
            2.1.6
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        

添加配置

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

springboot-admin监控_第1张图片

 springboot-admin监控_第2张图片

 

你可能感兴趣的:(java,spring,boot,spring)