Springboot admin 搭建過程包含nginx配置

官網倉庫https://github.com/codecentric/spring-boot-admin

官方文檔https://codecentric.github.io/spring-boot-admin/2.7.2/

使用版本 2.7.1? 非微服務項目


一、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 配置參考官網

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

配置登錄用戶

使用企業微信通知

web版登錄地址:https://work.exmail.qq.com/ 獲取郵件密碼




生成密碼

3、security 配置 參考官網配置

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; # 轉 發 規 則? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

? ? ? ? proxy_set_header Host $proxy_host; # 修 改 轉 發 請 求 頭 , 讓 8080端 口 的 應 用 可 以 受 到 真 實 的 請 求? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

? ? ? ? proxy_set_header X-Real-IP $remote_addr;? ? ? ?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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: 剛配置服務端地址

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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。