Eureka加密认证

当我们不配置Eureka账号密码是,只要输入地址即可访问,下面记录如何对Eureka的服务中心进行加密
加密方法比较简单,首先是引入jar
pom.xml文件增加如下配置

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

application.yml配置如下:

spring:
  application:
    name: eureka
  security:
    user:
      name: admin
      password: pass
eureka:
  client:
    service-url:
      defaultZone: http://admin:pass@localhost:8761/eureka/,http://admin:pass@localhost:8762/eureka/

这里dafaultZone的地址中将用户名密码拼入,否则将无法注册,同样,向这个Eureka注册的微服务也需要将用户名密码拼入,到这一步加密成功,访问http:localhost:8761就会显示输入用户名密码,但是输入用户名密码后,还是不能访问到Eureka服务中心,这是因为被拦截了,需要增加如下配置,将这个拦截的地址忽略掉

WebSecurityConfig.java

package com.example.eurekadiscovery;

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;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // /eureka路径忽略csrf
        http.csrf().ignoringAntMatchers("/eureka/**");
        super.configure(http);
    }
}

加入该文件后再启动,访问localhost:8761,输入用户名密码后即可正常访问。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值