发布于2021-06-12 16:06 阅读(745) 评论(0) 点赞(14) 收藏(2)
codecentric 的 Spring Boot Admin 是一个社区项目,用于管理和监控您的Spring Boot ®应用程序。应用程序向我们的 Spring Boot Admin Client 注册(通过 HTTP)或使用 Spring Cloud ®(例如 Eureka、Consul)发现。UI 只是一个位于 Spring Boot Actuator 端点之上的 Vue.js 应用程序。
环境 | 版本 |
Spring Boot | 2.4.6 |
JDK | 13 |
Idea | 2020.1 |
- <properties>
- <java.version>11</java.version>
- <spring-boot-admin.version>2.3.1</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>
- </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>
- server:
- port: 8090
- servlet:
- contest-path: /
- spring:
- application:
- name: admin-server
- @SpringBootApplication
- @EnableAdminServer
- public class ServerApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(ServerApplication.class, args);
- }
-
- }
- <properties>
- <java.version>11</java.version>
- <spring-boot-admin.version>2.3.1</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-client</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>
- server:
- port: 8091
- servlet:
- contest-path: /
- spring:
- application:
- name: admin-client
- boot:
- admin:
- client:
- url: http://localhost:8090
- management:
- endpoints:
- web:
- exposure:
- include: '*'
- endpoint:
- health:
- show-details: always
分别启动8090和8091项目,下图说明Client成功在Server端注册
打开浏览器输入http://localhost:8090回车
点击实例进入监控页面
功能不一一列举了,admin功能包括:
添加安全认证也很简单,需要在原来文件的基础上添加以下配置即可:
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-security</artifactId>
- </dependency>
- 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;
-
- /**
- * @ClassName : SecuritySecureConfig //类名
- * @Description : //描述
- * @Author : //作者
- * @Date: 2021-06-04 15:31 //时间
- */
- @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");
- }
-
- }
- spring:
- security:
- user:
- name: 'admin'
- password: 'admin'
- spring:
- boot:
- admin:
- client:
- username: 'admin'
- password: 'admin'
重新启动8090和8091项目
输入配置的用户名密码即可登录(此处是'admin')
admin支持邮件通知,配置如下:
(集成的邮箱以自己需求为主,此处以163邮箱为例)
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-mail</artifactId>
- </dependency>
- spring:
- mail:
- host: smtp.163.com
- username: 发送人@163.com
- password: 授权码
- boot:
- admin:
- notify:
- mail:
- enabled: true
- to: 接收人@xx.com
- from: 发送人@163.com
已经拿到邮箱授权码的可以跳过此步骤
打开浏览器进入https://mail.163.com/登录163邮箱,成功后
这里需要扫描一下二维码发送短信开启(个人吐槽:有点坑短信费)
发送成功后选择'我已发送',把红圈里的授权码复制下来替换掉上面yml中的'授权码'文字
然后修改发送人和接收人邮箱地址,这里要注意一下,username和from必须是相同的邮箱,to是接收人的邮箱
修改完成后重启8090项目
登录进入监控页面
此刻两个项目都正常运行,停止8091项目
启动8091项目
离线邮件
上线邮件
以上就是本篇的全部内容,下面附上github代码地址
1.https://codecentric.github.io/spring-boot-admin/current/#clustering-support
作者:想要飞翔的天使
链接:http://www.javaheidong.com/blog/article/222489/90f5f29831da010cfb59/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!