springCloud学习第二天---security

昨天简单的作了一个eureka注册中心的案例,我们有没有发现一个问题,我们的注册中心谁都可以注册,因为我们没有加入安全认证,下面我们引入

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

在配置文件appliction.yml中加入

spring:
  security:
    user:
      name: user
      password: 123

注意springboot2.0版本是这么配的,1.0版本的不是这样,可以自行百度,还有就是2.0版本后security会默认开启csrf验证,所以就需要我们去手动关闭,如下

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 SecurityConf extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        super.configure(http);
    }

}

访问注册中心,会看到认证界面

 需要认证后才能进入注册中心

接下来修改eureka-client的配置文件

server:
  port: 7777
eureka:
  client:
    service-url:
      defaultZone: http://user:123@localhost:7776/eureka/

spring:
  application:
    name: eureka-client

只需要在路径前加   用户名:密码@   就可以了

ok了,我们的安全认证就加上了

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值