项目构建
This commit is contained in:
89
demo-gateway/pom.xml
Normal file
89
demo-gateway/pom.xml
Normal file
@@ -0,0 +1,89 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>com.yxt</groupId>
|
||||
<artifactId>demo</artifactId>
|
||||
<version>0.0.1</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.yxt.demo</groupId>
|
||||
<artifactId>demo-gateway</artifactId>
|
||||
<version>0.0.1</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.yxt.demo</groupId>
|
||||
<artifactId>demo-common-utils</artifactId>
|
||||
<version>0.0.1</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.yxt.demo</groupId>
|
||||
<artifactId>demo-common-core</artifactId>
|
||||
<version>0.0.1</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.yxt.demo</groupId>
|
||||
<artifactId>demo-common-redis</artifactId>
|
||||
<version>0.0.1</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>2.5.6</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<includes>
|
||||
<include>**/*.yml</include>
|
||||
</includes>
|
||||
<filtering>false</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
128
demo-gateway/src/main/java/com/yxt/demo/gateway/AuthFilter.java
Normal file
128
demo-gateway/src/main/java/com/yxt/demo/gateway/AuthFilter.java
Normal file
@@ -0,0 +1,128 @@
|
||||
package com.yxt.demo.gateway;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.yxt.demo.common.core.constant.StatusEnum;
|
||||
import com.yxt.demo.common.core.result.ResultBean;
|
||||
import com.yxt.demo.common.redis.service.RedisService;
|
||||
import com.yxt.demo.common.utils.convert.StringUtil;
|
||||
import com.yxt.demo.gateway.config.CacheConstants;
|
||||
import com.yxt.demo.gateway.config.IgnoreWhiteProperties;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.io.buffer.DataBufferFactory;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* @author dimengzhe
|
||||
* @date 2020/12/2 9:52
|
||||
* @description 网关鉴权
|
||||
* 1.某些接口不需要不进行登录验证,如登录,注册,获取验证码等接口。(uri白名单)
|
||||
* 2.某些接口需要登录验证,但是不需要刷新token有效时间,如客户端轮询请求的接口。
|
||||
* 3.特定场景下IP黑、白名单。
|
||||
* 4.处于安全考虑的接口流量控制。
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class AuthFilter implements GlobalFilter, Ordered {
|
||||
|
||||
/**
|
||||
* 过期时间设置为4小时
|
||||
*/
|
||||
private final static long EXPIRE_TIME = CacheConstants.TOKEN_EXPIRE * 60;
|
||||
private final static long EXPIRE_TIME_APP = CacheConstants.TOKEN_EXPIRE_APP * 60;
|
||||
private final static String APP = "App";
|
||||
|
||||
|
||||
// 排除过滤的 uri 地址,nacos自行添加
|
||||
@Autowired
|
||||
private IgnoreWhiteProperties ignoreWhite;
|
||||
@Autowired
|
||||
private RedisService redisUtil;
|
||||
/*
|
||||
redis中数据存储结构为两个键值对
|
||||
键为用户ID,值为用户token,可以通过用户ID查询用户token,实现立刻失效用户token功能。
|
||||
键为用户token,值为用户数据,实现token有效性,用户数据缓存功能。
|
||||
*/
|
||||
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||
String url = exchange.getRequest().getURI().getPath();
|
||||
//1.uri白名单。 跳过不需要验证的路径
|
||||
if (StringUtil.matches(url, ignoreWhite.getWhites())) {
|
||||
return chain.filter(exchange);
|
||||
} else if (StringUtil.matchesTwo(url, ignoreWhite.getWhitesTwo())) {
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
//2.验证有无令牌。 从请求的header中获取token
|
||||
String token = getToken(exchange.getRequest());
|
||||
if (StringUtil.isBlank(token)) {
|
||||
return setUnauthorizedResponse(exchange, "令牌不能为空");
|
||||
}
|
||||
//3.验证token是否有效。(a.验证token是否合法 b.验证token是否过期)
|
||||
//从redis缓存中获取key对应的内容
|
||||
String userName = redisUtil.get(token);
|
||||
|
||||
if (StringUtil.isBlank(userName)) {
|
||||
return setUnauthorizedResponse(exchange, "登录状态已过期");
|
||||
}
|
||||
//验签:需要验证token中的签名是否与用户sid一致,后台用密钥+userSid+token除签名以外的内容,重新生成签名,与token中的签名进行比较
|
||||
|
||||
//刷新token过期日期
|
||||
if (token.contains(APP)) {
|
||||
redisUtil.expire(token, EXPIRE_TIME_APP);
|
||||
} else {
|
||||
redisUtil.expire(token, EXPIRE_TIME);
|
||||
}
|
||||
|
||||
// 在请求中增加用户信息
|
||||
ServerHttpRequest mutableReq = exchange.getRequest().mutate()
|
||||
.header(CacheConstants.DETAILS_USERNAME, userName).build();
|
||||
ServerWebExchange mutableExchange = exchange.mutate().request(mutableReq).build();
|
||||
return chain.filter(mutableExchange);
|
||||
}
|
||||
|
||||
/**
|
||||
* 鉴权异常处理
|
||||
*
|
||||
* @param exchange
|
||||
* @param msg
|
||||
* @return
|
||||
*/
|
||||
private Mono<Void> setUnauthorizedResponse(ServerWebExchange exchange, String msg) {
|
||||
ResultBean resultBean = ResultBean.fireFail();
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
|
||||
response.setStatusCode(HttpStatus.OK);
|
||||
|
||||
log.error("[鉴权异常处理]请求路径:{}", exchange.getRequest().getPath());
|
||||
|
||||
return response.writeWith(Mono.fromSupplier(() -> {
|
||||
DataBufferFactory bufferFactory = response.bufferFactory();
|
||||
log.error(msg);
|
||||
return bufferFactory.wrap(JSON.toJSONBytes(resultBean.setCode(StatusEnum.OVERDUE.getCode()).setMsg(msg)));
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求token
|
||||
*/
|
||||
private String getToken(ServerHttpRequest request) {
|
||||
String token = request.getHeaders().getFirst(CacheConstants.HEADER);
|
||||
return token;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.yxt.demo.gateway;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalTime;
|
||||
|
||||
/**
|
||||
* @author dimengzhe
|
||||
* @date 2020/8/28 14:11
|
||||
* @description 网关
|
||||
*/
|
||||
@EnableDiscoveryClient
|
||||
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class},
|
||||
scanBasePackages = {"com.yxt.demo"})
|
||||
@Slf4j
|
||||
public class DemoGatewayApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DemoGatewayApplication.class, args);
|
||||
log.info("------" + LocalDate.now(), LocalTime.now() + "------");
|
||||
log.info("(♥◠‿◠)ノ゙ 网关模块启动成功 ლ(´ڡ`ლ)゙ \n");
|
||||
}
|
||||
}
|
||||
107
demo-gateway/src/main/java/com/yxt/demo/gateway/RedisConfig.java
Normal file
107
demo-gateway/src/main/java/com/yxt/demo/gateway/RedisConfig.java
Normal file
@@ -0,0 +1,107 @@
|
||||
package com.yxt.demo.gateway;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
|
||||
/**
|
||||
* @author dimengzhe
|
||||
* @date 2020/9/18 19:37
|
||||
* @description
|
||||
*/
|
||||
@Configuration
|
||||
public class RedisConfig {
|
||||
|
||||
@Value("${spring.redis.host}")
|
||||
private String host;
|
||||
|
||||
@Value("${spring.redis.password}")
|
||||
private String password;
|
||||
|
||||
@Value("${spring.redis.port}")
|
||||
private int port;
|
||||
|
||||
@Value("${spring.redis.timeout}")
|
||||
private int timeout;
|
||||
|
||||
@Value("${spring.redis.jedis.pool.max-active}")
|
||||
private int redisPoolMaxActive;
|
||||
|
||||
@Value("${spring.redis.jedis.pool.max-wait}")
|
||||
private int redisPoolMaxWait;
|
||||
|
||||
@Value("${spring.redis.jedis.pool.max-idle}")
|
||||
private int redisPoolMaxIdle;
|
||||
|
||||
@Value("${spring.redis.jedis.pool.min-idle}")
|
||||
private int redisPoolMinIdle;
|
||||
@Value("${spring.redis.database}")
|
||||
private int database;
|
||||
|
||||
@Bean
|
||||
public JedisPoolConfig getJedisPoolConfig() {
|
||||
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
|
||||
//最大空闲数
|
||||
jedisPoolConfig.setMaxIdle(redisPoolMaxIdle);
|
||||
//最小空闲数
|
||||
jedisPoolConfig.setMinIdle(redisPoolMinIdle);
|
||||
//最大建立连接等待时间
|
||||
jedisPoolConfig.setMaxWaitMillis(redisPoolMaxWait);
|
||||
//连接池的最大数据库连接数
|
||||
jedisPoolConfig.setMaxTotal(redisPoolMaxActive);
|
||||
return jedisPoolConfig;
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
|
||||
|
||||
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(jedisPoolConfig);
|
||||
jedisConnectionFactory.setDatabase(database);
|
||||
//IP地址
|
||||
jedisConnectionFactory.setHostName(host);
|
||||
//如果redis设置有密码
|
||||
jedisConnectionFactory.setPassword(password);
|
||||
//端口号
|
||||
jedisConnectionFactory.setPort(port);
|
||||
//客户端超时时间单位是毫秒
|
||||
jedisConnectionFactory.setTimeout(timeout);
|
||||
// jedisConnectionFactory.afterPropertiesSet(); //记得添加这行!
|
||||
return jedisConnectionFactory;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
|
||||
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
|
||||
redisTemplate.setConnectionFactory(redisConnectionFactory);
|
||||
// 使用Jackson2JsonRedisSerialize 替换默认序列化
|
||||
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
|
||||
ObjectMapper om = new ObjectMapper();
|
||||
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
||||
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
|
||||
jackson2JsonRedisSerializer.setObjectMapper(om);
|
||||
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
|
||||
// key采用String的序列化方式
|
||||
//redisTemplate.setKeySerializer(new StringRedisSerializer());
|
||||
redisTemplate.setKeySerializer(stringRedisSerializer);
|
||||
// hash的key也采用String的序列化方式
|
||||
redisTemplate.setHashKeySerializer(stringRedisSerializer);
|
||||
// value序列化方式采用jackson
|
||||
// redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
|
||||
redisTemplate.setValueSerializer(stringRedisSerializer);
|
||||
// hash的value序列化方式采用jackson
|
||||
redisTemplate.setHashValueSerializer(stringRedisSerializer);
|
||||
redisTemplate.afterPropertiesSet();
|
||||
return redisTemplate;
|
||||
}
|
||||
}
|
||||
300
demo-gateway/src/main/java/com/yxt/demo/gateway/RedisUtil.java
Normal file
300
demo-gateway/src/main/java/com/yxt/demo/gateway/RedisUtil.java
Normal file
@@ -0,0 +1,300 @@
|
||||
package com.yxt.demo.gateway;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.connection.RedisStringCommands;
|
||||
import org.springframework.data.redis.core.*;
|
||||
import org.springframework.data.redis.core.types.Expiration;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author dimengzhe
|
||||
* @date 2020/9/9 17:35
|
||||
* @description redis工具类
|
||||
*/
|
||||
@Service
|
||||
public class RedisUtil {
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
|
||||
/**
|
||||
* 字符串类型:根据key设置value值,如果key中的value存在,那么返回false
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
* @return
|
||||
*/
|
||||
public Boolean setnx(final String key, final String value, final long expration, final TimeUnit timeUnit) {
|
||||
return (Boolean) redisTemplate.execute(new RedisCallback<Boolean>() {
|
||||
@Override
|
||||
public Boolean doInRedis(RedisConnection redisConnection) throws DataAccessException {
|
||||
RedisSerializer<String> redisSerializer = redisTemplate.getStringSerializer();
|
||||
byte keys[] = redisSerializer.serialize(key);
|
||||
byte values[] = redisSerializer.serialize(value);
|
||||
return redisConnection.set(keys, values, Expiration.from(expration, timeUnit), RedisStringCommands.SetOption.SET_IF_ABSENT);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入缓存
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
* @return
|
||||
*/
|
||||
public boolean set(final String key, final String value) {
|
||||
|
||||
boolean result = (boolean) redisTemplate.execute(new RedisCallback<Boolean>() {
|
||||
@Override
|
||||
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
|
||||
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
|
||||
connection.set(serializer.serialize(key), serializer.serialize(value));
|
||||
return true;
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入缓存设置时效时间
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
* @return
|
||||
*/
|
||||
public boolean set(final String key, Object value, Long expireTime) {
|
||||
boolean result = false;
|
||||
try {
|
||||
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
|
||||
operations.set(key, value);
|
||||
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
|
||||
result = true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/**
|
||||
* 刷新缓存到期时间
|
||||
* @param key
|
||||
* @param expire
|
||||
* @return
|
||||
*/
|
||||
public boolean expire(String key, long expire) {
|
||||
return redisTemplate.expire(key, expire, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取缓存
|
||||
*
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public String get(final String key) {
|
||||
String result = (String) redisTemplate.execute(new RedisCallback<String>() {
|
||||
@Override
|
||||
public String doInRedis(RedisConnection connection) throws DataAccessException {
|
||||
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
|
||||
byte[] value = connection.get(serializer.serialize(key));
|
||||
return serializer.deserialize(value);
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 正则获取key集合
|
||||
*
|
||||
* @param pattern
|
||||
* @return
|
||||
*/
|
||||
public Set<String> keys(String pattern) {
|
||||
Set<String> keys = redisTemplate.keys(pattern);
|
||||
return keys;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 批量删除对应的value
|
||||
*
|
||||
* @param keys
|
||||
*/
|
||||
public void remove(final String... keys) {
|
||||
for (String key : keys) {
|
||||
remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除key
|
||||
*
|
||||
* @param pattern
|
||||
*/
|
||||
public void removePattern(final String pattern) {
|
||||
Set<Serializable> keys = redisTemplate.keys(pattern);
|
||||
if (keys.size() > 0) {
|
||||
redisTemplate.delete(keys);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public Long remove(final String key) {
|
||||
return (Long) redisTemplate.execute(new RedisCallback<Long>() {
|
||||
@Override
|
||||
public Long doInRedis(RedisConnection redisConnection) throws DataAccessException {
|
||||
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
|
||||
byte keys[] = serializer.serialize(key);
|
||||
return redisConnection.del(keys);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 判断缓存中是否有对应的value
|
||||
*
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public boolean exists(final String key) {
|
||||
return redisTemplate.hasKey(key);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 哈希 添加
|
||||
*
|
||||
* @param key
|
||||
* @param hashKey
|
||||
* @param value
|
||||
*/
|
||||
public void hmSet(String key, Object hashKey, Object value) {
|
||||
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
|
||||
hash.put(key, hashKey, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 哈希获取数据
|
||||
*
|
||||
* @param key
|
||||
* @param hashKey
|
||||
* @return
|
||||
*/
|
||||
public String hmGet(String key, Object hashKey) {
|
||||
HashOperations<String, String, String> hash = redisTemplate.opsForHash();
|
||||
return hash.get(key, hashKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取哈希 keys
|
||||
*
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public Set<String> hmGetKeys(String key) {
|
||||
HashOperations<String, String, String> hash = redisTemplate.opsForHash();
|
||||
return hash.keys(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除集合中的key
|
||||
*
|
||||
* @param key
|
||||
* @param hashKey
|
||||
*/
|
||||
public void hmDelete(String key, Object hashKey) {
|
||||
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
|
||||
hash.delete(key, hashKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 列表添加
|
||||
*
|
||||
* @param k
|
||||
* @param v
|
||||
*/
|
||||
public void lPush(String k, Object v) {
|
||||
ListOperations<String, Object> list = redisTemplate.opsForList();
|
||||
list.rightPush(k, v);
|
||||
}
|
||||
|
||||
/**
|
||||
* 列表获取
|
||||
*
|
||||
* @param k
|
||||
* @param l
|
||||
* @param l1
|
||||
* @return
|
||||
*/
|
||||
public List<Object> lRange(String k, long l, long l1) {
|
||||
ListOperations<String, Object> list = redisTemplate.opsForList();
|
||||
return list.range(k, l, l1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 集合添加
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
*/
|
||||
public void add(String key, Object value) {
|
||||
SetOperations<String, Object> set = redisTemplate.opsForSet();
|
||||
set.add(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 集合获取
|
||||
*
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public Set<Object> setMembers(String key) {
|
||||
SetOperations<String, Object> set = redisTemplate.opsForSet();
|
||||
return set.members(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 有序集合添加
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
* @param scoure
|
||||
*/
|
||||
public void zAdd(String key, Object value, double scoure) {
|
||||
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
|
||||
zset.add(key, value, scoure);
|
||||
}
|
||||
|
||||
/**
|
||||
* 有序集合获取
|
||||
*
|
||||
* @param key
|
||||
* @param scoure
|
||||
* @param scoure1
|
||||
* @return
|
||||
*/
|
||||
public Set<Object> rangeByScore(String key, double scoure, double scoure1) {
|
||||
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
|
||||
return zset.rangeByScore(key, scoure, scoure1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 实现命令:TTL key 以秒为单位,返回给定key的剩余生存时间
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public long ttl(String key) {
|
||||
return redisTemplate.getExpire(key);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.yxt.demo.gateway.config;
|
||||
|
||||
/**
|
||||
* @author dimengzhe
|
||||
* @date 2020/12/2 9:58
|
||||
* @description 缓存的key 常量
|
||||
*/
|
||||
|
||||
public class CacheConstants {
|
||||
|
||||
/**
|
||||
* 令牌自定义标识
|
||||
*/
|
||||
public static final String HEADER = "token";
|
||||
|
||||
/**
|
||||
* 令牌前缀
|
||||
*/
|
||||
public static final String TOKEN_PREFIX = "Bearer ";
|
||||
|
||||
/**
|
||||
* 用户名字段
|
||||
*/
|
||||
public static final String DETAILS_USERNAME = "userName";
|
||||
|
||||
/**
|
||||
* 令牌有效期(分钟)
|
||||
*/
|
||||
public final static long TOKEN_EXPIRE = 4*60;
|
||||
public final static long TOKEN_EXPIRE_APP = 15*24*60;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.yxt.demo.gateway.config;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author dimengzhe
|
||||
* @date 2020/12/2 9:54
|
||||
* @description 放行白名单配置
|
||||
*/
|
||||
@Configuration
|
||||
@RefreshScope
|
||||
@ConfigurationProperties(prefix = "ignore")
|
||||
public class IgnoreWhiteProperties {
|
||||
|
||||
/**
|
||||
* 放行白名单配置,网关不校验此处的白名单
|
||||
*/
|
||||
private List<String> whites = new ArrayList<>();
|
||||
|
||||
public List<String> getWhites() {
|
||||
return whites;
|
||||
}
|
||||
|
||||
public void setWhites(List<String> whites) {
|
||||
this.whites = whites;
|
||||
}
|
||||
|
||||
private List<String> whitesTwo = new ArrayList<>();
|
||||
|
||||
public List<String> getWhitesTwo() {
|
||||
return whitesTwo;
|
||||
}
|
||||
|
||||
public void setWhitesTwo(List<String> whitesTwo) {
|
||||
this.whitesTwo = whitesTwo;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.yxt.demo.gateway.config.swagger;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.cloud.gateway.config.GatewayProperties;
|
||||
import org.springframework.cloud.gateway.route.RouteLocator;
|
||||
import org.springframework.cloud.gateway.support.NameUtils;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Component;
|
||||
import springfox.documentation.swagger.web.SwaggerResource;
|
||||
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2022/11/5 22:58
|
||||
* @Description
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@Primary
|
||||
@AllArgsConstructor
|
||||
public class SwaggerConfig implements SwaggerResourcesProvider {
|
||||
/**
|
||||
* 路由加载器
|
||||
*/
|
||||
private final RouteLocator routeLocator;
|
||||
private final GatewayProperties gatewayProperties;
|
||||
|
||||
@Override
|
||||
public List<SwaggerResource> get() {
|
||||
List<SwaggerResource> resources = new ArrayList<>();
|
||||
List<String> routes = new ArrayList<>();
|
||||
//获取所有路由的ID
|
||||
routeLocator.getRoutes().subscribe(route -> routes.add(route.getId()));
|
||||
//过滤出配置文件中定义的路由->过滤出Path Route Predicate->根据路径拼接成api-docs路径->生成SwaggerResource
|
||||
gatewayProperties.getRoutes().stream().filter(routeDefinition -> routes.contains(routeDefinition.getId())).forEach(route -> {
|
||||
route.getPredicates().stream()
|
||||
.filter(predicateDefinition -> ("Path").equalsIgnoreCase(predicateDefinition.getName()))
|
||||
.forEach(predicateDefinition -> resources.add(swaggerResource(route.getId(),
|
||||
predicateDefinition.getArgs().get(NameUtils.GENERATED_NAME_PREFIX + "0")
|
||||
.replace("**", "v2/api-docs"))));
|
||||
});
|
||||
|
||||
return resources;
|
||||
}
|
||||
|
||||
private SwaggerResource swaggerResource(String name, String location) {
|
||||
log.info("name:{},location:{}", name, location);
|
||||
SwaggerResource swaggerResource = new SwaggerResource();
|
||||
swaggerResource.setName(name);
|
||||
swaggerResource.setLocation(location);
|
||||
swaggerResource.setSwaggerVersion("2.0");
|
||||
return swaggerResource;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.yxt.demo.gateway.config.swagger;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import reactor.core.publisher.Mono;
|
||||
import springfox.documentation.swagger.web.SecurityConfiguration;
|
||||
import springfox.documentation.swagger.web.SecurityConfigurationBuilder;
|
||||
import springfox.documentation.swagger.web.UiConfiguration;
|
||||
import springfox.documentation.swagger.web.UiConfigurationBuilder;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2022/11/5 23:03
|
||||
* @Description
|
||||
*/
|
||||
|
||||
@RestController
|
||||
public class SwaggerHandler {
|
||||
|
||||
@Autowired(required = false)
|
||||
private SecurityConfiguration securityConfiguration;
|
||||
|
||||
@Autowired(required = false)
|
||||
private UiConfiguration uiConfiguration;
|
||||
|
||||
private final SwaggerConfig swaggerResources;
|
||||
|
||||
@Autowired
|
||||
public SwaggerHandler(SwaggerConfig swaggerResources) {
|
||||
this.swaggerResources = swaggerResources;
|
||||
}
|
||||
|
||||
/**
|
||||
* Swagger安全配置,支持oauth和apiKey设置
|
||||
*/
|
||||
@GetMapping("/swagger-resources/configuration/security")
|
||||
public Mono<ResponseEntity<SecurityConfiguration>> securityConfiguration() {
|
||||
return Mono.just(new ResponseEntity<>(
|
||||
Optional.ofNullable(securityConfiguration).orElse(SecurityConfigurationBuilder.builder().build()), HttpStatus.OK));
|
||||
}
|
||||
|
||||
/**
|
||||
* Swagger UI配置
|
||||
*/
|
||||
@GetMapping("/swagger-resources/configuration/ui")
|
||||
public Mono<ResponseEntity<UiConfiguration>> uiConfiguration() {
|
||||
return Mono.just(new ResponseEntity<>(
|
||||
Optional.ofNullable(uiConfiguration).orElse(UiConfigurationBuilder.builder().build()), HttpStatus.OK));
|
||||
}
|
||||
|
||||
/**
|
||||
* Swagger资源配置,微服务中这各个服务的api-docs信息
|
||||
*/
|
||||
@GetMapping("/swagger-resources")
|
||||
public Mono<ResponseEntity> swaggerResources() {
|
||||
return Mono.just((new ResponseEntity<>(swaggerResources.get(), HttpStatus.OK)));
|
||||
}
|
||||
}
|
||||
19
demo-gateway/src/main/resources/application-devv.yml
Normal file
19
demo-gateway/src/main/resources/application-devv.yml
Normal file
@@ -0,0 +1,19 @@
|
||||
spring:
|
||||
resources:
|
||||
static-locations: file:D://anrui
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
server-addr: 127.0.0.1:8848
|
||||
redis:
|
||||
database: 3 # Redis数据库索引(默认为0)
|
||||
host: 127.0.0.1
|
||||
jedis:
|
||||
pool:
|
||||
max-active: -1 #连接池最大连接数(使用负值表示没有限制)
|
||||
max-idle: 8 #连接池中的最大空闲连接
|
||||
max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
|
||||
min-idle: 0 # 连接池中的最小空闲连接
|
||||
password: 123456
|
||||
port: 6379
|
||||
timeout: 0 # 连接超时时间(毫秒)
|
||||
19
demo-gateway/src/main/resources/application-pro.yml
Normal file
19
demo-gateway/src/main/resources/application-pro.yml
Normal file
@@ -0,0 +1,19 @@
|
||||
spring:
|
||||
resources:
|
||||
static-locations: file:D://anrui
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
server-addr: 127.0.0.1:8848
|
||||
redis:
|
||||
database: 3 # Redis数据库索引(默认为0)
|
||||
host: 127.0.0.1
|
||||
jedis:
|
||||
pool:
|
||||
max-active: -1 #连接池最大连接数(使用负值表示没有限制)
|
||||
max-idle: 8 #连接池中的最大空闲连接
|
||||
max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
|
||||
min-idle: 0 # 连接池中的最小空闲连接
|
||||
password: 123456
|
||||
port: 6379
|
||||
timeout: 0 # 连接超时时间(毫秒)
|
||||
19
demo-gateway/src/main/resources/application-test.yml
Normal file
19
demo-gateway/src/main/resources/application-test.yml
Normal file
@@ -0,0 +1,19 @@
|
||||
spring:
|
||||
resources:
|
||||
static-locations: file:/home/lzh/docker_data/nginx/html/anrui-system-ui
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
server-addr: 172.18.0.5:8848
|
||||
redis:
|
||||
database: 3 # Redis数据库索引(默认为0)
|
||||
host: 172.18.0.7
|
||||
jedis:
|
||||
pool:
|
||||
max-active: -1 #连接池最大连接数(使用负值表示没有限制)
|
||||
max-idle: 8 #连接池中的最大空闲连接
|
||||
max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
|
||||
min-idle: 0 # 连接池中的最小空闲连接
|
||||
password:
|
||||
port: 6379
|
||||
timeout: 0 # 连接超时时间(毫秒)
|
||||
91
demo-gateway/src/main/resources/application.yml
Normal file
91
demo-gateway/src/main/resources/application.yml
Normal file
@@ -0,0 +1,91 @@
|
||||
hystrix:
|
||||
command:
|
||||
default:
|
||||
execution:
|
||||
isolation:
|
||||
strategy: SEMAPHORE
|
||||
thread:
|
||||
timeoutInMilliseconds: 300000
|
||||
server:
|
||||
port: 8111
|
||||
spring:
|
||||
application:
|
||||
name: gateway-server
|
||||
profiles:
|
||||
active: devv
|
||||
cloud:
|
||||
gateway:
|
||||
routes:
|
||||
- id: demo-system
|
||||
predicates:
|
||||
- Path= /system/**
|
||||
uri: lb://demo-system
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
|
||||
ignore:
|
||||
whites:
|
||||
- /portal/v1/regions/treeList
|
||||
- /portal/v1/sysuser/reGetPwd #忘记密码
|
||||
- /portal/v1/sysuser/login #pc端登录
|
||||
-
|
||||
- /portal/v1/sysuser/loginByNoVeriCode #登录无验证码
|
||||
- /portal/v1/sysuser/signOut #pc 退出登录
|
||||
- /portal/v1/sysuser/save #pc端登录
|
||||
- /portal/v1/captcha/clickWord #验证码
|
||||
|
||||
- /portal/v1/appuser/login #app端登录
|
||||
- /portal/v1/appuser/sendVerificationCodeForApp #app端验证码
|
||||
- /portal/v1/appuser/checkResetPwdCode #app端验证找回密码的验证码
|
||||
- /portal/v1/appuser/resetPwd #app端重置密码(找回密码)
|
||||
- /portal/v1/appversion/save #更新app框架
|
||||
- /portal/v1/appsubsetversion/save #更新app子应用(新增)
|
||||
- /portal/v1/appsubsetversion/update #更新app子应用(更新)
|
||||
- /portal/v1/appversion/selectNewAppVersion #获取最新版本
|
||||
- /portal/v1/wxuser/sendMessageCode #客户端注册获取验证码
|
||||
- /portal/v1/wxuser/registsUser #客户端注册
|
||||
- /portal/v1/wxuser/login #客户端登录
|
||||
- /portal/file/getImgBase64
|
||||
- /portal/v1/wxuser/checkResetPwdCode #客户端端验证找回密码的验证码
|
||||
- /portal/v1/wxuser/resetPwd #修改密码
|
||||
- /portal/v1/sysstaffinfo/searchAppContactsList #通讯录搜索
|
||||
- /base/v1/basevehiclebrand/selectAppListByManufacturerSid # 查询品牌
|
||||
|
||||
- /portal/file/upload #上传文件
|
||||
- /crm/v1/crmcustomer/getsCard #证件扫描
|
||||
- /crm/v1/crmcustomertemp/getCustomerTempListByUserSid #获取客户列表
|
||||
- /base/v1/baseaffiliatcompanyappendix/uploadImage #挂靠公司附件上传
|
||||
- /base/v1/basedistributorappendix/upload #经销商备案资料
|
||||
- /base/v1/basefinbank/listPage #查询资方
|
||||
|
||||
# 数据字典
|
||||
- /portal/v1/dictcommons/typeValues # 获取下拉框
|
||||
# 手机批量上传
|
||||
- /base/file/batchUploadImage
|
||||
- /portal/file/batchUploadImage
|
||||
- /terminal/autoservice/v1/getVins/{modelSid}/{configSid} #获取车架号
|
||||
# 手机端根据使用orgSid查询生产厂商列表
|
||||
- /base/v1/basemanufacturer/selectAppListByOrgSid
|
||||
# 查询中介单位
|
||||
- /base/v1/basedistributor/getAppDistributorList
|
||||
# 查询挂靠单位
|
||||
- /base/v1/baseaffiliatcompany/getAppAffiliatList
|
||||
|
||||
# 省市县
|
||||
- /portal/v1/regions/getProvince
|
||||
- /portal/v1/regions/getCity
|
||||
- /portal/v1/regions/getCounty
|
||||
- /portal/v1/wxuser/test
|
||||
- /portal/v1/appuser/updateAppId #解绑用户的appId
|
||||
|
||||
|
||||
whitesTwo: #包含所有
|
||||
###swagger相关开始
|
||||
- /doc.html
|
||||
- /webjars/**
|
||||
- /swagger-resources
|
||||
- /v2/**
|
||||
- /favicon.ico
|
||||
- /upload/**
|
||||
###swagger相关结束
|
||||
|
||||
Reference in New Issue
Block a user