Spring boot admin的实现
服务端:
第一步:POM依赖引入
<?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>vnacos</artifactId> <groupId>com.nacos</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>spring-admin</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--1. Spring-boot-admin服务端 --> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.3.1</version> </dependency> <!--安全问题--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <!--nacos-服务注册发现--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!--邮件通知--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> </dependencies> </project>
第二部:配置文件(使用nacos进行管理)
server: port: 8091 spring: application: name: admin security: # 使用 security 进行安全管理 user: name: vn password: vn cloud: nacos: server-addr: 192.168.43.197:8848 # nacos 服务器地址 discovery: username: nacos password: nacos namespace: public
第三步:security进行安全管理
package vn.admin.config; import de.codecentric.boot.admin.server.config.AdminServerProperties; import org.springframework.boot.autoconfigure.security.SecurityProperties; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpMethod; import org.springframework.security.config.Customizer; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 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 vn * @date */ @Configuration(proxyBeanMethods = false) public class SecuritySecureConfig extends WebSecurityConfigurerAdapter { private final AdminServerProperties adminServer; private final SecurityProperties security; public SecuritySecureConfig(AdminServerProperties adminServer, SecurityProperties security) { this.adminServer = adminServer; this.security = security; } @Override protected void configure(HttpSecurity http) throws Exception { SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler(); successHandler.setTargetUrlParameter("redirectTo"); successHandler.setDefaultTargetUrl(this.adminServer.path("/")); http.authorizeRequests( (authorizeRequests) -> authorizeRequests.antMatchers(this.adminServer.path("/assets/**")).permitAll() .antMatchers(this.adminServer.path("/actuator/info")).permitAll() .antMatchers(this.adminServer.path("/actuator/health")).permitAll() .antMatchers(this.adminServer.path("/login")).permitAll().anyRequest().authenticated() ).formLogin( (formLogin) -> formLogin.loginPage(this.adminServer.path("/login")).successHandler(successHandler).and() ).logout((logout) -> logout.logoutUrl(this.adminServer.path("/logout"))).httpBasic(Customizer.withDefaults()) .csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) .ignoringRequestMatchers( new AntPathRequestMatcher(this.adminServer.path("/instances"), HttpMethod.POST.toString()), new AntPathRequestMatcher(this.adminServer.path("/instances/*"), HttpMethod.DELETE.toString()), new AntPathRequestMatcher(this.adminServer.path("/actuator/**")) )) .rememberMe((rememberMe) -> rememberMe.key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600)); } // Required to provide UserDetailsService for "remember functionality" @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().withUser(security.getUser().getName()) .password("{noop}" + security.getUser().getPassword()).roles("USER"); } }
第四步:启动类
package vn.admin; import de.codecentric.boot.admin.server.config.EnableAdminServer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author vn * @version 1.0 * @date 2022/3/19 20:43 */ /** * 通过添加@EnableAdminServer到配置中来引入Spring Boot Admin Server配置 */ @SpringBootApplication @EnableAdminServer public class VnAdminApplication { public static void main(String[] args) { SpringApplication.run(VnAdminApplication.class, args); } }
客户端:
第一步:POM文件依赖
<?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>vnacos</artifactId> <groupId>com.nacos</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>order-feign</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--nacos-服务注册发现--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!--1. 添加openfeign依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!--spring-boot-admin客户端依赖--> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.3.1</version> </dependency> </dependencies> </project>
第二部:配置文件
server: port: 82 spring: application: name: order-service # 应用名称 nacos 会将名称作为服务名称 boot: # spring boot admin 服务端密码 admin: client: username: vn password: vn cloud: nacos: server-addr: 192.168.43.197:8848 # nacos 服务器地址 discovery: username: nacos password: nacos namespace: public
如图所示:
非Nacos注册中心管理:
配置文件:
server: port: 8090 shutdown: graceful # 设置web服务器的关闭方式 (默认是立即关闭) 需要改优雅关闭 spring: application: name: actuator cloud: nacos: server-addr: 192.168.43.197:8848 # nacos 服务器地址 discovery: username: nacos password: nacos namespace: public boot: admin: client: username: vn password: vn instance: prefer-ip: true # 使用 IP 进行注册 url: http://localhost:8091 # 指定服务端地址 # 暴露所有端点 management: endpoints: enabled-by-default: false # 下面来看一下如何细粒度的开启指定的端点,需要关闭默认开启的所有端点 web: base-path: /vn # 设置访问路径 exposure: # include: '*' # 属性可以通过逗号分隔符来配置多个端点 include=beans, loggers include: health # 开启并暴露/health端点 endpoint: health: # shutdown: show-details: 'always' enabled: true # 开启并暴露/health端点 logging: file: name: D:/logs/vn.log # 日志存放位置
邮箱服务:
spring: mail: # 发件人使用的qq邮箱服务 host: smtp.qq.com username: tulingxushu@foxmail.com # 授权码,不是密码,在qq邮箱设置-账号里面有生成授权码 password: bktymeooyuapggbe boot: admin: notify: mail: # 收件人,多个中间用,分隔 to: tulingxushu@foxmail.com # 发件人 from: tulingxushu@foxmail.com