前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Cloud 微服务(九)- 集成 Spring Boot Admin

Spring Cloud 微服务(九)- 集成 Spring Boot Admin

原创
作者头像
安宁
修改2020-07-27 10:08:48
1.9K0
修改2020-07-27 10:08:48
举报

本文简单介绍在项目中集成 spring-boot-admin

SBA(spring-boot-admin) 可简单理解为一个 UI 组件,提供 Endpoint 接口数据的界面展示。

1. 创建项目

创建 peacetrue-microservice-admin-server 项目,作为 eureka 客户端。然后添加依赖 implementation 'de.codecentric:spring-boot-admin-starter-server:2.3.0-SNAPSHOT',作为 admin 服务端。

2. 改造 Spring Security

Spring Security 默认的登陆页面如下:

默认的登陆页面
默认的登陆页面

Figure 1. 默认的登陆页面

参考 SBA官方文档 实现的 Reactive 版本:

WebFluxSecurityConfig

代码语言:javascript
复制
@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http,
                                                        AdminServerProperties adminServer) {
    http.authorizeExchange(exchanges -> exchanges
            .matchers(EndpointRequest.to(HealthEndpoint.class, InfoEndpoint.class)).permitAll()
            .pathMatchers(adminServer.path("/assets/**")).permitAll()
            .pathMatchers(adminServer.path("/login")).permitAll()
            .anyExchange().authenticated()
    );
    http.formLogin()
            .loginPage(adminServer.path("/login"))
            .authenticationSuccessHandler(new RedirectServerAuthenticationSuccessHandler(adminServer.path("/")));
    http.logout().logoutUrl(adminServer.path("/logout"));
    http.httpBasic(withDefaults());
    http.csrf().csrfTokenRepository(CookieServerCsrfTokenRepository.withHttpOnlyFalse())
            .requireCsrfProtectionMatcher(new AndServerWebExchangeMatcher(
                    CsrfWebFilter.DEFAULT_CSRF_MATCHER,
                    new NegatedServerWebExchangeMatcher(EndpointRequest.toAnyEndpoint()),
                    new NegatedServerWebExchangeMatcher(ServerWebExchangeMatchers.pathMatchers(HttpMethod.POST, adminServer.path("/instances"))),
                    new NegatedServerWebExchangeMatcher(ServerWebExchangeMatchers.pathMatchers(HttpMethod.DELETE, adminServer.path("/instances/*"))),
                    new NegatedServerWebExchangeMatcher(ServerWebExchangeMatchers.pathMatchers(adminServer.path("/actuator/*")))
            ));
    return http.build();
}

改造后,SBA 的登陆页面如下:

SBA登陆页面
SBA登陆页面

Figure 2. SBA登陆页面

登陆后会提示 CSRF Token has been associated to this client,原因是 CsrfWebFilter 配合 CookieServerCsrfTokenRepository 设置 Cookie 存在 BUG [1]。

忽略登陆的 CSRF 拦截后,可以正常登陆,但还是有其他功能受影响,所以直接禁用 CSRF:http.csrf().disable();

3. 效果展示

应用墙
应用墙

Figure 3. 应用墙

应用列表
应用列表

Figure 4. 应用列表

内存使用情况
内存使用情况

Figure 5. 内存使用情况

modify log leve
modify log leve

Figure 6. 修改日志级别

修改日志级别这个功能很好用。


1. https://github.com/spring-projects/spring-security/issues/5766

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 创建项目
  • 2. 改造 Spring Security
  • 3. 效果展示
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档