13693261870
8 天以前 3701213986cfe65042d459a832207ae0b76f0989
se-gateway/src/main/java/com/terra/gateway/filter/XssFilter.java
@@ -1,7 +1,10 @@
package com.terra.gateway.filter;
import java.nio.charset.StandardCharsets;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.Resource;
import com.terra.gateway.utils.StringUtils;
import com.terra.gateway.utils.html.EscapeUtil;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
@@ -33,7 +36,7 @@
public class XssFilter implements GlobalFilter, Ordered
{
    // 跨站脚本的 xss 配置,nacos自行添加
    @Autowired
    @Resource
    private XssProperties xss;
    @Override
@@ -58,10 +61,10 @@
        }
        // excludeUrls 不过滤
        String url = request.getURI().getPath();
//        if (StringUtils.matches(url, xss.getExcludeUrls()))
//        {
//            return chain.filter(exchange);
//        }
        if (StringUtils.matches(url, xss.getExcludeUrls()))
        {
            return chain.filter(exchange);
        }
        ServerHttpRequestDecorator httpRequestDecorator = requestDecorator(exchange);
        return chain.filter(exchange.mutate().request(httpRequestDecorator).build());
@@ -83,7 +86,7 @@
                    DataBufferUtils.release(join);
                    String bodyStr = new String(content, StandardCharsets.UTF_8);
                    // 防xss攻击过滤
                    //bodyStr = EscapeUtil.clean(bodyStr);
                    bodyStr = EscapeUtil.clean(bodyStr);
                    // 转成字节
                    byte[] bytes = bodyStr.getBytes(StandardCharsets.UTF_8);
                    NettyDataBufferFactory nettyDataBufferFactory = new NettyDataBufferFactory(ByteBufAllocator.DEFAULT);
@@ -110,14 +113,13 @@
    /**
     * 是否是Json请求
     *
     *
     * @param exchange HTTP请求
     */
    public boolean isJsonRequest(ServerWebExchange exchange)
    {
        String header = exchange.getRequest().getHeaders().getFirst(HttpHeaders.CONTENT_TYPE);
        //return StringUtils.startsWithIgnoreCase(header, MediaType.APPLICATION_JSON_VALUE);
        return true;
        return StringUtils.startsWithIgnoreCase(header, MediaType.APPLICATION_JSON_VALUE);
    }
    @Override