Commit 9392020c by xmh

<fix> 修复用户名传输时中文乱码

1 parent 57b9e902
...@@ -20,6 +20,8 @@ import org.springframework.web.server.ServerWebExchange; ...@@ -20,6 +20,8 @@ import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
...@@ -63,10 +65,16 @@ public class AuthorizationFilter implements GlobalFilter { ...@@ -63,10 +65,16 @@ public class AuthorizationFilter implements GlobalFilter {
return unAuthorized(exchange.getResponse(), checkResult.right); return unAuthorized(exchange.getResponse(), checkResult.right);
} }
// 添加 username 到 header 中, 供之后的服务使用 // 添加 username 到 header 中, 供之后的服务使用
ServerHttpRequest req = request.mutate().headers(headers -> headers.add("username", checkResult.right)).build(); ServerHttpRequest req = request.mutate().headers(headers -> {
try {
headers.add("username", URLEncoder.encode(checkResult.right, "utf8"));
} catch (UnsupportedEncodingException e) {
log.error("", e);
}
}).build();
exchange.mutate().request(req).build(); exchange.mutate().request(req).build();
log.info("token 校验成功:{}", token); log.info("token 校验成功:[{}][{}]", checkResult.right, token);
} }
return chain.filter(exchange); return chain.filter(exchange);
} }
......
...@@ -13,6 +13,7 @@ import org.springframework.web.context.request.ServletRequestAttributes; ...@@ -13,6 +13,7 @@ import org.springframework.web.context.request.ServletRequestAttributes;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.util.Date; import java.util.Date;
@Service @Service
...@@ -33,7 +34,7 @@ public class LogServiceImpl extends BaseServiceImpl<Log> implements LogService { ...@@ -33,7 +34,7 @@ public class LogServiceImpl extends BaseServiceImpl<Log> implements LogService {
String username = null; String username = null;
if (requestAttributes != null) { if (requestAttributes != null) {
HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest(); HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
username = request.getHeader("username"); username = URLDecoder.decode(request.getHeader("username"), "utf8");
} }
Log log = new Log(); Log log = new Log();
log.setUsername(username); log.setUsername(username);
......
...@@ -15,6 +15,7 @@ import org.springframework.web.context.request.ServletRequestAttributes; ...@@ -15,6 +15,7 @@ import org.springframework.web.context.request.ServletRequestAttributes;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
...@@ -80,7 +81,7 @@ public class OpsClientService { ...@@ -80,7 +81,7 @@ public class OpsClientService {
String username = null; String username = null;
if (requestAttributes != null) { if (requestAttributes != null) {
HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest(); HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
username = request.getHeader("username"); username = URLDecoder.decode(request.getHeader("username"), "utf8");
} }
LogVo logVo = new LogVo(); LogVo logVo = new LogVo();
logVo.setUsername(username); logVo.setUsername(username);
......
...@@ -17,6 +17,7 @@ import org.springframework.web.context.request.ServletRequestAttributes; ...@@ -17,6 +17,7 @@ import org.springframework.web.context.request.ServletRequestAttributes;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.util.List; import java.util.List;
/** /**
...@@ -79,7 +80,7 @@ public class OpsClientService { ...@@ -79,7 +80,7 @@ public class OpsClientService {
String username = null; String username = null;
if (requestAttributes != null) { if (requestAttributes != null) {
HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest(); HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
username = request.getHeader("username"); username = URLDecoder.decode(request.getHeader("username"), "utf8");
} }
LogVo logVo = new LogVo(); LogVo logVo = new LogVo();
logVo.setUsername(username); logVo.setUsername(username);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!