官網(wǎng)倉庫https://github.com/codecentric/spring-boot-admin
官方文檔https://codecentric.github.io/spring-boot-admin/2.7.2/
使用版本 2.7.1? 非微服務(wù)項目
一披摄、Server端
1拓春、新建項目引入依賴稠屠,我用的gradle聂抢,pom也差不多
```
implementation'org.springframework.boot:spring-boot-starter-undertow'
implementation'org.springframework.boot:spring-boot-starter-security'
implementation'org.springframework.boot:spring-boot-starter-mail'
implementation"de.codecentric:spring-boot-admin-starter-server:${property('springboot-admin.version')}"
```
2由蘑、配置yml 配置參考官網(wǎng)
https://codecentric.github.io/spring-boot-admin/#set-up-admin-server
forward-headers-strategy: native
use-forward-headers: true
這兩個配置最好加上,不然后面nginx訪問會失敗
spring:
security:
user:
name: admin
password: admin
配置登錄用戶
使用企業(yè)微信通知
web版登錄地址:https://work.exmail.qq.com/ 獲取郵件密碼
3状您、security 配置 參考官網(wǎng)配置
https://codecentric.github.io/spring-boot-admin/#_securing_spring_boot_admin_server
新建配置類例驹,注意springboot 2.7以后WebSecurityConfigurerAdapter已棄用,官方文檔是2.7之前的
@Configuration(proxyBeanMethods =false)
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled =true)
@RequiredArgsConstructor
public class WebSecurityConfiguration {
private final AdminServerPropertiesadminServer;
? ? @Bean
? ? SecurityFilterChainfilterChain(HttpSecurity http)throws Exception {
SavedRequestAwareAuthenticationSuccessHandler successHandler =new SavedRequestAwareAuthenticationSuccessHandler();
? ? ? ? successHandler.setTargetUrlParameter("redirectTo");
? ? ? ? successHandler.setDefaultTargetUrl(this.adminServer.path("/"));
? ? ? return http.authorizeRequests(
(authorizeRequests) -> authorizeRequests.antMatchers(this.adminServer.path("/assets/**")).permitAll()
.antMatchers(this.adminServer.path("/actuator/info")).permitAll()
.antMatchers(this.adminServer.path("/actuator/health")).permitAll()
.antMatchers(this.adminServer.path("/login")).permitAll().anyRequest().authenticated()
).formLogin(
(formLogin) -> formLogin.loginPage(this.adminServer.path("/login")).successHandler(successHandler).and()
).logout((logout) -> logout.logoutUrl(this.adminServer.path("/logout"))).httpBasic(Customizer.withDefaults())
.csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.ignoringRequestMatchers(
new AntPathRequestMatcher(this.adminServer.path("/instances"),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? HttpMethod.POST.toString()),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? new AntPathRequestMatcher(this.adminServer.path("/instances/*"),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? HttpMethod.DELETE.toString()),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? new AntPathRequestMatcher(this.adminServer.path("/actuator/**"))
))
.rememberMe((rememberMe) -> rememberMe.key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600)).build();
? ? }
@Bean
? ? WebSecurityCustomizerwebSecurityCustomizer() {
return web -> web.ignoring().antMatchers("/favicon.ico", "/public/**");
? ? }
}
application類注解
@SpringBootApplication
@EnableAdminServer
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
? ? }
}
至此server端就配置完成库继。
二箩艺、Server端Nginx配置
location / {
? ? ? ? proxy_pass? http://ip:8899; # 轉(zhuǎn) 發(fā) 規(guī) 則? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? proxy_set_header Host $proxy_host; # 修 改 轉(zhuǎn) 發(fā) 請 求 頭 , 讓 8080端 口 的 應(yīng) 用 可 以 受 到 真 實 的 請 求? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?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? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? proxy_set_header X-Forwarded-Port $server_port;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? }? ? ? ?
我一直卡在nginx這里宪萄,配置出來的總是訪問ui會報錯
注意幾個X-Forwarded的配置就行了艺谆,我也是參考了GitHub 的issue才解決的問題
還有上面配置?public-url
https://github.com/codecentric/spring-boot-admin/issues/1770
https://github.com/codecentric/spring-boot-admin/issues/1496
三、客戶端
只需在 yml 配置即可
spring.boot.admin.client.url: 剛配置服務(wù)端地址
spring.boot.admin.client.username: admin
?spring.boot.admin.client.password: admin
?spring.boot.admin.client.instance.prefer-ip: true
暴露端點
management:
? endpoints:
? ? web:
? ? ? exposure:
? ? ? ? include: "*"
? endpoint:
? ? health:
? ? ?show-details: ALWAYS
? ? logfile:
? ? ? external-file: ./logs/${spring.application.name}.log? ?# 日志訪問
搞定
配圖
-- The End