手把手教你解决升级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]
相关推荐
张忠琳9 小时前
【containerd 2.1.8】(Part 1)containerd 2.1.8 超深度源码分析 — 总体架构与模块全景
云原生·kubernetes·containerd
真实的菜11 小时前
微服务注册配置中心终极选型:2026指南
微服务·云原生·架构
与海boy18 小时前
docker compose minio
docker·容器·eureka
星辰徐哥18 小时前
云原生核心特性:容器化、微服务与DevOps的通俗解读
微服务·云原生·devops
武子康18 小时前
调查研究-167 Docker Compose 详解:从单容器到多服务编排的工程化入口
运维·docker·云原生·容器·kubernetes·k8s·docker-compose
heimeiyingwang19 小时前
【架构实战】分布式会话:从Session到JWT的演进
微服务·云原生·架构
DolphinScheduler社区1 天前
Apache DolphinScheduler 3.4.2 正式发布!新增 Amazon EMR Serverless 插件,增强监控与补数据能力
大数据·云原生·serverless·apache·海豚调度·版本发版
heimeiyingwang1 天前
【架构实战】注册中心选型:Nacos vs Eureka vs Consul
微服务·云原生·架构
java_cj1 天前
阅读 k8s 源码的准备工作
云原生·容器·kubernetes
鹤落晴春2 天前
【K8s】Pod调度、configMaps
云原生·容器·kubernetes