Spring boot 客户端注册 Spring boot admin 失败(2.x 版本)

Posted

技术标签:

【中文标题】Spring boot 客户端注册 Spring boot admin 失败(2.x 版本)【英文标题】:Spring boot client fails to register Spring boot admin (version 2.x) 【发布时间】:2019-01-29 16:09:32 【问题描述】:

我正在运行启用了基本身份验证的 Spring Boot 应用程序和启用了 UI 安全性的 Spring Boot 管理员。

我的 Spring Boot 客户端无法注册到 Spring Boot 管理服务器。以下是日志:

2018-08-23 15:17:09.676 DEBUG 4992 --- [gistrationTask1] o.s.web.client.RestTemplate              : Created POST request for "http://localhost:9001/instances"
2018-08-23 15:17:09.699 DEBUG 4992 --- [gistrationTask1] o.s.web.client.RestTemplate              : Setting request Accept header to [application/json, application/*+json]
2018-08-23 15:17:09.724 DEBUG 4992 --- [gistrationTask1] o.s.web.client.RestTemplate              : Writing [Application(name=spring-boot-application, managementUrl=http://localhost:8001/relay/actuator, healthUrl=http://localhost:8001/relay/actuator/health, serviceUrl=http://localhost:8001/relay)] as "application/json" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@12c0c0b3]
2018-08-23 15:17:09.864 DEBUG 4992 --- [gistrationTask1] s.n.www.protocol.http.HttpURLConnection  : sun.net.www.MessageHeader@5953ec3b8 pairs: POST /instances HTTP/1.1: nullAccept: application/jsonContent-Type: application/jsonAuthorization: Basic YWRtaW46YWRtaW4=User-Agent: Java/1.8.0_161Host: localhost:9001Connection: keep-aliveContent-Length: 287
2018-08-23 15:17:10.124 DEBUG 4992 --- [gistrationTask1] s.n.www.protocol.http.HttpURLConnection  : sun.net.www.MessageHeader@126c0bea10 pairs: null: HTTP/1.1 403Cache-Control: no-cache, no-store, max-age=0, must-revalidatePragma: no-cacheExpires: 0X-Content-Type-Options: nosniffX-Frame-Options: DENYX-XSS-Protection: 1 ; mode=blockContent-Type: text/plainTransfer-Encoding: chunkedDate: Thu, 23 Aug 2018 09:47:10 GMT
2018-08-23 15:17:10.128 DEBUG 4992 --- [gistrationTask1] o.s.web.client.RestTemplate              : POST request for "http://localhost:9001/instances" resulted in 403 (null); invoking error handler
2018-08-23 15:17:10.138  WARN 4992 --- [gistrationTask1] d.c.b.a.c.r.ApplicationRegistrator       : Failed to register application as Application(name=spring-boot-application, managementUrl=http://localhost:8001/relay/actuator, healthUrl=http://localhost:8001/relay/actuator/health, serviceUrl=http://localhost:8001/relay) at spring-boot-admin ([http://localhost:9001/instances]): 403 null. Further attempts are logged on DEBUG level

下面是我来自管理服务器的 webSecurity 配置类

@SpringBootApplication
@EnableAdminServer
@EnableAutoConfiguration
@Configuration
public class SpringStarterAdminApplication 

    public static void main(String[] args) 
        SpringApplication.run(SpringStarterAdminApplication.class, args);
    


    // Added for spring boot security login ui for admin



        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 
            // @formatter:off
            SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
            successHandler.setTargetUrlParameter("redirectTo");
            successHandler.setDefaultTargetUrl(adminContextPath + "/");

            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()
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())  
                .ignoringAntMatchers(
                    "/instances",   
                    "/actuator/**"  
                );
            // @formatter:on
        
    

不确定是什么阻止了客户端注册到管理服务器。

【问题讨论】:

我也面临同样的问题。你有什么解决办法吗?? 【参考方案1】:

内部配置类也应该用@Configuration注解。

如果你将它重构为一个单独的类,那就更好了。

【讨论】:

以上是关于Spring boot 客户端注册 Spring boot admin 失败(2.x 版本)的主要内容,如果未能解决你的问题,请参考以下文章

带有 Spring Boot 的 Docker 和 Eureka 无法注册客户端

Spring Boot 项目如何对接注册中心(ZooKeeper)

没有 Spring-boot 的 Eureka 服务发现

基于Spring Boot 2.0.3的Spring Cloud Eureka Server与Client

Spring Cloud第十三篇 | Spring Boot Admin服务监控

springcloud Spring Boot mybatis分布式微服务云架构:服务提供与调用