SpringBoot Admin 是一个codecentric 的社区项目,用于管理和监控您的Spring Boot ®应用程序

文档地址:https://codecentric.github.io/spring-boot-admin/current/

强调1点:Springboot Admin的接入 需要单独开一个Server服务,其他服务作为客户端,去向Server服务进行注册,将来就可以在Server端看到各个客户端的信息了!

快速接入

Maven Server 端依赖

        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-server-ui</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

服务端代码

启动类加入下面注解,服务端配置完成

// 开启Spring Boot Admin服务
@EnableAdminServer

服务端配置文件 特殊说明,配置文件的2套账号作用不同,具体看配置文件注释

server.port=10000

# 设置Spring Boot Admin-UI登录的账号与密码
spring.security.user.name=root
spring.security.user.password=root

# 配置服务端实例授权账号(这样可以防止别人的服务随意注册)
spring.boot.admin.instance-auth.enabled=true
spring.boot.admin.instance-auth.default-user-name=root
spring.boot.admin.instance-auth.default-password=root

# 配置UI界面的样式
spring.boot.admin.ui.title=740969606

服务端UI配置类

import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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 : zanglikun
 * @date : 2021/12/22 14:55
 * @Version: 1.0
 * @Desc : Spring Boot Admin 的UI界面登录限制  官方粘贴获取
 */
@Configuration(proxyBeanMethods = false)
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {

    private final AdminServerProperties adminServer;

    private final SecurityProperties security;

    public SecuritySecureConfig(AdminServerProperties adminServer, SecurityProperties security) {
        this.adminServer = adminServer;
        this.security = security;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");
        successHandler.setDefaultTargetUrl(this.adminServer.path("/"));

        http.authorizeRequests(
                        (authorizeRequests) -> authorizeRequests.antMatchers(this.adminServer.path("/assets/**")).permitAll()
                                .antMatchers(this.adminServer.path("/actuator/info")).permitAll()
                                .antMatchers(this.adminServer.path("/actuator/health")).permitAll()
                                .antMatchers(this.adminServer.path("/login")).permitAll().anyRequest().authenticated()
                ).formLogin(
                        (formLogin) -> formLogin.loginPage(this.adminServer.path("/login")).successHandler(successHandler).and()
                ).logout((logout) -> logout.logoutUrl(this.adminServer.path("/logout"))).httpBasic(Customizer.withDefaults())
                .csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                        .ignoringRequestMatchers(
                                new AntPathRequestMatcher(this.adminServer.path("/instances"),
                                        HttpMethod.POST.toString()),
                                new AntPathRequestMatcher(this.adminServer.path("/instances/*"),
                                        HttpMethod.DELETE.toString()),
                                new AntPathRequestMatcher(this.adminServer.path("/actuator/**"))
                        ))
                .rememberMe((rememberMe) -> rememberMe.key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600));
    }

    // Required to provide UserDetailsService for "remember functionality"
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser(security.getUser().getName())
                .password("{noop}" + security.getUser().getPassword()).roles("USER");
    }

}

Maven Client 端依赖

        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>2.5.5</version>
        </dependency>

客户端配置文件

# SpringBoot Admin配置 指定服务的URL
spring.boot.admin.client.enabled=true
# 向服务端中注册的位置
spring.boot.admin.client.url=http://127.0.0.1:10000
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

# 在服务端展示IP以代替主机名。(如:DESKTOP-5MJ5585)
spring.boot.admin.client.instance.prefer-ip=true

# 默认值是配置的spring.application.name的值
spring.boot.admin.client.instance.name=WMS

# 如果服务端开启了注册限制,就需要我们输入账号了。
spring.boot.admin.client.username=root
spring.boot.admin.client.password=root

完结。先启动Admin服务,启动完成后,再去开启Client端!

服务端 直接访问项目位置即可,如127.0.0.1:8080 即可登录Spring Boot Admin

服务端配置的是root root

注意事项:服务端、客户端<version>尽量保持一致。

特殊说明:
上述文章均是作者实际操作后产出。烦请各位,请勿直接盗用!转载记得标注原文链接:www.zanglikun.com
第三方平台不会及时更新本文最新内容。如果发现本文资料不全,可访问本人的Java博客搜索:标题关键字。以获取最新全部资料 ❤