SpringCloud确保服务只能通过gateway转发访问禁止直接调用接口访问_第1页
SpringCloud确保服务只能通过gateway转发访问禁止直接调用接口访问_第2页
SpringCloud确保服务只能通过gateway转发访问禁止直接调用接口访问_第3页
SpringCloud确保服务只能通过gateway转发访问禁止直接调用接口访问_第4页
SpringCloud确保服务只能通过gateway转发访问禁止直接调用接口访问_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

SpringCloud确保服务只能通过gateway转发访问,禁止直接调用接口访问前言在微服务体系架构中,网关承担着重要的角色,在网关中可以添加各种过滤器,过滤请求,保证请求参数安全,限流等等。如果请求绕过了网关,那就等于绕过了重重关卡,直捣黄龙,所以,在分布式架构中,我们需要有一定的防范,来确保各个服务相互之间安全调用。正文思路1、在网关中给所有请求加上一个请求密钥2、在服务添加过滤器,验证密钥首先在网关服务(gateway)中添加过滤器,给放行的请求头上加上判断package

com.hpsyche.hpsychegatewayserver.filter;

import

com.hpsyche.hpsychegatewayserver.utils.RedisUtil;

import

org.springframework.beans.factory.annotation.Autowired;

import

org.springframework.cloud.gateway.filter.GatewayFilterChain;

import

org.springframework.cloud.gateway.filter.GlobalFilter;

import

org.springframework.http.HttpStatus;

import

org.springframework.http.server.reactive.ServerHttpRequest;

import

org.springframework.util.StringUtils;

import

org.springframework.web.server.ServerWebExchange;

import

reactor.core.publisher.Mono;

/**

*

@author

Hpsyche

*/

public

class

TokenFilter

implements

GlobalFilter

{

@Autowired

private

RedisUtil

redisUtil;

@Override

public

Mono

filter(ServerWebExchange

exchange,

GatewayFilterChain

chain)

{

//token(用户身份)判断

................

ServerHttpRequest

req

=

exchange.getRequest().mutate()

.header("from",

"gateway").build();

return

chain.filter(exchange.mutate().request(req.mutate().build()).build());

}

}如果考虑到安全性的话,这里header的值也可以使用UUID,并保存至redis中,在其他服务中通过redis读取判断身份。搜索后端架构师公众号回复“架构整洁”,送你一份惊喜礼包。在主服务,如auth授权服务上,添加全局拦截器,判断header上是否有对应的value,是则否则,反之拦截。package

erceptor;

import

lombok.extern.slf4j.Slf4j;

import

mons.lang.StringUtils;

import

org.springframework.web.servlet.HandlerInterceptor;

import

javax.servlet.http.HttpServletRequest;

import

javax.servlet.http.HttpServletResponse;

import

java.io.PrintWriter;

/**

*

@author

Hpsyche

*/

@Slf4j

public

class

GlobalInterceptor

implements

HandlerInterceptor

{

@Override

public

boolean

preHandle(HttpServletRequest

request,

HttpServletResponse

response,

Object

obj)

throws

Exception

{

String

secretKey

=

request.getHeader("from");

if(!StringUtils.isNotBlank(secretKey)||secretKey.equals("gateway"){

response.setContentType("application/json;

charset=utf-8");

PrintWriter

writer

=

response.getWriter();

writer.write("error");

return

false;

}

return

true;

}

}此时,绕过网关,直接访问auth服务的接口会返回error,即必须通过网关,我们的任务看似完成了。问题在项目中,发现了feign远程调用会失败,因为在fegin中调用不通过gateway,缺少from的请求头,会被拦截器拦截,导致调用失败。解决方案在auth-client(服务暴露方)添加请求头,在供其他服务调用时添加header。package

com.hpsyche.hpsycheauthclient.config;

import

feign.RequestInterceptor;

import

feign.RequestTemplate;

import

org.springframework.context.annotation.Configuration;

/**

*

Feign调用的时候添加请求头from

*/

@Configuration

public

class

FeignConfiguration

implements

RequestInterceptor

{

@Override

public

void

apply(RequestTemplate

requestTemplate)

{;

requestTemplate.header("from",

"gateway");

}

}同时,需要在service接口中添加configuration配置:package

com.hpsyche.hpsycheauthclient.api;

import

com.hpsyche.hpsychecommonscommon.vo.BaseResponse;

import

com.hpsyche.hpsycheauthclient.config.FeignConfiguration;

import

org.springframework.cloud.openfeign.FeignClient;

import

org.springframework.web.bind.annotation.PostMapping;

import

org.springframework.web.bind.annotation.RequestParam;

/**

*

@author

Hpsyche

*/

@FeignClient(value

=

"hpsyche-auth-server",configuration

=

FeignConfiguration.class)

public

interface

AuthService

{

@PostMapping("/auth/verifyUser")

BaseResponse

verifyUser(

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论