手把手教你解决升级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]
相关推荐
last demo2 小时前
Docker-compose和图形界面管理
docker·容器·eureka
Bonnie3734 小时前
云边端一体化解析-什么是云边端,为何能成为AI基础设施核心
人工智能·程序人生·云原生·个人开发
江畔何人初4 小时前
Docker、containerd、CRI、shim 之间的关系
运维·docker·云原生·容器·kubernetes
2401_891655817 小时前
Git + 云原生:如何管理K8s配置版本?
git·云原生·kubernetes
Volunteer Technology9 小时前
zookeeper基础应用与实战二
分布式·zookeeper·云原生
不吃香菜kkk、10 小时前
夜莺n9e+监控K8s集群+自定义监控页面
运维·云原生·云计算
匀泪10 小时前
云原生(docker私有仓库)
云原生
vpk11210 小时前
Docker Compose 常用命令大全
docker·容器·eureka
匀泪11 小时前
云原生(docker网络)
docker·云原生·容器
飞火流星0202711 小时前
常见的k8s平台功能对比、界面一览及KubeSphere安装、Rancher‌安装
云原生·容器·kubernetes·主流k8s平台·主流k8s平台功能对比·k8s在线安装·k8s离线安装