手把手教你解决升级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]
相关推荐
源代码•宸17 分钟前
前置准备:定时微服务有什么价值
经验分享·后端·微服务·云原生·架构
IT大白鼠1 小时前
Kubernetes 核心资源ReplicaSet 控制器详解
云原生·容器·kubernetes
池以遇2 小时前
云原生——k8s中的微服务
微服务·云原生·kubernetes
分布式存储与RustFS2 小时前
用 RustFS 原生 CLI `rc` 管对象存储:从 alias 到 cp 的完整玩法
云原生·开源·对象存储·分布式存储·s3·rustfs·性能基准
分布式存储与RustFS2 小时前
用 RPM/DEB 包在 Linux 上安装 RustFS:生产落地第一步
云原生·开源·对象存储·分布式存储·s3·rustfs·性能基准
UseLessQQ4 小时前
云原生 kubernetes 中的service
云原生·容器·kubernetes
大大大大晴天️4 小时前
把湖仓一体放进 K8s:Iceberg、Hudi、Paimon 如何重塑云原生大数据架构
大数据·云原生·kubernetes
智购科技无人售货机工厂4 小时前
2026自动售货机云端API设计规范:从RESTful到GraphQL的接口演进~YH
android·人工智能·驱动开发·单片机·云原生·pandas·设计规范
雨辰AI21 小时前
信创数仓分层建模|国产数据库 ODS/DWD/DWS 分层落地规范(金仓 / 达梦 / 高斯适配)
数据库·云原生·政务
阿里云云原生21 小时前
实验:回测、离线实验平台与题目级 Rubric丨AgentLoop 数据飞轮实践(四)
云原生·agent