手把手教你解决升级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]
相关推荐
gwjcloud6 小时前
Kubernetes从入门到精通(高级篇)04
云原生·容器·kubernetes
阿里云云原生7 小时前
阿里云微服务引擎 MSE 及 API 网关 2026 年 4 月产品动态
微服务·云原生
阿里云云原生8 小时前
从“对话式编程”到“规格驱动”:民生银行企业级AI研发范式重构实践
云原生
苍煜9 小时前
现代生产级微服务+容器治理完整技术栈与架构方案详解(国内主流完整云原生微服务闭环架构)
微服务·云原生·架构
邵奈一10 小时前
OrbStack 环境下 Dify 启动报错完整解决方案教程:validating docker-compose.yaml
docker·容器·eureka
倔强的胖蚂蚁11 小时前
Transformer 大模型原理 完整入门指南
人工智能·深度学习·云原生·transformer
苍煜12 小时前
K8s 核心资源详解(Pod/Deployment/Service 实战)
云原生·容器·kubernetes
sbjdhjd13 小时前
企业级 Docker 镜像仓库建设与运维规范
linux·运维·docker·云原生·容器·eureka·开源
云达闲人14 小时前
搭建DevOps企业级仿真实验环境:010Kubernetes 单节点集群完整搭建指南
云原生·kubernetes·devops·devops 实验环境·k8s 集群·flannel 网络插件·kubernetes集群搭建
Jul1en_14 小时前
【SpringCloud】Eureka、Nacos 简单概念笔记
笔记·spring cloud·eureka