前边有在项目中集成过actuator程序监控器,但是那个玩意是页面返回的一个json。关于actuator使用请移步《SpringBoot(三十三)SpringBoot集成Spring boot actuator程序监控器》
看起来不是那么方便。那么有没有一个图形化页面的程序监控器呢?
你别说,还真有,那就是Spring-boot-admin,这是个好东西啊。
老规矩,动手之前。先看官网:spring-boot-admin.com/docs/index
一:部署server端
1:引入pom依赖
创建一个Springboot项目,在项目中引入Spring-boot-admin依赖。Springboot和Spring-boot-admin版本一一对应,我的Springboot项目版本是3.2.10,但是Spring-boot-admin得版本只有3.2.3,但也是好用的。
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>3.2.3</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server</artifactId>
<version>3.2.3</version>
</dependency>
2:添加启动注解@EnableAdminServer
在项目的启动类上添加注解@EnableAdminServer
3:添加账号密码配置
其实走到上一步,Spring-boot-admin就可以访问了,但是呢,这样不安全,现在网络爬虫太狠了,一旦爬到你这个地址,要是没有账号密码直接就能访问,那不完了嘛。
(1):引入pom依赖
<!-- 加入密码认证 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
(2):security配置类
package com.modules.admin.config;
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import jakarta.servlet.DispatcherType;
import org.apache.catalina.filters.CsrfPreventionFilter;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
import org.springframework.security.web.csrf.CsrfTokenRequestAttributeHandler;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import java.util.UUID;
import static java.nio.file.attribute.AclEntryPermission.DELETE;
import static javax.swing.text.html.FormSubmitEvent.MethodType.POST;
/**
* spring boot admin 安全配置
* 参考文档:http://spring-boot-admin.com/docs/server/security
*/
@Configuration(proxyBeanMethods = false)
public class SecuritySecureConfig
{
private final AdminServerProperties adminServer;
private final SecurityProperties security;
public SecuritySecureConfig(AdminServerProperties adminServer, SecurityProperties security)
{
this.adminServer = adminServer;
this.security = security;
}
@Bean
protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception
{
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(this.adminServer.path("/"));
http.authorizeHttpRequests((authorizeRequests) -> authorizeRequests //
.requestMatchers(new AntPathRequestMatcher(this.adminServer.path("/assets/**")))
.permitAll() // (1)
.requestMatchers(new AntPathRequestMatcher(this.adminServer.path("/actuator/info")))
.permitAll()
.requestMatchers(new AntPathRequestMatcher(adminServer.path("/actuator/health")))
.permitAll()
.requestMatchers(new AntPathRequestMatcher(this.adminServer.path("/login")))
.permitAll()
.dispatcherTypeMatchers(DispatcherType.ASYNC)
.permitAll() // https://github.com/spring-projects/spring-security/issues/11027
.anyRequest()
.authenticated()) // (2)
.formLogin(
(formLogin) -> formLogin.loginPage(this.adminServer.path("/login")).successHandler(successHandler)) // (3)
.logout((logout) -> logout.logoutUrl(this.adminServer.path("/logout")))
.httpBasic(Customizer.withDefaults()); // (4)
//http.addFilterAfter(new CustomCsrfFilter(), BasicAuthenticationFilter.class) // (5)
http.csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.csrfTokenRequestHandler(new CsrfTokenRequestAttributeHandler())
.ignoringRequestMatchers(
new AntPathRequestMatcher(this.adminServer.path("/instances"), POST.toString()), // (6)
new AntPathRequestMatcher(this.adminServer.path("/instances/*"), DELETE.toString()), // (6)
new AntPathRequestMatcher(this.adminServer.path("/actuator/**")) // (7)
));//*/
http.rememberMe((rememberMe) -> rememberMe.key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600));
return http.build();
}
// 在内存中配置用户
// Required to provide UserDetailsService for "remember functionality"
/*@Bean
public InMemoryUserDetailsManager userDetailsService(PasswordEncoder passwordEncoder) {
UserDetails user = User.withUsername("user").password(passwordEncoder.encode("camellia")).roles("USER").build();
return new InMemoryUserDetailsManager(user);
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}//*/
}
以上的代码来自官方文档,链接我也放在代码注释里边了。参考即可。
4:配置application.yml配置文件
server:
port: 6688
spring:
application:
name: SpringBootAdmin
security:
user:
name: admin
password: camellia
basic:
enabled: true
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
以上的配置也是来自官方文档,有兴趣的自己去了解一下。懒得看的,我这个配置应该也就是够用了。
至此,Spring-boot-admin的server端已经配置成功了。
浏览器访问:http://127.0.0.1:6688/
用上边配置的用户名密码登录即可。
二:配置Spring-boot-admin的client端
这个地方是最坑人的,可坑了。别问我被啥坑了,我替你踩过了,你就不用踩了。照着来就行了。
1:添加pom依赖
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>3.2.3</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
2:配置application.yml
spring:
application:
name: admin-client
boot:
admin:
client:
url: http://127.0.0.1:6688
username: admin
password: camellia
# instance:
# metadata:
# user.name: admin
# user.password: camellia
info.tags.security: secured
management:
health:
elasticsearch:
enabled: false # true:开启,false:关闭,默认为 true
redis:
enabled: false # true:开启,false:关闭,默认为 true
mail:
enabled: false # true:开启,false:关闭,默认为 true
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
到这里,Spring-boot-admin的客户端配置就完成了。
启动客户端。服务端就会收到客户端注册信息:
上边集成的过程基本上就是按照官方文档的提示简单来的。
Spring-boot-admin还为我们提供了很多的定制化配置,比如去掉界面上的一些显示设置之类的。这个我没有过多研究,具体请参考官方文档。
有好的建议,请在下方输入你的评论。