好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Spring Cloud 如何保证微服务内安全

一、简介

在微服务的架构下,我们需要把系统的业务划分成多个单一的微服务。每个微服务都会提供接口供其他微服务调用,在Dubbo中可以通过rmi、nio等实现,Spring Cloud中是通过http调用的。

但有些时候,我们只希望用户通过我们的网关调用微服务,不允许用户直接请求微服务。这时我们就可以借助Spring Security来保障安全。

二、使用步骤

2.1 在提供接口的微服务项目中配置Spring Security

1 首先在pom.xml引入Spring Security的相关配置,如下

?

1

2

3

4

< dependency >

     < groupId >org.springframework.boot</ groupId >

     < artifactId >spring-boot-starter-security</ artifactId >

</ dependency >

2 在qpplication.yml中配置账号密码,如下

?

1

2

3

4

5

6

security:

   basic:

     enabled: true

   user:

     name: sunbufu

     password: 123456

3 此时访问接口发现已经需要认证了。

输入正确的账号和密码后就可以访问了。

2.2在调用微服务项目中配置Feign的账号密码

1 在application.yml中配置账号密码

?

1

2

3

4

security:

   user:

     name: sunbufu

     password: 123456

2 添加Feign的配置文件

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

package com.sunbufu.config;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import feign.auth.BasicAuthRequestInterceptor;

@Configuration

public class FeignConfiguration {

     @Value ( "${security.user.name}" )

     private String userName;

     @Value ( "${security.user.password}" )

     private String passWord;

     @Bean

     public BasicAuthRequestInterceptor basicAuthRequestInterceptor(){

         return new BasicAuthRequestInterceptor(userName, passWord);

     }

}

3 这样完成后,就可以正常的访问了。

三、实例

git源码地址:https://github测试数据/sunbufu/sunbufu-cloud

下面是这4个工程的说明:

1. sunbufu-erueka:Eureka服务的工程

2. sunbufu-hello-face:服务接口的定义工程,其中包括定义微服务需要实现什么功能,其他微服务怎么调用,以及feign的配置

3. sunbufu-hello-impl:服务接口的实现工程,实现了sunbufu-hello-face定义的功能

4. sunbufu-hello-web:服务的网关工程,主要为了调用sunbufu-hello-face

Spring Cloud服务安全连接

Spring Cloud可以增加HTTP Basic认证来增加服务连接的安全性。

1、加入security启动器

在maven配置文件中加入Spring Boot的security启动器。

?

1

2

3

4

< dependency >

     < groupId >org.springframework.boot</ groupId >

     < artifactId >spring-boot-starter-security</ artifactId >

</ dependency >

这样,就开启对服务连接的安全保护,系统默认为生成一个用户名为]user]及一个随机密码,随机密码在服务启动的时候在日志中会打印出来。

2、自定义用户名密码

随机密码没什么实际意义,我们需要一个固定的连接用户名和密码。

在应用配置文件中加入以下配置即可。

?

1

2

3

4

security:

   user:

     name: admin

     password: admin123456

这样配置完后在连接这个服务的时候就会要求输入用户名和密码,如果认证失败会返回401错误。

?

1

2

3

4

5

6

7

{

     "timestamp" : 1502689874556 ,

     "status" : 401 ,

     "error" : "Unauthorized" ,

     "message" : "Bad credentials" ,

     "path" : "/test/save"

}

3、安全连接

1、注册中心安全连接

?

1

username:password @ipaddress

2、Feign申明式服务安全连接

?

1

2

3

@FeignClient (name = "SERVICE" , configuration = FeignAuthConfig. class )

public interface OrderService extends OrderAPI {

}

?

1

2

3

4

5

6

7

@Configuration

public class FeignAuthConfig {

     @Bean

     public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {

         return new BasicAuthRequestInterceptor( "admin" , "admin123456" );

     }

}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/sunbufu/article/details/79287340

查看更多关于Spring Cloud 如何保证微服务内安全的详细内容...

  阅读:32次