一.配置server端
1.pom.xml添加依赖
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2.application.yml配置
spring:
application:
name: admin-server
security:
user:
name: "admin"
password: "admin"
server:
port: 64000
3.配置程序
package com.wsk.admin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
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 de.codecentric.boot.admin.server.config.AdminServerProperties;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
@SpringBootApplication
@EnableScheduling
@EnableAdminServer
public class SpringbootAdminApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootAdminApplication.class, args);
}
@Configuration
public static 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()//Grants public access to all static assets and the login page.
.antMatchers(adminContextPath + "/login").permitAll()
.anyRequest().authenticated()// Every other request must be authenticated.
.and()
.formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()//Configures login and logout.
.logout().logoutUrl(adminContextPath + "/logout").and()
.httpBasic().and()//Enables HTTP-Basic support. This is needed for the Spring Boot Admin Client to register.
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())// Enables CSRF-Protection using Cookies
.ignoringAntMatchers(
adminContextPath + "/instances",// Disables CRSF-Protection the endpoint the Spring Boot Admin Client uses to register.
adminContextPath + "/actuator/**"//Disables CRSF-Protection for the actuator endpoints.
);
}
}
}
4.运行http://localhost:64000/login
输入用户名admin和密码admin登入
二. 配置需要监控的spring boot程序
1.添加依赖
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.1.4</version>
</dependency>
2.修改application.yml配置
spring:
application:
name: admin-client
boot:
admin:
client:
url: http://localhost:64000
username: admin
password: admin
server:
port: 64001
management:
endpoints:
web:
exposure:
include: '*'
exclude: configprops
endpoint:
health:
show-details: ALWAYS
3.执行应该能注册成功,如发现问题可增加debug打印,配置为application.yml同级目录增加logback.xml
配置为
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="org.springframework.web" level="DEBUG"/>
<jmxConfigurator/>
</configuration>
可显示debug信息,正常关闭即可
4.正常显示结果
参考博客文档
https://blog.csdn.net/lv4961382/article/details/85129144
https://blog.csdn.net/weixin_34067049/article/details/88677682