修复网关跨域问题,修复cas cookie重复问题,抽象后台

This commit is contained in:
lhc
2021-03-10 09:12:26 +08:00
parent c84dba33c7
commit f9e10be006
33 changed files with 370 additions and 100 deletions

View File

@@ -1,11 +1,14 @@
package com.hcframe.gateway;
import com.hcframe.redis.RedisUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Import;
@SpringBootApplication
@EnableDiscoveryClient
@Import(RedisUtil.class)
public class GatewayApplication {
public static void main(String[] args) {

View File

@@ -16,7 +16,7 @@ public class CorsConfig {
config.setAllowCredentials(true);
config.addAllowedMethod("*");
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedHeader("X-Access-Token");
//设置预检请求的缓存时间(秒),在这个时间段里,对于相同的跨域请求不会再预检了
config.setMaxAge(18000L);
UrlBasedCorsConfigurationSource source=new UrlBasedCorsConfigurationSource(new PathPatternParser());

View File

@@ -0,0 +1,55 @@
package com.hcframe.gateway.config;
import com.hcframe.gateway_cas_stater.data.DataStorage;
import com.hcframe.redis.RedisUtil;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
/**
* @author lhc
*/
public class MyDataStorage implements DataStorage {
private static final String SESSION = "session";
private static final String EXPIRE_TIME = "expireTime";
private Long expireTime;
@Resource
private RedisUtil redisUtil;
public MyDataStorage(Long expireTime) {
if (expireTime==null){
throw new RuntimeException("expireTime can not be null");
}
this.expireTime = expireTime;
}
@Override
public Object getValue(String userKey, String key) {
Map<String,Object> map = (Map<String, Object>) redisUtil.hget(SESSION, userKey);
if (map == null) {
return null;
}
if (!map.containsKey(key)) {
return null;
}
return map.get(key);
}
@Override
public void setValue(String userKey, String key, Object attr) {
Map<String,Object> map = (Map<String, Object>) redisUtil.hget(SESSION, userKey);
if (map != null) {
map.put(key, attr);
redisUtil.hset("session", userKey, map, expireTime);
} else {
map = new HashMap<>(2);
map.put(key, attr);
map.put(EXPIRE_TIME, expireTime);
redisUtil.hset("session", userKey, map, expireTime);
}
}
}

View File

@@ -5,6 +5,9 @@ import com.hcframe.gateway_cas_stater.config.CasGatewayClientConfig;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
public class GatewayController {
@@ -15,8 +18,11 @@ public class GatewayController {
this.casGatewayClientConfig = casGatewayClientConfig;
}
@GetMapping("cas")
public ResultVO<String> getCasUrl(){
return ResultVO.getSuccess(casGatewayClientConfig.casServiceUrl+casGatewayClientConfig.casContextPath);
@GetMapping("gateway/cas")
public ResultVO<Map<String,String>> getCasUrl(){
Map<String,String> map=new HashMap<>(2);
map.put("cas", casGatewayClientConfig.casServiceUrl + casGatewayClientConfig.casContextPath + casGatewayClientConfig.getLoginUrl());
map.put("config", "http://192.168.1.130:8084/user/cas/valid");
return ResultVO.getSuccess(map);
}
}

View File

@@ -20,6 +20,8 @@ spring:
uri: lb://cloud-user
predicates:
- Path=/user/**
filters:
- PreserveHostHeader
- id: config
uri: lb://cloud-config
predicates:
@@ -53,23 +55,6 @@ spring:
filters: stat,slf4j
# 通过connectProperties属性来打开mergeSql功能慢SQL记录
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 若不使用redis需要注释掉此类信息
redis:
database: 0
host: 192.168.4.119
port: 6379
password:
lettuce:
pool:
# 连接池中的最大空闲连接 默认8
max-idle: 8
# 连接池中的最小空闲连接 默认0
min-idle: 0
# 连接池最大连接数 默认8 ,负数表示没有限制
max-active: 8
# 连接池最大阻塞等待时间(使用负值表示没有限制) 默认-1
max-wait: -1
timeout: 30000
eureka:
client:
service-url:
@@ -119,4 +104,6 @@ hcframe:
casServiceUrl: http://192.168.1.131:8080
serviceUrl: http://192.168.1.130:9527
clientContextPath: /
cookieHolderPattern: com.hcframe.gateway.config.MyDataStorage
white-url: "^(.*(/logout?))|(/user/cas/url)$"

View File

@@ -24,8 +24,7 @@ spring:
druid:
# 配置sqlite文件路径需要填写绝对路径推荐将sqlite文件放入到服务器上而非程序jar包或war包中
driver-class-name: org.sqlite.JDBC
url: jdbc:sqlite:/Volumes/DATA/ideaProject/common-frame/hcframe/hcframe-parent/hcframe-config/src/main/resources/datasource.db
# url: jdbc:sqlite:D:/nacos-server-1.4.1/datasource.db
url: jdbc:sqlite:D:/nacos-server-1.4.1/datasource.db
username:
password:
#使用Druid数据源
@@ -113,3 +112,4 @@ hcframe:
casServiceUrl: http://192.168.1.131:8080
serviceUrl: http://192.168.1.130:9527
clientContextPath: /
cookieHolderPattern: com.hcframe.gateway.config.MyDataStorage

View File

@@ -24,7 +24,6 @@ spring:
druid:
# 配置sqlite文件路径需要填写绝对路径推荐将sqlite文件放入到服务器上而非程序jar包或war包中
driver-class-name: org.sqlite.JDBC
# url: jdbc:sqlite:/Volumes/DATA/ideaProject/common-frame/hcframe/hcframe-parent/hcframe-config/src/main/resources/datasource.db
url: jdbc:sqlite:D:/nacos-server-1.4.1/datasource.db
username:
password:
@@ -64,11 +63,6 @@ spring:
# 连接池最大阻塞等待时间(使用负值表示没有限制) 默认-1
max-wait: -1
timeout: 30000
eureka:
client:
service-url:
# defaultZone: http://admin:123456@tomcat1:8080/eureka/
defaultZone: http://admin:123456@192.168.4.119:8081/eureka/
# 框架配置
frame:
# 是否开启redis 用户登录若开启此项需要配置redis节点及相关配置若不开启需要注释掉redis相关配置信息
@@ -111,5 +105,6 @@ feign:
hcframe:
cas:
casServiceUrl: http://192.168.1.131:8080
serviceUrl: http://192.168.4.131:8084
serviceUrl: http://192.168.1.130:8084
clientContextPath: /
cookieHolderPattern: com.hcframe.gateway.config.MyDataStorage

View File

@@ -3,6 +3,23 @@ spring:
active: @profile.name@
application:
name: cloud-gateway
# 若不使用redis需要注释掉此类信息
redis:
database: @redis.database@
host: @redis.host@
port: @redis.port@
password: @redis.password@
lettuce:
pool:
# 连接池中的最大空闲连接 默认8
max-idle: 8
# 连接池中的最小空闲连接 默认0
min-idle: 0
# 连接池最大连接数 默认8 ,负数表示没有限制
max-active: 8
# 连接池最大阻塞等待时间(使用负值表示没有限制) 默认-1
max-wait: -1
timeout: 30000
mybatis:
mapper-locations: classpath*:mapping/**/*.xml
type-aliases-package: com.hcframe.**.entity