集成spring-boot-admin 做为 monitor
This commit is contained in:
44
ballcat-monitor/pom.xml
Normal file
44
ballcat-monitor/pom.xml
Normal file
@@ -0,0 +1,44 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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">
|
||||
<parent>
|
||||
<artifactId>ballcat</artifactId>
|
||||
<groupId>com.hccake</groupId>
|
||||
<version>0.0.1</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>ballcat-monitor</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!--监控server端-->
|
||||
<dependency>
|
||||
<groupId>de.codecentric</groupId>
|
||||
<artifactId>spring-boot-admin-starter-server</artifactId>
|
||||
<version>2.1.6</version>
|
||||
</dependency>
|
||||
<!--webmvc-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<!--undertow容器-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-undertow</artifactId>
|
||||
</dependency>
|
||||
<!-- spring-security -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-security</artifactId>
|
||||
</dependency>
|
||||
<!-- ballcat-common-core -->
|
||||
<dependency>
|
||||
<groupId>com.hccake</groupId>
|
||||
<artifactId>ballcat-common-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.hccake.ballcat.monitor;
|
||||
|
||||
import de.codecentric.boot.admin.server.config.EnableAdminServer;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* @author Hccake
|
||||
* @version 1.0
|
||||
* @date 2019/10/26 16:32
|
||||
*/
|
||||
@EnableAdminServer
|
||||
@SpringBootApplication
|
||||
public class MonitorApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(MonitorApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.hccake.ballcat.monitor.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 Hccake
|
||||
*/
|
||||
@Configuration
|
||||
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
private final String adminContextPath;
|
||||
|
||||
public SecurityConfig(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(this.adminContextPath +"/");
|
||||
|
||||
http.authorizeRequests()
|
||||
.antMatchers(this.adminContextPath +"/assets/**").permitAll()
|
||||
.antMatchers(this.adminContextPath +"/login").permitAll()
|
||||
.anyRequest().authenticated()
|
||||
.and()
|
||||
.formLogin().loginPage(this.adminContextPath +"/login").successHandler(successHandler).and()
|
||||
.logout().logoutUrl(this.adminContextPath +"/logout").and()
|
||||
.httpBasic().and()
|
||||
.csrf()
|
||||
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
|
||||
.ignoringRequestMatchers(
|
||||
new AntPathRequestMatcher(this.adminContextPath +"/instances", HttpMethod.POST.toString()),
|
||||
new AntPathRequestMatcher(this.adminContextPath +"/instances/*", HttpMethod.DELETE.toString()),
|
||||
new AntPathRequestMatcher(this.adminContextPath +"/actuator/**")
|
||||
)
|
||||
.and()
|
||||
.rememberMe().key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600);
|
||||
// @formatter:on
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.hccake.ballcat.monitor.security;
|
||||
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import com.hccake.ballcat.common.core.constant.HeaderConstants;
|
||||
import de.codecentric.boot.admin.server.domain.entities.Instance;
|
||||
import de.codecentric.boot.admin.server.web.client.HttpHeadersProvider;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author Hccake
|
||||
* @version 1.0
|
||||
* @date 2019/10/31 11:03
|
||||
*/
|
||||
@Component
|
||||
public class AuthHeaderProvider implements HttpHeadersProvider {
|
||||
|
||||
@Value("${monitor.secret-id: ballcat-monitor}")
|
||||
private String secretId;
|
||||
@Value("${monitor.secret-key: =BallCat-Monitor}")
|
||||
private String secretKey;
|
||||
|
||||
|
||||
@Override
|
||||
public HttpHeaders getHeaders(Instance instance) {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
// 当前时间戳
|
||||
long reqTime = System.currentTimeMillis();
|
||||
headers.set(HeaderConstants.REQ_TIME, String.valueOf(reqTime));
|
||||
// 客户端ID
|
||||
headers.set(HeaderConstants.SECRET_ID, secretId);
|
||||
// sign
|
||||
String tempSign = new StringBuilder().append(reqTime).reverse().append(secretId).append(secretKey).toString();
|
||||
String sign = SecureUtil.md5(tempSign);
|
||||
headers.set(HeaderConstants.SIGN, sign);
|
||||
|
||||
return headers;
|
||||
}
|
||||
}
|
||||
19
ballcat-monitor/src/main/resources/application.yml
Normal file
19
ballcat-monitor/src/main/resources/application.yml
Normal file
@@ -0,0 +1,19 @@
|
||||
# 项目端口号
|
||||
server:
|
||||
port: 9999
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: @artifactId@
|
||||
profiles:
|
||||
active: @profiles.active@ # 当前激活配置,默认dev
|
||||
security:
|
||||
user:
|
||||
name: admin
|
||||
password: 123456
|
||||
|
||||
|
||||
# 请求Actuator加解密密钥
|
||||
monitor:
|
||||
secret-id: 'ballcat-monitor'
|
||||
secret-key: '=BallCat-Monitor'
|
||||
Reference in New Issue
Block a user