SpringCloud Eureka 设置账号密码(security )

Eureka server  导入 security 包

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

Eureka server 设置账号密码

spring:
  security: # security 账号密码
    user:
      name: ludb
      password: ludb123

设置连接Eureka server 端 的地址的账号密码

server:
  port: 8761  #端口号

eureka:
  client:
#    设置服务注册中心地址,指向另一个注册中心,以实现高可用
    serviceUrl:
      defaultZone: http://ludb:ludb123@localhost:8762/eureka/
server:
  port: 8762  #端口号

eureka:
  client:
#    设置服务注册中心地址,指向另一个注册中心,以实现高可用
    serviceUrl:
      defaultZone: http://ludb:ludb123@localhost:8761/eureka/

设置连接Eureka client 端 的地址的账号密码

server:
  port: 8081

eureka:

  client:
#    设置服务注册中心地址【向注册中心集群注册(注册两台是为了防止第一台宕机还可以挂在第2台上)】
    service-url:
      defaultZone: http://ludb:ludb123@localhost:8761/eureka/,http://ludb:ludb123@localhost:8762/eureka/

修改过虑CSRF

加入了安全认证模块后,默认会开启 CSRF 跨站脚本攻击

新版(Spring Cloud 2.0 以上)的security默认启用了csrf检验,要在eurekaServer端配置securitycsrf检验为false。(两种方法用一种就可以)

package com.example.eureakserver1.config;

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;

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);//访问eureka控制台和actuator控制台时能做安全控制
        //方法1:关闭csrf
        http.csrf().disable();
        //方法2:忽略/eureka/** 所有请求
        http.csrf().ignoringAntMatchers("/eureka/**");
    }
}

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值