电子商务网站建设的步骤一般为(国外免费建站网站搭建
白名单
2.4.1 什么是白名单
问题:所有请求都被拦截,与实际需求不符合。(存在部分请求不需要拦截)
白名单:用于存放不需要拦截的请求路径
2.4.3 实现
步骤一:修改网关yml文件,配置不需要拦截的路径(白名单)
步骤二:创建配置类,用于存放yml中自定义配置信息
步骤三:完善网关过滤器,修改第三方法,在白名单中放行,不在进行拦截。
步骤一:修改==网关==yml文件,配置不需要拦截的路径(白名单)
可能存在多个路径,需要在yml中配置集合
#配置一个
a:
b:
c: abc
#配置一组
a:
b:
c:
- abc
- aaa
- bbb
#自定义数据
sc:
jwt:
secret: sc@Login(Auth}*^31)&czxy% # 登录校验的密钥
pubKeyPath: D:/rsa/rsa.pub # 公钥地址
priKeyPath: D:/rsa/rsa.pri # 私钥地址
expire: 360 # 过期时间,单位分钟
filter:
allowPaths:
- /checkusername
- /checkmobile
- /sms
- /register
- /login
- /verifycode
- /categorys
- /news
- /brands
- /specifications
- /search
- /goods
- /comments
步骤二:创建配置类,用于存放yml中自定义配置信息
package com.czxy.changgou3.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* Created by liangtong.
*/
[@Component](https://my.oschina.net/u/3907912) //交于spring容器的
@ConfigurationProperties(prefix = "sc.filter")
[@Data](https://my.oschina.net/difrik) //getter和setter
public class FilterProperties {
private List allowPaths;
}
编写实现:
~~~java
[@Resource](https://my.oschina.net/u/929718)
private FilterProperties filterProperties;
@Override
public boolean shouldFilter() { //是否进行过滤,返回值true表示执行run();返回值false表示不执行run()
//当前请求路径,如果与“白名单”路径匹配,将return false,否则return true
//1 获得请求
// 1.1 获得上下文对象
RequestContext currentContext = RequestContext.getCurrentContext();
// 1.2 获得请求对象
HttpServletRequest request = currentContext.getRequest();
// 1.3 获得路径
// URI : 统一资源标识符(部分路径),/v3/cgwebservice/user/checkusername
//System.out.println(request.getRequestURI());
// URL :统一资源定位符(完整路径),http://localhost:10010/v3/cgwebservice/user/checkusername
//System.out.println(request.getRequestURL());
String requestURI = request.getRequestURI();
//2 获得白名单路径
List allowPaths = filterProperties.getAllowPaths();
//3 路径匹配,请求路径中,【包含】配置路径
for(String path : allowPaths) {
if( requestURI.contains( path)) {
return false;
}
}
//其他路径,必须执行run()
return true;
}