程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2023-06(2)

Spring Boot整合Admin

发布于2021-06-12 16:06     阅读(745)     评论(0)     点赞(14)     收藏(2)


什么是 Spring Boot Admin?

codecentric 的 Spring Boot Admin 是一个社区项目,用于管理和监控您的Spring Boot ®应用程序。应用程序向我们的 Spring Boot Admin Client 注册(通过 HTTP)或使用 Spring Cloud ®(例如 Eureka、Consul)发现。UI 只是一个位于 Spring Boot Actuator 端点之上的 Vue.js 应用程序。 

整合admin

版本

环境版本
Spring Boot2.4.6
JDK13
Idea2020.1

 Server

pom 

  1. <properties>
  2. <java.version>11</java.version>
  3. <spring-boot-admin.version>2.3.1</spring-boot-admin.version>
  4. </properties>
  5. <dependencies>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-web</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>de.codecentric</groupId>
  12. <artifactId>spring-boot-admin-starter-server</artifactId>
  13. </dependency>
  14. </dependencies>
  15. <dependencyManagement>
  16. <dependencies>
  17. <dependency>
  18. <groupId>de.codecentric</groupId>
  19. <artifactId>spring-boot-admin-dependencies</artifactId>
  20. <version>${spring-boot-admin.version}</version>
  21. <type>pom</type>
  22. <scope>import</scope>
  23. </dependency>
  24. </dependencies>
  25. </dependencyManagement>

 yml

  1. server:
  2. port: 8090
  3. servlet:
  4. contest-path: /
  5. spring:
  6. application:
  7. name: admin-server

启动类

  1. @SpringBootApplication
  2. @EnableAdminServer
  3. public class ServerApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(ServerApplication.class, args);
  6. }
  7. }

Client

pom

  1. <properties>
  2. <java.version>11</java.version>
  3. <spring-boot-admin.version>2.3.1</spring-boot-admin.version>
  4. </properties>
  5. <dependencies>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-web</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>de.codecentric</groupId>
  12. <artifactId>spring-boot-admin-starter-client</artifactId>
  13. </dependency>
  14. </dependencies>
  15. <dependencyManagement>
  16. <dependencies>
  17. <dependency>
  18. <groupId>de.codecentric</groupId>
  19. <artifactId>spring-boot-admin-dependencies</artifactId>
  20. <version>${spring-boot-admin.version}</version>
  21. <type>pom</type>
  22. <scope>import</scope>
  23. </dependency>
  24. </dependencies>
  25. </dependencyManagement>

yml

  1. server:
  2. port: 8091
  3. servlet:
  4. contest-path: /
  5. spring:
  6. application:
  7. name: admin-client
  8. boot:
  9. admin:
  10. client:
  11. url: http://localhost:8090
  12. management:
  13. endpoints:
  14. web:
  15. exposure:
  16. include: '*'
  17. endpoint:
  18. health:
  19. show-details: always

测试

分别启动8090和8091项目,下图说明Client成功在Server端注册

打开浏览器输入http://localhost:8090回车

 点击实例进入监控页面

功能不一一列举了,admin功能包括:

  • 显示 name/id 和版本号
  • 显示在线状态
  • Logging日志级别管理
  • JMX beans管理
  • Threads会话和线程管理
  • Trace应用请求跟踪
  • 应用运行参数信息,如:
  • Java 系统属性
  • Java 环境变量属性
  • 内存信息
  • Spring 环境属性

安全认证(security)

添加安全认证也很简单,需要在原来文件的基础上添加以下配置即可:

Server端

pom

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-security</artifactId>
  4. </dependency>

SecuritySecureConfig

  1. import de.codecentric.boot.admin.server.config.AdminServerProperties;
  2. import org.springframework.boot.autoconfigure.security.SecurityProperties;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.http.HttpMethod;
  5. import org.springframework.security.config.Customizer;
  6. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  7. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  8. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  9. import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
  10. import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
  11. import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
  12. import java.util.UUID;
  13. /**
  14. * @ClassName : SecuritySecureConfig //类名
  15. * @Description : //描述
  16. * @Author : //作者
  17. * @Date: 2021-06-04 15:31 //时间
  18. */
  19. @Configuration(proxyBeanMethods = false)
  20. public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
  21. private final AdminServerProperties adminServer;
  22. private final SecurityProperties security;
  23. public SecuritySecureConfig(AdminServerProperties adminServer, SecurityProperties security) {
  24. this.adminServer = adminServer;
  25. this.security = security;
  26. }
  27. @Override
  28. protected void configure(HttpSecurity http) throws Exception {
  29. SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
  30. successHandler.setTargetUrlParameter("redirectTo");
  31. successHandler.setDefaultTargetUrl(this.adminServer.path("/"));
  32. http.authorizeRequests(
  33. (authorizeRequests) -> authorizeRequests.antMatchers(this.adminServer.path("/assets/**")).permitAll()
  34. .antMatchers(this.adminServer.path("/actuator/info")).permitAll()
  35. .antMatchers(this.adminServer.path("/actuator/health")).permitAll()
  36. .antMatchers(this.adminServer.path("/login")).permitAll().anyRequest().authenticated()
  37. ).formLogin(
  38. (formLogin) -> formLogin.loginPage(this.adminServer.path("/login")).successHandler(successHandler).and()
  39. ).logout((logout) -> logout.logoutUrl(this.adminServer.path("/logout"))).httpBasic(Customizer.withDefaults())
  40. .csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
  41. .ignoringRequestMatchers(
  42. new AntPathRequestMatcher(this.adminServer.path("/instances"),
  43. HttpMethod.POST.toString()),
  44. new AntPathRequestMatcher(this.adminServer.path("/instances/*"),
  45. HttpMethod.DELETE.toString()),
  46. new AntPathRequestMatcher(this.adminServer.path("/actuator/**"))
  47. ))
  48. .rememberMe((rememberMe) -> rememberMe.key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600));
  49. }
  50. // Required to provide UserDetailsService for "remember functionality"
  51. @Override
  52. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  53. auth.inMemoryAuthentication().withUser(security.getUser().getName())
  54. .password("{noop}" + security.getUser().getPassword()).roles("USER");
  55. }
  56. }

yml

  1. spring:
  2. security:
  3. user:
  4. name: 'admin'
  5. password: 'admin'

Client端

yml

  1. spring:
  2. boot:
  3. admin:
  4. client:
  5. username: 'admin'
  6. password: 'admin'

测试

重新启动8090和8091项目

输入配置的用户名密码即可登录(此处是'admin')

邮件通知

admin支持邮件通知,配置如下:

(集成的邮箱以自己需求为主,此处以163邮箱为例)

Server端

pom

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-mail</artifactId>
  4. </dependency>

yml

  1. spring:
  2. mail:
  3. host: smtp.163.com
  4. username: 发送人@163.com
  5. password: 授权码
  6. boot:
  7. admin:
  8. notify:
  9. mail:
  10. enabled: true
  11. to: 接收人@xx.com
  12. from: 发送人@163.com

获取授权码

已经拿到邮箱授权码的可以跳过此步骤

打开浏览器进入https://mail.163.com/登录163邮箱,成功后

这里需要扫描一下二维码发送短信开启(个人吐槽:有点坑短信费)

发送成功后选择'我已发送',把红圈里的授权码复制下来替换掉上面yml中的'授权码'文字

然后修改发送人和接收人邮箱地址,这里要注意一下,username和from必须是相同的邮箱,to是接收人的邮箱

修改完成后重启8090项目

测试

登录进入监控页面

此刻两个项目都正常运行,停止8091项目

启动8091项目

离线邮件

上线邮件


以上就是本篇的全部内容,下面附上github代码地址

springboot-admin

参考资料

1.https://codecentric.github.io/spring-boot-admin/current/#clustering-support

2.https://blog.csdn.net/forezp/article/details/86105850



所属网站分类: 技术文章 > 博客

作者:想要飞翔的天使

链接:http://www.javaheidong.com/blog/article/222489/90f5f29831da010cfb59/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

14 0
收藏该文
已收藏

评论内容:(最多支持255个字符)