Spring Boot Admin 監(jiān)控平臺
背景:Spring Boot Admin 監(jiān)控平臺不添加鑒權(quán)就直接訪問的話,是非常不安全的。所以在生產(chǎn)環(huán)境中使用時,需要添加鑒權(quán),只有通過鑒權(quán)后才能監(jiān)控客戶端服務(wù)。本文整合Spring Security進(jìn)行實(shí)現(xiàn)。
pom依賴
<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>
<version>2.6.11</version>
</dependency>
<!--alibaba-nacos-discovery(阿里注冊中心discovery)-->
<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-security</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!--Spring Boot 相關(guān)依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--Spring Cloud 相關(guān)依賴-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2020.0.5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--Spring Cloud Alibaba 相關(guān)依賴-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2021.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
yml配置
server:
port: 18000
spring:
application:
name: admin-server
security:
user:
name: admin
password: admin
cloud:
nacos:
discovery:
enabled: true
server-addr: 127.0.0.1:8848
group: admin #指定group
namespace: public
service: ${spring.application.name}
啟動類@EnableAdminServer
package com.admin;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@EnableAdminServer
@SpringBootApplication
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServerApplication.class,args);
}
}
安全配置類:SecuritySecureConfig.java
package com.admin.config;
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.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
@Configuration(proxyBeanMethods = false)
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
private final String adminContextPath;
public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter( "redirectTo" );
http.authorizeRequests()
.antMatchers( adminContextPath + "/assets/**" ).permitAll()
.antMatchers( adminContextPath + "/login" ).permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage( adminContextPath + "/login" ).successHandler( successHandler ).and()
.logout().logoutUrl( adminContextPath + "/logout" ).and()
.httpBasic().and()
.csrf().disable();
}
}
服務(wù)啟動成功后,訪問鏈接:http://127.0.0.1:18000。需要先進(jìn)行登錄(admin-admin),才能進(jìn)入控制臺頁面。
客戶端服務(wù)
背景:客戶端服務(wù)的檢查接口(/actuator/**)默認(rèn)可以直接通過接口調(diào)用,是非常不安全的。所以在生產(chǎn)環(huán)境中使用時,可添加鑒權(quán)功能提升安全性。
pom依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.6.11</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.14</version>
<scope>provided</scope>
</dependency>
<!-- alibaba-nacos-discovery(阿里注冊中心discovery)-->
<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-security</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!--Spring Boot 相關(guān)依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--Spring Cloud 相關(guān)依賴-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2020.0.5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--Spring Cloud Alibaba 相關(guān)依賴-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2021.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
yml配置(通過Url注冊)
spring:
security:
user:
name: user
password: user
application:
name: admin-order
# spring boot admin
boot:
admin:
client:
url: http://127.0.0.1:18000
username: admin
password: admin
instance:
prefer-ip: true
name: admin-order
# 這個name與password用于在注冊到管理端時,使管理端有權(quán)限獲取客戶端端點(diǎn)數(shù)據(jù)
metadata:
user.name: ${spring.security.user.name}
user.password: ${spring.security.user.password}
server:
port: 18001
servlet:
context-path: /order
# endpoints config
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: always
logging:
# 只有配置了日志文件,才能被監(jiān)控收集
file:
name: logs/${spring.application.name}/${spring.application.name}.log
yml配置(通過注冊中心注冊)
spring:
security:
user:
name: user
password: user
application:
name: admin-order
cloud:
nacos:
discovery:
metadata:
# 為服務(wù)實(shí)例添加一個名為“user.name”的元數(shù)據(jù)項(xiàng),并將其值設(shè)置為指定的服務(wù)用戶名。這個用戶名通常用于進(jìn)行鑒權(quán),以確保只有授權(quán)的用戶才能訪問該服務(wù)。
user.name: ${spring.security.user.name}
user.password: ${spring.security.user.password}
management:
# 表示Actuator端點(diǎn)的上下文路徑。具體地說,這個屬性的作用是將Actuator端點(diǎn)的上下文路徑設(shè)置為${server.servlet.context-path}/actuator
context-path: ${server.servlet.context-path}/actuator
enabled: true
server-addr: 127.0.0.1:8848
group: admin #指定group
namespace: public
service: ${spring.application.name}
server:
port: 18001
servlet:
context-path: /order
# endpoints config
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: always
logging:
# 只有配置了日志文件,才能被監(jiān)控收集
file:
name: logs/${spring.application.name}/${spring.application.name}.log
啟動類
package com.admin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@Slf4j
@EnableDiscoveryClient
@SpringBootApplication
public class AdminOrderApp {
public static void main(String[] args) {
SpringApplication.run(AdminOrderApp.class, args);
}
}
服務(wù)啟動成功后,訪問監(jiān)控平臺,就能監(jiān)控admin-order服務(wù)了。文章來源:http://www.zghlxwxcb.cn/news/detail-603001.html
注意:如果監(jiān)控平臺上沒有看見客戶端服務(wù),則需要重啟Spring Boot Admin 監(jiān)控服務(wù)文章來源地址http://www.zghlxwxcb.cn/news/detail-603001.html
到了這里,關(guān)于【Spring Boot Admin】使用(整合Spring Security服務(wù),添加鑒權(quán))的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!