SpringCloud-路由⽹关Gateway⾃定义GatewayFilterFactory 1.概述
上⽂我们讲述了spring cloud gateway提供了很多内置的过滤器,但有些时候为了满⾜个性需求场景,我们需要⾃定义⾃⼰的过滤器,这时我们可以通过⾃定义GatewayFilterFactory来实现。
对于⾃定义的factory,我们可以选择去实现接⼝或继承已有的抽象类,相关的接⼝是GatewayFilterFactory,⽽springboot默认帮我们实现的抽象类是AbstractGatewayFilterFactory这个类。
2.⾃定义GatewayFilterFactory
代码⽰例如下:
package com.faw.btims.gateway.filter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.faw.btims.gateway.utils.RedisUtil;
import com.faw.btims.gateway.utils.ServletHttpHelper;
springboot aop
import stant.CommonConstants;
import domain.AjaxResult;
import ption.ValidateCodeException;
import org.apachemons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import io.buffer.DataBuffer;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.active.ServerHttpRequest;
import org.springframework.active.ServerHttpRequestDecorator;
import org.springframework.active.ServerHttpResponse;
import org.springframework.stereotype.Component;
publisher.Flux;
publisher.Mono;
import javax.security.auth.login.LoginException;
import java.URI;
/**
* FileName: LoginFilter
* Author: TP
* Description:登陆前置校验过滤器
*/
@Component
public class LoginCaptchaFilterGatewayFilterFactory extends AbstractGatewayFilterFactory {
private final static String AUTH_URL = "/auth/login";
@Autowired
private RedisUtil redisUtil;
@Override
public GatewayFilter apply(Object config) {
return (exchange, chain) -> {
ServerHttpRequest serverHttpRequest = Request();
URI uri = URI();
/
/ 不是登录请求,直接向下执⾏
if (!Path(), AUTH_URL)) {
return chain.filter(exchange);
}
if (HttpMethod.POST.MethodValue())) {
String bodyStr = solveBodyFromRequest(serverHttpRequest);
try {
JSONObject bodyJson = JSONObject.parseObject(bodyStr);
String username = String.("username"));
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论