自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Spring Boot Admin 報(bào)警提醒和登錄驗(yàn)證功能實(shí)現(xiàn)!

開發(fā) 架構(gòu)
報(bào)警提醒功能是基于郵箱實(shí)現(xiàn)的,當(dāng)然也可以使用其他的提醒功能,如釘釘或飛書機(jī)器人提醒也是可以的,但郵箱報(bào)警功能的實(shí)現(xiàn)成本最低,所以本文我們就來看郵箱的報(bào)警提醒功能的具體實(shí)現(xiàn)。

Spring Boot Admin(SBA)是一個(gè)開源的社區(qū)項(xiàng)目,用于管理和監(jiān)控 Spring Boot 應(yīng)用程序,它提供了詳細(xì)的健康信息、內(nèi)存信息、JVM 系統(tǒng)和環(huán)境屬性、垃圾回收信息、日志設(shè)置和查看、定時(shí)任務(wù)查看、Spring Boot 緩存查看和管理等功能。SBA 監(jiān)控概覽如下圖所示:

上一篇我們已經(jīng)說了 SBA 的搭建和使用了,點(diǎn)擊訪問:https://mp.weixin.qq.com/s/cciU2u-LXnQHIrHN9uhVYA然而上面的使用是無法滿足我們生產(chǎn)環(huán)境的要求的,生產(chǎn)環(huán)境至少還需要配置以下兩個(gè)功能:

  • 被監(jiān)控的 Spring Boot 項(xiàng)目的報(bào)警功能,因?yàn)槲覀儾荒軙r(shí)刻盯著 SBA 監(jiān)控系統(tǒng),但當(dāng)系統(tǒng)出現(xiàn)問題時(shí),我們又需要第一時(shí)間知道,因此報(bào)警提醒功能是必不可少的。
  • 默認(rèn)情況下 SBA 的使用是沒有權(quán)限驗(yàn)證的,也就是所有人知道了地址都可以正常使用,這不滿足生產(chǎn)系統(tǒng)的安全要求,所以用戶授權(quán)功能也是必不可少的。

接下來我們來看以上功能的具體實(shí)現(xiàn)。

1.添加報(bào)警提醒功能

報(bào)警提醒功能是基于郵箱實(shí)現(xiàn)的,當(dāng)然也可以使用其他的提醒功能,如釘釘或飛書機(jī)器人提醒也是可以的,但郵箱報(bào)警功能的實(shí)現(xiàn)成本最低,所以本文我們就來看郵箱的報(bào)警提醒功能的具體實(shí)現(xiàn)。

1.1 添加郵件支持框架

在 SBA 的依賴文件 pom.xml 中添加以下配置:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

1.2 配置收、發(fā)郵箱信息

在 SBA 的配置文件 application.properties 中添加以下收、發(fā)郵箱的配置:

# 配置發(fā)送郵箱
spring.boot.admin.notify.mail.from=xxx@qq.com
# 配置接收郵箱
spring.boot.admin.notify.mail.to=xxx@qq.com
# 配置郵箱 smtp 地址(qq 發(fā)送郵箱的固定 host 是 smtp.qq.com
spring.mail.host=smtp.qq.com
# 配置郵箱授權(quán)碼(此處為授權(quán)碼,而非密碼,獲取授權(quán)碼本文下一步有說明)
spring.mail.password=xxxxxx
# 配置郵箱的賬戶名(這個(gè)是上面配置發(fā)送郵件的賬戶名)
spring.mail.username=xxx@qq.com

1.2.1 開啟 SMTP 服務(wù)

SMTP 是一種提供可靠且有效的電子郵件傳輸?shù)膮f(xié)議。發(fā)送的郵箱必要要開啟 SMTP 服務(wù),否則就實(shí)現(xiàn)不了郵件的發(fā)送功能了。如果使用的是 QQ 郵箱參考以下配置,打開 QQ 郵箱,在郵箱的賬號(hào)設(shè)置中找到 IMAP/SMTP 服務(wù)并開啟它,如下圖所示:

1.2.2 生成授權(quán)碼

發(fā)送的郵箱要生成郵箱授權(quán)碼,以 QQ 郵箱為例,在郵箱的賬號(hào)設(shè)置中找到“生成授權(quán)碼”,點(diǎn)擊即可生成,如下圖所示:

1.3郵件報(bào)警測(cè)試

經(jīng)過以上配置之后,**無需添加任何代碼!!!無需添加任何代碼!!!無需添加任何代碼!!!**就可以實(shí)現(xiàn)項(xiàng)目狀態(tài)改變的郵件提醒功能了。我們來測(cè)試一下,關(guān)閉我本地被監(jiān)控的 Spring Boot 項(xiàng)目,郵箱會(huì)收到項(xiàng)目離線信息,如下圖所示:

當(dāng)我把被監(jiān)控的 Spring Boot 項(xiàng)目啟動(dòng)之后,郵箱會(huì)收到服務(wù)器啟動(dòng)郵件,如下圖所示:

也就是說,當(dāng)你配置好了收、發(fā)郵箱之后,Spring Boot Admin 會(huì)在被監(jiān)控的項(xiàng)目停機(jī)或啟動(dòng)時(shí),自動(dòng)發(fā)送郵件到接收提醒的郵箱了。

1.4 注意事項(xiàng)報(bào)警功能注意事項(xiàng)有以下幾個(gè):

發(fā)送郵件的郵箱必須開啟 SMTP 服務(wù)。

發(fā)送郵箱無需設(shè)置密碼,只需要為配置項(xiàng)“spring.mail.password”設(shè)置郵箱授權(quán)碼即可。

發(fā)送郵箱和接收郵箱可以是同一個(gè)郵箱地址。

SBA 郵箱報(bào)警提醒功能無需添加任何代碼,只需要添加相應(yīng)的框架支持,然后再配置上正確的收、發(fā)郵箱即可。

1.5 配置多個(gè)報(bào)警通知郵箱

通常項(xiàng)目的報(bào)警功能,需要通知的是一群相關(guān)負(fù)責(zé)人,而不是一個(gè)人,比如可能會(huì)通知運(yùn)維負(fù)責(zé)人、程序負(fù)責(zé)人,還有項(xiàng)目經(jīng)理等,而 SBA 多人提醒郵箱的配置也很容易,只需要在 SBA 的配置文件中添加多個(gè)收件郵箱即可,多個(gè)郵箱間使用英文逗號(hào)隔開,如下配置所示:

# 配置接收郵箱
spring.boot.admin.notify.mail.to=xxx@qq.com,yyy@qq.com

2.訪問權(quán)限設(shè)置

SBA 默認(rèn)是沒有權(quán)限驗(yàn)證的,而生產(chǎn)環(huán)境一定要配置權(quán)限驗(yàn)證,我們這里通過添加 Spring Security 框架來實(shí)現(xiàn)權(quán)限攔截,具體實(shí)現(xiàn)如下。

2.1 添加 Security 框架支持

在 SBA 的依賴文件 pom.xml 中添加如下配置:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

2.2 設(shè)置登錄賬戶

在 SBA 的配置文件 application.properties 中添加如下配置:

# 設(shè)置登錄用戶名、密碼和角色
spring.security.user.name=java666
spring.security.user.password=java666
spring.security.user.roles=SBA_ADMIN

2.3 權(quán)限資源設(shè)置接下來在 SBA 項(xiàng)目中,添加以下資源設(shè)置類,如下代碼所示(直接復(fù)制到項(xiàng)目中即可使用):

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

此時(shí)訪問 SBA 監(jiān)控系統(tǒng)就需要輸入用戶名和密碼才能正常使用了,如下圖所示:

我們輸入 2.2 步驟中設(shè)置的用戶名和密碼即可登錄,如下圖所示:

點(diǎn)擊注銷就退出 SBA 系統(tǒng)了。

總結(jié)

SBA 報(bào)警提醒功能只需要添加郵件發(fā)送框架,配置正確的收、發(fā)郵件,無需添加任何代碼就可以實(shí)現(xiàn)報(bào)警提醒功能了,而且報(bào)警提醒的郵箱可以配置多個(gè)。SBA 可通過添加 Spring Security 來實(shí)現(xiàn)用戶的權(quán)限效驗(yàn)。


責(zé)任編輯:武曉燕 來源: Java中文社群
相關(guān)推薦

2020-12-01 08:32:12

Spring Boot

2022-02-08 17:07:54

Spring BooSpring Aop日志記錄

2011-04-01 17:01:11

Zabbix短信

2021-01-06 10:09:05

Spring Boothttps sslhttps

2023-07-27 08:53:44

2024-06-11 00:00:06

Spring考試系統(tǒng)

2022-07-28 06:50:52

微服務(wù)業(yè)務(wù)系統(tǒng)

2022-01-14 06:59:39

開源Spring BootSBA

2024-11-01 10:40:32

2024-02-27 08:27:18

元素拖拽Vue3拼圖驗(yàn)證

2024-01-12 18:26:44

2020-06-30 07:58:39

微服務(wù)Spring BootCloud

2021-07-13 14:05:37

單點(diǎn)登錄頁面

2009-08-03 17:31:26

.NET驗(yàn)證控件

2024-08-05 09:51:00

2025-02-17 00:00:45

接口支付寶沙箱

2021-04-26 08:54:17

Spring BootSecurity防重登錄

2010-06-01 16:25:24

Zabbix報(bào)警

2021-09-15 09:02:20

Spring 6Spring BootJava

2021-09-03 06:46:34

Spring 6pring Boot 項(xiàng)目
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)