开公司先建设网站,海会主机,成都市分类信息网站开发,网站分几类好的#xff0c;下面是优化后的版本。为了提高可读性和规范性#xff0c;我对内容进行了结构化、简化了部分代码#xff0c;同时增加了注释说明#xff0c;便于理解。 1. 引入依赖
在 pom.xml 中添加以下依赖#xff1a;
dependencies!-- Spring Cloud Gate…好的下面是优化后的版本。为了提高可读性和规范性我对内容进行了结构化、简化了部分代码同时增加了注释说明便于理解。 1. 引入依赖
在 pom.xml 中添加以下依赖
dependencies!-- Spring Cloud Gateway提供API网关功能 --dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-gateway/artifactId/dependency!-- Spring Cloud Alibaba Nacos Discovery用于服务发现 --dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId/dependency!-- Spring Cloud Loadbalancer提供客户端负载均衡 --dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-loadbalancer/artifactId/dependency
/dependencies2. 启动类 (GateApplication.java)
定义主启动类启动 Spring Boot 应用
SpringBootApplication // 标明这是Spring Boot应用的入口
public class GateApplication {public static void main(String[] args) {SpringApplication.run(GateApplication.class, args); // 启动应用}
}3. 配置文件 (application.yml)
配置网关的基本设置、Nacos 服务发现以及路由规则。
server:port: 8080 # 配置网关监听的端口spring:application:name: gateway # 应用名称用于 Nacos 等服务发现cloud:nacos:discovery:server-addr: xiaotianlong.xyz:8848 # 配置 Nacos 服务器地址gateway:routes: # 配置网关的路由规则# 路由规则 1- id: service_name # 路由的唯一IDuri: lb://service_name # 使用负载均衡访问注册到 Nacos 中的服务predicates:- Path/user/** # 请求路径以 /user/ 开头时触发此路由filters:- AddRequestHeaderX-Request-Foo, Bar # 添加请求头# 路由规则 2- id: service_name2uri: lb://service_name2predicates:- Path/order/** # 请求路径以 /order/ 开头时触发此路由4. 自定义全局过滤器
定义一个全局过滤器记录请求的时间并打印日志。
Component // 声明为Spring组件
public class MyGlobalFilter implements GlobalFilter, Ordered {Overridepublic MonoVoid filter(ServerWebExchange exchange, GatewayFilterChain chain) {// 记录请求开始时间System.out.println(请求开始时间: System.currentTimeMillis());// 放行请求return chain.filter(exchange).then(Mono.fromRunnable(() - {// 记录请求结束时间System.out.println(请求结束时间: System.currentTimeMillis());}));}Overridepublic int getOrder() {// 过滤器的执行顺序数字越小优先级越高return 0;}
}5. 自定义Gateway过滤器
创建一个自定义的 Gateway 过滤器工厂允许动态配置过滤器的参数。
Component
public class MyGatewayFilterFactory extends AbstractGatewayFilterFactoryMyGatewayFilterFactory.Config {// 内部配置类允许用户配置过滤器的参数Datapublic static class Config {private String pattern yyyy-MM-dd; // 设置默认日期格式private String message 默认日志信息; // 自定义日志信息}public MyGatewayFilterFactory() {super(Config.class); // 指定配置类类型}Overridepublic ListString shortcutFieldOrder() {return List.of(pattern, message); // 设置快捷字段顺序}Overridepublic GatewayFilter apply(Config config) {// 创建过滤器逻辑return new OrderedGatewayFilter((exchange, chain) - {DateTimeFormatter formatter DateTimeFormatter.ofPattern(config.getPattern());System.out.println(请求开始时间: formatter.format(LocalDateTime.now()));System.out.println(config.getMessage()); // 打印自定义日志信息return chain.filter(exchange).then(Mono.fromRunnable(() - {System.out.println(请求结束时间: formatter.format(LocalDateTime.now()));}));}, 1);//在这里设置顺序}
}6. 配置自定义过滤器
在 application.yml 文件中配置自定义的 MyGatewayFilterFactory 过滤器使其生效
spring:cloud:gateway:routes:- id: example_routeuri: lb://some-servicepredicates:- Path/somepath/** # 路径匹配条件filters:- name: My # 使用自定义过滤器args:pattern: yyyy-MM-dd~HH:mm:ss # 自定义日期格式message: 这是一个统计时间的gateway过滤器 # 自定义日志信息或者
spring:cloud:gateway:routes:- id: example_routeuri: lb://some-servicepredicates:- Path/somepath/** # 路径匹配条件filters:# 由于设置了shortcutfieldorder,所以可以这样写- Myyyyy-MM-dd~HH:mm:ss, 这是一个统计时间的gateway过滤器7.案例:登录检验
Component
EnableConfigurationProperties(AuthProperties.class)
public class AuthGlobalFilter implements GlobalFilter, Ordered {Resourceprivate AuthProperties authProperties;Resourceprivate JwtTool jwtTool;private final AntPathMatcher antPathMatcher new AntPathMatcher();Overridepublic MonoVoid filter(ServerWebExchange exchange, GatewayFilterChain chain) {//1.获取requestServerHttpRequest request exchange.getRequest();//2.判断路径是否需做登录拦截ListString excludePaths authProperties.getExcludePaths();if (isExclude(request.getPath())) {//此时不需要拦截,直接放行return chain.filter(exchange);}//3.获得tokenString token request.getHeaders().getFirst(authorization);//4.检验并解析tokenLong userId null;try {userId jwtTool.parseToken(token);} catch (Exception e) {//设置响应状态码为401ServerHttpResponse response exchange.getResponse();response.setStatusCode(HttpStatus.UNAUTHORIZED);//拦截return response.setComplete();}//todo 5.传递用户信息//6.放行return chain.filter(exchange);}private boolean isExclude(RequestPath path) {ListString excludePaths authProperties.getExcludePaths();for (String pattern : excludePaths) {if (antPathMatcher.match(pattern, path.toString())) {return true;}}return false;}Overridepublic int getOrder() {return 0;}
}