手把手教你解决升级SpringBoot2.X后无法向eureka注册服务的问题详解

前言

今天对蘑菇博客的springboot和springcloud的版本进行升级,在升级后发现挺多地方需要更改的

首先是yml配置文件里面的security已经更改了,由之前的配置

复制代码
security:
  basic:
    enabled: true
  user:
    name: user
    password: password123

更改成下面这样的配置

复制代码
spring:
  security:
    user:
      name: user
      password: password123

然后,我们需要引入的eureka依赖,也由原来的

复制代码
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>

变成了下面的

复制代码
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

在启动eureka之前,我们还需要添加一个配置文件:WebSecurityConfig

复制代码
package com.moxi.mogublog.eureka.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;

/**
 * WebSecurityConfig
 *
 * @author: 陌溪
 * @create: 2019-12-21-19:20
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    /**
     *
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // Configure HttpSecurity as needed (e.g. enable http basic).
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
        http.csrf().disable();
        // 如果是form方式,不能使用url格式登录
        http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
    }

}

因为springboot 2.x版本,将下列的yml配置给废弃了

复制代码
#高版本的丢弃了
security:
 basic:
  enabled: true

所以我们需要通过配置类的方式,来启动安全验证,如果不配置的话,就会出现下面的问题

复制代码
javax.ws.rs.WebApplicationException: com.fasterxml.jackson.databind.exc.MismatchedInputException: Root name 'timestamp' does not match expected ('instance') for type [simple type, class com.netflix.appinfo.InstanceInfo]
相关推荐
努力搬砖的咸鱼15 小时前
Label 与 Selector:Kubernetes 资源选择的核心机制
微服务·云原生·容器·架构·kubernetes
IT大师兄吖1 天前
Qwen3 ASR 流式转写 Docker 懒人整合包
docker·容器·eureka
立莹Sir1 天前
SaaS多租户资源隔离的云原生解决方案:在不单独部署的情况下实现租户级资源保障
云原生
rockmelodies1 天前
用 Python 实现 Docker 镜像批量推送(带进度条)
python·docker·eureka
人道领域1 天前
GPT-5架构泄露?Kubernetes 1.31发布与Rust重构浪潮下的云原生之变
gpt·云原生·架构
刘~浪地球1 天前
云原生与容器--Service Mesh (Istio) 入门实战
云原生·istio·service_mesh
倔强的胖蚂蚁1 天前
Gemma4 优势与 Ollama 更新
运维·云原生
立莹Sir1 天前
【架构图解+实战配置】SaaS多租户资源隔离的云原生完整方案
云原生·架构
刘~浪地球1 天前
云原生与容器--CI/CD 流水线设计实践
ci/cd·云原生
SilentSamsara1 天前
Linux 管道与重定向:命令行精髓的结构性解析
linux·运维·服务器·c++·云原生