springcloud系列六 整合security

springcloud系列六 整合security

一 Eureka注册中心认证:

  Eureka自带了一个管理界面,如果不加密,所有人都可以进行访问这个地址,这样安全问题就来了,所以需要对其进行加密认证:

那么该如何进行整合呢:

1 在注册中心模块添加依赖:

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

2 yml文件配置:

spring:
  security:
    user:
      name: admin
      password: admin

3 启动服务再次登录尝试:

之前是谷歌登录,所以换了一个浏览器,需要再登录,

再次启动注册模块,就出现一堆错误:

那么这个问题怎么解决呢:

eureka:
  client:
    serviceUrl:
     defaultZone: http://admin:admin@127.0.0.1:8761/eureka/  #eureka注册中心地址

在注册服务上加上这个,名字,密码就可以了吗?

 

再启动还是报一堆错误,服务根本注册不进去:

 

 可以看到报错信息:

com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server

百度查询:

但是好像并没有解决啊:

开启认证了,但是没有禁用CSRF

新版本的spring-cloud2.0中: Spring Security默认开启了CSRF攻击防御

CSRF会将微服务的注册也给过滤了,虽然不会影响注册中心,但是其他客户端是注册不了的

解决方案:

关闭csrf攻击:

package com.cxy.config;

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;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;

/***
 * @ClassName: WebSecurityConfig
 * @Description:
 * @Auther: 陈绪友
 * @Date: 2019/1/2820:34
 * @version : V1.0
 */
@Configuration

public class WebSecurityConfig {
    @EnableWebSecurity
    public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests().anyRequest().authenticated().and().httpBasic().and().csrf().disable();
        }
    }
}

 

 

posted @ 2019-01-28 20:38 动手的程序员 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值