SpringCloud网关部分初学,设置了网关过滤器,但是访问该路由无法跳转
问题描述
SpringCloud网关部分初学,设置了网关过滤器,但是访问该路由无法跳转
相关文件
BulldozerGatewayApplication.java
package com.bulldozer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class BulldozerGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(BulldozerGatewayApplication.class, args);
}
}
JwtCheckGatewayFilterFactory
package com.bulldozer.filter;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpResponse;
import reactor.core.publisher.Mono;
/**
* JWT验证的过滤器
*
* @author
* @create 2018-09-09 下午10:05
**/
public class JwtCheckGatewayFilterFactory extends AbstractGatewayFilterFactory<JwtCheckGatewayFilterFactory.Config> {
public JwtCheckGatewayFilterFactory() {
super(Config.class);
}
@Override
public GatewayFilter apply(Config config) {
return (exchange, chain) -> {
String jwtToken = exchange.getRequest().getHeaders().getFirst("Authorization");
//校验jwtToken的合法性
if (jwtToken != null) {
// 合法
// 将用户id作为参数传递下去
return chain.filter(exchange);
}
//不合法(响应未登录的异常)
ServerHttpResponse response = exchange.getResponse();
//设置headers
HttpHeaders httpHeaders = response.getHeaders();
httpHeaders.add("Content-Type", "application/json; charset=UTF-8");
httpHeaders.add("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0");
//设置body
String warningStr = "未登录或登录超时";
DataBuffer bodyDataBuffer = response.bufferFactory().wrap(warningStr.getBytes());
return response.writeWith(Mono.just(bodyDataBuffer));
};
}
public static class Config {
//Put the configuration properties for your filter here
}
}
AppConfig
package com.bulldozer.config;
import com.bulldozer.filter.JwtCheckGatewayFilterFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public JwtCheckGatewayFilterFactory jwtCheckGatewayFilterFactory(){
return new JwtCheckGatewayFilterFactory();
}
}
application.yml
server:
port: 9093
spring:
application:
name: bulldozer-cloud-gateway
cloud:
gateway:
routes:
#netty 路由过滤器,http或https开头 lb://代表是eureka服务的名称 predicates:表示会过滤掉的请求头
- id: gateway-route
uri: lb://server-client
predicates:
- Path=/api/**,/route-api/**
#处理跨域请求问题
globalcors:
cors-configurations:
'[/**]':
allowedOrigins: "*"
allowedMethods: "*"
#redis配置
redis:
host:
port: 6379
password:
database: 1
timeout: 60s
## springboot2.0之后将连接池由jedis改为lettuce
lettuce:
pool:
max-idle: 30
max-active: 8
max-wait: 10000
min-idle: 10
jwt.properties
jwt.ignoreUrlList=/route-api/login,/route-api/refresh
#token过期时间:30分钟
token.expire.time=1800000
#refreshToken过期时间:12小时
refresh.token.expire.time=43200000
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论