springboot 監控actuator+admin+security(單機版)

actuaotr是spring boot項目中非常強大的一個功能,有助於對應用程序進行監控和管理。

Spring Boot Admin 用於監控基於 Spring Boot 的應用,它是在 Spring Boot Actuator 的基礎上提供簡潔的可視化 WEB UI。

一個能夠爲基於Spring的企業應用系統提供聲明式的安全訪問控制解決方式的安全框架

使用步驟

1引入POM依賴

	<!-- 服務端:帶UI界面 -->
		<dependency>
			<groupId>de.codecentric</groupId>
			<artifactId>spring-boot-admin-starter-server</artifactId>
			<version>2.0.0</version>
		</dependency>
		<!-- 客戶端包 -->
		<dependency>
			<groupId>de.codecentric</groupId>
			<artifactId>spring-boot-admin-starter-client</artifactId>
			<version>2.0.0</version>
		</dependency>
		<!-- 安全認證 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>
		<!-- 端點 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>

		<!-- 在管理界面中與 JMX-beans 進行交互所需要被依賴的 JAR -->
		<dependency>
			<groupId>org.jolokia</groupId>
			<artifactId>jolokia-core</artifactId>
		</dependency>

2在啓動類上添加註解

@EnableAdminServer
@SpringBootApplication
public class SpringBootActuatorAdminApplication {
}

3在啓動類中加入security配置 

/**
	 * dev 環境加載
	 */
	@Profile("dev")
	@Configuration
	public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
		@Override
		protected void configure(HttpSecurity http) throws Exception {
			http.authorizeRequests().anyRequest().permitAll()
					.and().csrf().disable();
		}
	}

	/**
	 * prod 環境加載
	 */
	@Profile("prod")
	@Configuration
	public static 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();
		}
	}

4配置文件

application.properties

# 描述信息
[email protected]@
[email protected]@
# 選擇激活對應環境的配置,如果是dev則代表不用認證就能訪問監控頁,prod代表需要認證
spring.profiles.active=prod

# 加載所有的端點/默認只加載了 info / health
management.endpoints.web.exposure.include=*
# 比較重要,默認 /actuator spring-boot-admin 掃描不到
management.endpoints.web.base-path=/
management.endpoint.health.show-details=always

# 可以關閉制定的端點
management.endpoint.shutdown.enabled=false

# 日誌文件
logging.file=./target/admin-server.log

#將服務端自己註冊進去
spring.boot.admin.client.url=http://localhost:8080

application-prod.properties

#spring.security.user
#spring.boot.admin.client.username
#spring.boot.admin.client.instance.metadata
#三者用戶名密碼要一致,否則無法通過安全驗證

# 登陸所需的賬號密碼
spring.security.user.name=admin
spring.security.user.password=admin
# 便於客戶端可以在受保護的服務器上註冊api
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=admin
# 便服務器可以訪問受保護的客戶端端點
spring.boot.admin.client.instance.metadata.user.name=admin
spring.boot.admin.client.instance.metadata.user.password=admin

經過以上配置就能實現基於Springboot項目的監控。

訪問方式 直接輸入IP地址端口號即可 例:http://localhost:8080

注意事項

使用prod環境(spring.profiles.active=prod)需要輸入用戶名密碼才能登陸 

用戶名密碼配置方式,可以自行更改

spring.security.user.name=admin
spring.security.user.password=admin

項目源碼: https://github.com/houyongkang/spring-boot-actuator-admin.git

官方admin配置介紹 http://codecentric.github.io/spring-boot-admin/2.0.0/#_ui

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章