如何使注册中心开启密码认证?

如何使注册中心开启密码认证?

完成:第一遍

1. 如何使注册中心开启密码认证?

步骤一:在Eureka Server的pom.xml中先添加security依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

步骤二:修改配置文件添加spring.security.user的
name:adbycool
password:123456

server:
  port: 8001
spring:
  application:
    name: eureka
  security:
    user:
      name: adbycool
      password: 123456
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    server-url:
      defaultZone: http://localhost:8001/eureka
  

步骤三:在src\main\java\com\itzhiya\eureka\server下创建一个配置文件类SecurityConfig
需要继承WebSecurityConfigurerAdapter
重写configure()方法

package com.itzhiya.eureka.server;

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.stereotype.Component;

@Component
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
    //首先关闭  http.csrf().disable();
        http.csrf().disable();
  
        //支持httpBasic
               http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
       
    }
}

保存,重启服务器
访问:localhost:8001

2.当注册中心Eureka Server开启密码认证,Eureka Client如何在配置文件中添加注册中心的账户和密码信息?

Eureka Client原来的配置文件application.yml

server:
  port: 8002
spring:
  application:
    name: buildingservice
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8001/eureka

Eureka Client添加账户和密码,即更改后的的配置文件application.yml

server:
  port: 8002
spring:
  application:
    name: buildingservice
eureka:
  client:
    service-url:
      defaultZone: http://adbycool:123456@localhost:8001/eureka
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值