Eureka Server 加上安全认证之后,服务无法注册

接前几篇

Spring Cloud 学习笔记 (一) 快速创建Eureka Server服务注册中心
Spring Cloud (二) 服务注册与发现
Spring cloud (三) 简单的安全认证

服务注册中心加了安全认证之后,然后我运行eureka client 。。。发现。。。。
这里写图片描述
报错了,注册不了。。。后来才发现,注册地址要改一下。。改成下面这样:

eureka:
  client:
    service-url:
      defaultZone: http://boy:[email protected]:8000/eureka/

在地址里加上用户名密码,然后运行还是报错,是因为新版本的security默认开启csrf了,关掉就好了,新建一个配置类,看下面:

package com.yezi.dreamspark.dreamsparkserver.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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;
import org.springframework.security.core.userdetails.UserDetailsService;

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
   http.csrf().disable(); //关闭csrf
    http.authorizeRequests().anyRequest().authenticated().and().httpBasic(); //开启认证
    }

}

然后运行,完美!!成功注册。
这里写图片描述

猜你喜欢

转载自blog.csdn.net/weixin_39913200/article/details/80845867
今日推荐