Spring Boot Admin 添加報警提醒和登錄驗證功能!
Spring Boot Admin(SBA)是一個開源的社區項目,用於管理和監控 Spring Boot 應用程式,它提供了詳細的健康資訊、記憶體資訊、JVM 系統和環境屬性、垃圾回收資訊、日誌設置和查看、定時任務查看、Spring Boot 快取查看和管理等功能。
SBA 監控概覽如下圖所示:
上一篇我們已經說了 SBA 的搭建和使用了,點擊訪問://mp.weixin.qq.com/s/cciU2u-LXnQHIrHN9uhVYA
然而上面的使用是無法滿足我們生產環境的要求的,生產環境至少還需要配置以下兩個功能:
- 被監控的 Spring Boot 項目的報警功能,因為我們不能時刻盯著 SBA 監控系統,但當系統出現問題時,我們又需要第一時間知道,因此報警提醒功能是必不可少的。
- 默認情況下 SBA 的使用是沒有許可權驗證的,也就是所有人知道了地址都可以正常使用,這不滿足生產系統的安全要求,所以用戶授權功能也是必不可少的。
接下來我們來看以上功能的具體實現。
1.添加報警提醒功能
報警提醒功能是基於郵箱實現的,當然也可以使用其他的提醒功能,如釘釘或飛書機器人提醒也是可以的,但郵箱報警功能的實現成本最低,所以本文我們就來看郵箱的報警提醒功能的具體實現。
1.1 添加郵件支援框架
在 SBA 的依賴文件 pom.xml 中添加以下配置:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
1.2 配置收、發郵箱資訊
在 SBA 的配置文件 application.properties 中添加以下收、發郵箱的配置:
# 配置發送郵箱
[email protected]
# 配置接收郵箱
[email protected]
# 配置郵箱 smtp 地址(qq 發送郵箱的固定 host 是 smtp.qq.com)
spring.mail.host=smtp.qq.com
# 配置郵箱授權碼(此處為授權碼,而非密碼,獲取授權碼本文下一步有說明)
spring.mail.password=xxxxxx
# 配置郵箱的賬戶名(這個是上面配置發送郵件的賬戶名)
[email protected]
1.2.1 開啟 SMTP 服務
SMTP 是一種提供可靠且有效的電子郵件傳輸的協議。發送的郵箱必要要開啟 SMTP 服務,否則就實現不了郵件的發送功能了。如果使用的是 QQ 郵箱參考以下配置,打開 QQ 郵箱,在郵箱的帳號設置中找到 IMAP/SMTP 服務並開啟它,如下圖所示:
1.2.2 生成授權碼
發送的郵箱要生成郵箱授權碼,以 QQ 郵箱為例,在郵箱的帳號設置中找到「生成授權碼」,點擊即可生成,如下圖所示:
1.3 郵件報警測試
經過以上配置之後,無需添加任何程式碼!!!無需添加任何程式碼!!!無需添加任何程式碼!!!就可以實現項目狀態改變的郵件提醒功能了。
我們來測試一下,關閉我本地被監控的 Spring Boot 項目,郵箱會收到項目離線資訊,如下圖所示:
當我把被監控的 Spring Boot 項目啟動之後,郵箱會收到伺服器啟動郵件,如下圖所示:
也就是說,當你配置好了收、發郵箱之後,Spring Boot Admin 會在被監控的項目停機或啟動時,自動發送郵件到接收提醒的郵箱了。
1.4 注意事項
報警功能注意事項有以下幾個:
- 發送郵件的郵箱必須開啟 SMTP 服務。
- 發送郵箱無需設置密碼,只需要為配置項「spring.mail.password」設置郵箱授權碼即可。
- 發送郵箱和接收郵箱可以是同一個郵箱地址。
- SBA 郵箱報警提醒功能無需添加任何程式碼,只需要添加相應的框架支援,然後再配置上正確的收、發郵箱即可。
1.5 配置多個報警通知郵箱
通常項目的報警功能,需要通知的是一群相關負責人,而不是一個人,比如可能會通知運維負責人、程式負責人,還有項目經理等,而 SBA 多人提醒郵箱的配置也很容易,只需要在 SBA 的配置文件中添加多個收件郵箱即可,多個郵箱間使用英文逗號隔開,如下配置所示:
# 配置接收郵箱
[email protected],[email protected]
2.訪問許可權設置
SBA 默認是沒有許可權驗證的,而生產環境一定要配置許可權驗證,我們這裡通過添加 Spring Security 框架來實現許可權攔截,具體實現如下。
2.1 添加 Security 框架支援
在 SBA 的依賴文件 pom.xml 中添加如下配置:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2.2 設置登錄賬戶
在 SBA 的配置文件 application.properties 中添加如下配置:
# 設置登錄用戶名、密碼和角色
spring.security.user.name=java666
spring.security.user.password=java666
spring.security.user.roles=SBA_ADMIN
2.3 許可權資源設置
接下來在 SBA 項目中,添加以下資源設置類,如下程式碼所示(直接複製到項目中即可使用):
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
@EnableWebSecurity
@Configuration(proxyBeanMethods = false)
public class AdminSecurityConfig extends WebSecurityConfigurerAdapter {
private final String adminContextPath;
public AdminSecurityConfig(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()
.antMatchers(adminContextPath + "/instances/**").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/**"
);
}
@Override
public void configure(WebSecurity web) {
web.ignoring().antMatchers("/actuator/**");
}
}
2.4 訪問 SBA
此時訪問 SBA 監控系統就需要輸入用戶名和密碼才能正常使用了,如下圖所示:
我們輸入 2.2 步驟中設置的用戶名和密碼即可登錄,如下圖所示:
點擊註銷就退出 SBA 系統了。
總結
SBA 報警提醒功能只需要添加郵件發送框架,配置正確的收、發郵件,無需添加任何程式碼就可以實現報警提醒功能了,而且報警提醒的郵箱可以配置多個。SBA 可通過添加 Spring Security 來實現用戶的許可權效驗。
是非審之於己,毀譽聽之於人,得失安之於數。
公眾號:Java面試真題解析