Commit adad9945 authored by gaozhaochen's avatar gaozhaochen

update: ws问诊接口添加token权限校验

parent 8da2e797
package cn.sh.stc.sict.cloud.common.gateway.filter; package cn.sh.stc.sict.cloud.common.gateway.filter;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.sh.stc.sict.cloud.common.core.util.R; import cn.sh.stc.sict.cloud.common.core.util.R;
import cn.sh.stc.sict.cloud.common.gateway.feign.RemoteAuthService; import cn.sh.stc.sict.cloud.common.gateway.feign.RemoteAuthService;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilter; import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory; import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequest;
...@@ -24,14 +23,14 @@ public class GptServerFilter extends AbstractGatewayFilterFactory { ...@@ -24,14 +23,14 @@ public class GptServerFilter extends AbstractGatewayFilterFactory {
public final RemoteAuthService remoteAuthService; public final RemoteAuthService remoteAuthService;
public static final String SEC_WEBSOCKET_PROTOCOL = "Sec-WebSocket-Protocol"; public static final String WEB_TOKEN = "ws-token";
@Override @Override
public GatewayFilter apply(Object config) { public GatewayFilter apply(Object config) {
return (exchange, chain) -> { return (exchange, chain) -> {
ServerHttpRequest request = exchange.getRequest(); ServerHttpRequest request = exchange.getRequest();
String token = request.getHeaders().getFirst(SEC_WEBSOCKET_PROTOCOL); String token = request.getQueryParams().getFirst(WEB_TOKEN);
log.info("ws-token{}", token); log.error("ws-token = {}", token);
// 权限校验 // 权限校验
if (checkToken(token)) { if (checkToken(token)) {
return chain.filter(exchange); return chain.filter(exchange);
...@@ -48,15 +47,20 @@ public class GptServerFilter extends AbstractGatewayFilterFactory { ...@@ -48,15 +47,20 @@ public class GptServerFilter extends AbstractGatewayFilterFactory {
* @return * @return
*/ */
private boolean checkToken(String token) { private boolean checkToken(String token) {
// if (StringUtils.isEmpty(token)) { try {
// return false; if (StrUtil.isBlank(token)) {
// } return false;
return true; }
// R<Boolean> valid = remoteAuthService.isValid(token);
// if (ObjectUtil.isNull(valid) || ObjectUtil.isNull(valid.getData())) { R<Boolean> valid = remoteAuthService.isValid(token);
// return false; if (ObjectUtil.isNull(valid) || ObjectUtil.isNull(valid.getData())) {
// } return false;
// return valid.getData(); }
return valid.getData();
} catch (Exception e) {
log.error("权限校验失败", e);
return false;
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment