1
This commit is contained in:
@ -0,0 +1,26 @@
|
||||
package com.mosty.base.gateway;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 网关启动服务
|
||||
*
|
||||
* @author kevin
|
||||
* @date 2022/1/21 9:40 AM
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@Configuration
|
||||
@EnableDiscoveryClient
|
||||
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class, DataSourceAutoConfiguration.class})
|
||||
public class MostyGatewayApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(MostyGatewayApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.mosty.base.gateway.filter;
|
||||
|
||||
/**
|
||||
* gateway filter 常量
|
||||
* @author kevin
|
||||
* @date 2022/2/24 12:15 AM
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class FilterOrderConstant {
|
||||
|
||||
/** 最高优先级拦截器,token */
|
||||
public static final int TOKEN_FILTER_ORDER = 1;
|
||||
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
package com.mosty.base.gateway.filter;
|
||||
|
||||
import com.mosty.common.redis.service.RedisService;
|
||||
import com.mosty.common.token.JWTUtil;
|
||||
import com.mosty.common.token.UserInfo;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.data.redis.core.GeoOperations;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import static com.mosty.base.gateway.filter.FilterOrderConstant.TOKEN_FILTER_ORDER;
|
||||
|
||||
@Slf4j
|
||||
@AllArgsConstructor
|
||||
public class JwtFilter implements GlobalFilter, Ordered {
|
||||
|
||||
private final RedisService redisService;
|
||||
private final RedisTemplate redisTemplate;
|
||||
private static final String TOKEN_PREFIX = "token:";
|
||||
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||
String token = exchange.getRequest().getHeaders().getFirst("Authorization");
|
||||
if (token == null || token.isEmpty()) {
|
||||
log.info("token is empty...");
|
||||
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
|
||||
return exchange.getResponse().setComplete();
|
||||
}
|
||||
UserInfo userInfo = JWTUtil.getUserInfo(token);
|
||||
if (userInfo == null) {
|
||||
log.error("get token exception");
|
||||
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
|
||||
return exchange.getResponse().setComplete();
|
||||
}
|
||||
String tokenKey = TOKEN_PREFIX + userInfo.getDeptId() + ":" + userInfo.getUserId();
|
||||
Long time = (Long) redisTemplate.opsForValue().get(tokenKey);
|
||||
if (time == null) {
|
||||
redisTemplate.delete(tokenKey);
|
||||
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
|
||||
return exchange.getResponse().setComplete();
|
||||
}
|
||||
Date date = new Date(time);
|
||||
int second = getMinutes(new Date(), date);
|
||||
if (second > 30) {
|
||||
log.error("token 过期");
|
||||
redisTemplate.delete(tokenKey);
|
||||
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
|
||||
return exchange.getResponse().setComplete();
|
||||
}
|
||||
if (second > 5) {
|
||||
redisTemplate.opsForValue().set(tokenKey, System.currentTimeMillis());
|
||||
}
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取两个时间相差的分钟数
|
||||
*
|
||||
* @param date1 后面时间
|
||||
* @param date2 前面时间
|
||||
* @return 相差分支树
|
||||
*/
|
||||
public static int getMinutes(Date date1, Date date2) {
|
||||
int minutes = 0;
|
||||
if (null != date1 && null != date2) {
|
||||
minutes = (int) ((date1.getTime() - date2.getTime()) / (60 * 1000));
|
||||
}
|
||||
return minutes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return TOKEN_FILTER_ORDER;
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
{
|
||||
"properties": [
|
||||
{
|
||||
"name": "hystrix.command.fallbackcmd.execution.isolation.thread.timeoutInMilliseconds",
|
||||
"type": "java.lang.String",
|
||||
"description": "Description for hystrix.command.fallbackcmd.execution.isolation.thread.timeoutInMilliseconds."
|
||||
},
|
||||
{
|
||||
"name": "hystrix.command.fallbackcmd.execution.isolation.thread.timeoutInMilliseconds",
|
||||
"type": "java.lang.String",
|
||||
"description": "Description for hystrix.command.fallbackcmd.execution.isolation.thread.timeoutInMilliseconds."
|
||||
}
|
||||
] }
|
5
mosty-gateway/src/main/resources/application.yml
Normal file
5
mosty-gateway/src/main/resources/application.yml
Normal file
@ -0,0 +1,5 @@
|
||||
ribbon:
|
||||
ReadTimeout: 600000
|
||||
ConnectTimeout: 600000
|
||||
servlet:
|
||||
context-path: mosty-api
|
141
mosty-gateway/src/main/resources/bootstrap.yml
Normal file
141
mosty-gateway/src/main/resources/bootstrap.yml
Normal file
@ -0,0 +1,141 @@
|
||||
server:
|
||||
port: 8006
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: mosty-gateway
|
||||
redis:
|
||||
host: 192.168.200.131
|
||||
port: 6379
|
||||
password: mosty888
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
namespace: 657d1843-b590-41ac-b5e7-5d261bf00de9
|
||||
server-addr: 192.168.200.131:8848
|
||||
register-enabled: true
|
||||
gateway:
|
||||
globalcors:
|
||||
cors-configurations:
|
||||
'[/**]':
|
||||
allowCredentials: true
|
||||
allowedOrigins: "*"
|
||||
allowedMethods: "*"
|
||||
allowedHeaders: "*"
|
||||
add-to-simple-url-handler-mapping: true
|
||||
routes:
|
||||
- id: mosty-search
|
||||
uri: lb://MOSTY-SEARCH
|
||||
predicates:
|
||||
- Path=/mosty-search/**
|
||||
filters:
|
||||
- StripPrefix=2
|
||||
- id: mosty-base
|
||||
uri: lb://mosty-base
|
||||
predicates:
|
||||
- Path=/mosty-api/mosty-base/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- id: mosty-bkzx
|
||||
uri: lb://mosty-bkzx
|
||||
predicates:
|
||||
- Path=/mosty-api/mosty-bkzx/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- id: mosty-hczx
|
||||
uri: lb://mosty-hczx
|
||||
predicates:
|
||||
- Path=/mosty-api/mosty-hczx/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- id: mosty-jcgl
|
||||
uri: lb://mosty-jcgl
|
||||
predicates:
|
||||
- Path=/mosty-api/mosty-jcgl/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- id: mosty-qwzx
|
||||
uri: lb://mosty-qwzx
|
||||
predicates:
|
||||
- Path=/mosty-api/mosty-qwzx/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- id: mosty-rzzx
|
||||
uri: lb://mosty-rzzx
|
||||
predicates:
|
||||
- Path=/mosty-api/mosty-rzzx/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- id: mosty-sjzx
|
||||
uri: lb://mosty-sjzx
|
||||
predicates:
|
||||
- Path=/mosty-api/mosty-sjzx/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- id: mosty-spxl
|
||||
uri: lb://mosty-spxl
|
||||
predicates:
|
||||
- Path=/mosty-api/mosty-spxl/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- id: mosty-wzzx
|
||||
uri: lb://mosty-wzzx
|
||||
predicates:
|
||||
- Path=/mosty-api/mosty-wzzx/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- id: mosty-yjzl
|
||||
uri: lb://mosty-yjzl
|
||||
predicates:
|
||||
- Path=/mosty-api/mosty-yjzl/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- id: mosty-yszx
|
||||
uri: lb://mosty-yszx
|
||||
predicates:
|
||||
- Path=/mosty-api/mosty-yszx/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- id: mosty-rwzx
|
||||
uri: lb://mosty-rwzx
|
||||
predicates:
|
||||
- Path=/mosty-api/mosty-rwzx/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- id: mosty-other
|
||||
uri: lb://mosty-other
|
||||
predicates:
|
||||
- Path=/mosty-api/mosty-other/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- id: mosty-websocket
|
||||
uri: lb://mosty-websocket
|
||||
predicates:
|
||||
- Path=/mosty-api/mosty-websocket/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
main:
|
||||
allow-bean-definition-overriding: true
|
||||
|
||||
hystrix:
|
||||
command:
|
||||
fallbackcmd:
|
||||
execution:
|
||||
isolation:
|
||||
thread:
|
||||
timeoutInMilliseconds: 3000
|
||||
|
||||
# 开启健康监控
|
||||
management:
|
||||
endpoints:
|
||||
web:
|
||||
exposure:
|
||||
include: '*'
|
||||
endpoint:
|
||||
health:
|
||||
show-details: always
|
||||
|
||||
# 日志
|
||||
#logging:
|
||||
# file: /application/applogs/admin.log
|
||||
|
16
mosty-gateway/src/main/resources/rebel.xml
Normal file
16
mosty-gateway/src/main/resources/rebel.xml
Normal file
@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<!--
|
||||
This is the JRebel configuration file. It maps the running application to your IDE workspace, enabling JRebel reloading for this project.
|
||||
Refer to https://manuals.jrebel.com/jrebel/standalone/config.html for more information.
|
||||
-->
|
||||
<application generated-by="intellij" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.zeroturnaround.com" xsi:schemaLocation="http://www.zeroturnaround.com http://update.zeroturnaround.com/jrebel/rebel-2_3.xsd">
|
||||
|
||||
<id>mosty-gateway</id>
|
||||
|
||||
<classpath>
|
||||
<dir name="E:/project/rs/mosty-dyga-cloud/mosty-gateway/target/classes">
|
||||
</dir>
|
||||
</classpath>
|
||||
|
||||
</application>
|
Reference in New Issue
Block a user