原文链接:https://www.jianshu.com/p/6eb28c2545fb
官网仓库 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; proxy\_set\_header X-Forwarded-Host \$host; proxy\_http\_version 1.1; proxy\_set\_header X-Forwarded-Proto https; proxy\_set\_header Upgrade \$http\_upgrade; 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: 刚配置服务端地址
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
作者:昵称违规
链接:https://www.jianshu.com/p/6eb28c2545fb
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。