依赖pom.xml
<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>
<parent>
<groupId>org.pingruan</groupId>
<artifactId>vander-framework-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>org.pingruan.springboot</groupId>
<artifactId>vander-springboot-admin</artifactId>
<name>监控基于 Spring Boot 的应用</name>
<description>Spring Boot Admin 提供了很多功能,如显示 name、id 和 version,显示在线状态,Loggers 的日志级别管理,Threads 线程管理,Environment 管理等</description>
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<!-- 注册中心服务down发送邮件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- 服务监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<exclusions>
<exclusion>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置文件bootstrap.yml
server:
port: 9007
spring:
application:
name: vander-springboot-admin
profiles:
active:
- security #
#-----------------注册中心服务down发送邮件--------------------
mail:
host: smtp.qq.com
username: [email protected]
password: cgdyxuyuqebebbec
properties:
mail.debug: false
mail.smtp.auth: true #安全认证(默认是true)
mail.smtp.port: 465
mail.smtp.ssl.enable: true #开启ssl加密 否则项目启动时报530error
mail.smtp.ssl.socketFactory: sf
mail.smtp.starttls.enable: true
mail.smtp.starttls.required: true
boot:
admin:
notify:
mail:
to: [email protected]
from: [email protected]
#-----------------基本配置(客户端也需要配置)--------------------
management:
endpoints:
web:
exposure:
include:
- "*"
endpoint:
health:
show-details: always
#------------注册中心配置------------------
eureka:
client:
registerWithEureka: true #服务注册开关
fetchRegistry: true #服务发现开关
serviceUrl: #Eureka客户端与Eureka服务端进行交互的地址,多个中间用逗号分隔
defaultZone: http://root:qwe123@server1:9001/eureka/
instance:
prefer-ip-address: true #将自己的ip地址注册到Eureka服务中
ip-address: 127.0.0.1
instance-id: ${spring.application.name}:${server.port} #指定实例id
#------------不做安全校验------------------
---
spring:
profiles: default
#------------安全校验------------------
---
spring:
profiles: security
security:
user:
name: root
password: 123
eureka:
instance:
metadata-map:
user.name: ${spring.security.user.name} # 配置访问权限(如果客户端配置安全访问,需要配置其对应安全信息)
user.password: ${spring.security.user.password} #
配置源码
/**
* springboot应用监控中心
*
* 1、被监控服务需要添加actuator依赖 2、服务注册到euraka 3、服务启动访问/actuator/health status 为 up
* 4、参考该项目配置文件application.yml 中客户端配置项
*
*
* @author vander
*
*/
@EnableDiscoveryClient
@SpringBootApplication
@EnableAdminServer
public class SpringBootAdmin {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdmin.class, args);
}
@Profile("default")
@Configuration
public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll()//
.and().csrf().disable();
}
}
/**
* 基于安全认证的spring boot admin
*
* @author vander
*
*/
@Profile("security")
@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 {
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(adminContextPath + "/#/applications");
http.authorizeRequests()
.antMatchers(adminContextPath + "/login").permitAll().anyRequest().authenticated().and().formLogin()
.loginPage(adminContextPath + "/login").successHandler(successHandler).and().logout()
.logoutUrl(adminContextPath + "/logout").and().csrf().disable();
// 允许登录页面和静态资源的请求
http.authorizeRequests().antMatchers("/login.html", "/**/*.css", "/img/**", "/third-party/**").permitAll();
http.httpBasic();
}
}
}