spring-boot-admin对spring-boot项目进行监控

栏目: Java · 发布时间: 6年前

内容简介:今天需要使用配置之后,访问需要用户名和密码修改端口,防止端口占用,同时指定用户名和密码

今天需要使用 spring-boot-adminspring-boot项目 进行监控的功能,之前网上的版本都太老旧了,已经不适合现在总结一下。目前版本 spring-boot-admin 版本是 2.1.4spring-boot项目 版本是 2.1.4.RELEASE

创建Server项目

1、新建项目为boot-admin-server,并引入Jar包

<properties>
	<java.version>1.8</java.version>
	<spring-boot-admin.version>2.1.4</spring-boot-admin.version>
</properties>

<dependencies>
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    	<groupId>de.codecentric</groupId>
    	<artifactId>spring-boot-admin-starter-server</artifactId>
    </dependency>
    
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-test</artifactId>
    	<scope>test</scope>
    </dependency>
    
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    </dependencies>
    
<dependencyManagement>
    <dependencies>
    	<dependency>
    		<groupId>de.codecentric</groupId>
    		<artifactId>spring-boot-admin-dependencies</artifactId>
    		<version>${spring-boot-admin.version}</version>
    		<type>pom</type>
    		<scope>import</scope>
    	</dependency>
    </dependencies>
</dependencyManagement>
复制代码

2、配置Spring-Security

import org.springframework.context.annotation.Configuration;
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;

@Configuration
public 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 + "/");
        
        http.authorizeRequests().antMatchers(adminContextPath + "/assets/**").permitAll()
        		.antMatchers(adminContextPath + "/login").permitAll().anyRequest().authenticated().and().formLogin()
        		.loginPage(adminContextPath + "/login").successHandler(successHandler).and().logout()
        		.logoutUrl(adminContextPath + "/logout").and().httpBasic().and().csrf()
        		.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
        		.ignoringAntMatchers(adminContextPath + "/instances", adminContextPath + "/actuator/**");
    }
}
复制代码

配置之后,访问需要用户名和密码

3、配置文件

server.port=8070
spring.security.user.name=admin
spring.security.user.password=123456

复制代码

修改端口,防止端口占用,同时指定用户名和密码

4、启动程序配置

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import de.codecentric.boot.admin.server.config.EnableAdminServer;

@SpringBootApplication
@EnableAdminServer
public class BootAdminServerApplication {

    public static void main(String[] args) {
    	SpringApplication.run(BootAdminServerApplication.class, args);
    }
}

复制代码

上述程序启动即可

配置Client项目

1、正常是自己的项目,这里为一个案例项目boot-admin-client,添加Jar包

<properties>
	<java.version>1.8</java.version>
	<spring-boot-admin.version>2.1.4</spring-boot-admin.version>
</properties>

<dependencies>
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    	<groupId>de.codecentric</groupId>
    	<artifactId>spring-boot-admin-starter-client</artifactId>
    </dependency>
    
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-test</artifactId>
    	<scope>test</scope>
    </dependency>
    <dependency>
    	<groupId>org.springframework.security</groupId>
    	<artifactId>spring-security-test</artifactId>
    	<scope>test</scope>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
    	<dependency>
        	<groupId>de.codecentric</groupId>
        	<artifactId>spring-boot-admin-dependencies</artifactId>
        	<version>${spring-boot-admin.version}</version>
        	<type>pom</type>
        	<scope>import</scope>
    	</dependency>
    </dependencies>
</dependencyManagement>
复制代码

2、配置文件

# 开放所有页面节点  默认只开启了health、info两个节点
management.endpoints.web.exposure.include=*
# 去除配置文件信息
#management.endpoints.web.exposure.exclude=configprops
# 显示健康具体信息  默认不会显示详细信息  
management.endpoint.health.show-details=always

# server端地址
spring.boot.admin.client.url=http://127.0.0.1:8070
# 本client地址
spring.boot.admin.client.instance.service-url=http://127.0.0.1:8080

# 验证Admin用户名及密码 
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=123456

spring.security.user.name=client
spring.security.user.password=123456
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

图解TCP/IP : 第5版

图解TCP/IP : 第5版

[日]竹下隆史、[日]村山公保、[日]荒井透、[日]苅田幸雄 / 乌尼日其其格 / 人民邮电出版社 / 2013-7-1 / 69.00元

这是一本图文并茂的网络管理技术书籍,旨在让广大读者理解TCP/IP的基本知识、掌握TCP/IP的基本技能。 书中讲解了网络基础知识、TCP/IP基础知识、数据链路、IP协议、IP协议相关技术、TCP与UDP、路由协议、应用协议、网络安全等内容,引导读者了解和掌握TCP/IP,营造一个安全的、使用放心的网络环境。 本书适合计算机网络的开发、管理人员阅读,也可作为大专院校相关专业的教学参考......一起来看看 《图解TCP/IP : 第5版》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具