From e9fd64997b300fb55d64fb89902dd55533d00962 Mon Sep 17 00:00:00 2001 From: shizhendong Date: Wed, 25 Aug 2021 14:09:01 +0800 Subject: fix: 修改 agent token 校验逻辑 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../net/geedge/confagent/ConfagentApplication.java | 22 ++++++++++++++++++++ .../confagent/controller/AuthController.java | 24 +++++++++++----------- .../confagent/controller/HealthyController.java | 5 ++++- .../confagent/controller/LokiController.java | 2 +- .../net/geedge/confagent/thread/RequestThread.java | 2 +- .../net/geedge/confagent/util/ConfagentUtil.java | 14 +++++++++---- 6 files changed, 50 insertions(+), 19 deletions(-) diff --git a/src/main/java/net/geedge/confagent/ConfagentApplication.java b/src/main/java/net/geedge/confagent/ConfagentApplication.java index 18887c5..ff8758e 100644 --- a/src/main/java/net/geedge/confagent/ConfagentApplication.java +++ b/src/main/java/net/geedge/confagent/ConfagentApplication.java @@ -1,13 +1,35 @@ package net.geedge.confagent; +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.util.StrUtil; +import cn.hutool.log.Log; +import net.geedge.confagent.util.ConfagentUtil; +import net.geedge.confagent.util.Tool; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import java.io.File; +import java.io.IOException; +import java.util.Properties; + @SpringBootApplication public class ConfagentApplication { + private final static Log log = Log.get(); + public static void main(String[] args) { SpringApplication.run(ConfagentApplication.class, args); + try { + Properties properties = new Properties(); + File configFile = Tool.FileUtil.file(Tool.WebPathUtil.getRootPath(), "config/application.yml"); + properties.load(FileUtil.getInputStream(configFile)); + String tokenPath = (String) properties.get("tokenFile"); + File tokenFile = Tool.FileUtil.file(Tool.WebPathUtil.getRootPath(), StrUtil.emptyToDefault(tokenPath, "config/token.auth")); + String token = Tool.FileUtil.readString(tokenFile, Tool.CharsetUtil.UTF_8); + ConfagentUtil.tokenInMemory = token; + } catch (IOException e) { + log.error(e); + } } } diff --git a/src/main/java/net/geedge/confagent/controller/AuthController.java b/src/main/java/net/geedge/confagent/controller/AuthController.java index afc9ea3..fbc42fc 100644 --- a/src/main/java/net/geedge/confagent/controller/AuthController.java +++ b/src/main/java/net/geedge/confagent/controller/AuthController.java @@ -1,21 +1,19 @@ package net.geedge.confagent.controller; -import java.io.File; -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; - -import org.springframework.beans.factory.annotation.Value; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - import cn.hutool.log.Log; import net.geedge.confagent.annotation.UnCheckToken; import net.geedge.confagent.entity.AuthEntity; +import net.geedge.confagent.util.ConfagentUtil; import net.geedge.confagent.util.R; import net.geedge.confagent.util.RCode; import net.geedge.confagent.util.Tool; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.*; + +import java.io.File; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; @RestController @RequestMapping(value={"/auth"}) @@ -31,9 +29,9 @@ public class AuthController extends BaseController{ * @Author rui * @Date 2021/3/23 */ - @RequestMapping + @PostMapping @UnCheckToken - public R auth(AuthEntity auth){ + public R auth(@RequestBody AuthEntity auth) { if(Tool.StrUtil.isBlank(auth.getName())||Tool.StrUtil.isBlank(auth.getPin())){ return R.error(RCode.AUTH_NAME_PIN_ISNULL); @@ -52,6 +50,7 @@ public class AuthController extends BaseController{ writeToken(token); Map map = new HashMap<>(); map.put("token", token); + ConfagentUtil.tokenInMemory = token; return R.ok(map); } @@ -69,6 +68,7 @@ public class AuthController extends BaseController{ writeToken(t); Map map = new HashMap<>(); map.put("token", t); + ConfagentUtil.tokenInMemory = t; return R.ok(map); } diff --git a/src/main/java/net/geedge/confagent/controller/HealthyController.java b/src/main/java/net/geedge/confagent/controller/HealthyController.java index ff7871e..f604daa 100644 --- a/src/main/java/net/geedge/confagent/controller/HealthyController.java +++ b/src/main/java/net/geedge/confagent/controller/HealthyController.java @@ -23,6 +23,8 @@ import net.geedge.confagent.util.R; import net.geedge.confagent.util.RCode; import net.geedge.confagent.util.Tool; +import javax.servlet.http.HttpServletRequest; + @RestController @RequestMapping("healthy") public class HealthyController extends BaseController{ @@ -75,7 +77,8 @@ public class HealthyController extends BaseController{ @GetMapping @UnCheckToken - public R checkHealthy(@RequestHeader(value="Authorization",required = false) String token) { + public R checkHealthy(@RequestHeader(value = "Authorization", required = false) String token, HttpServletRequest request) { + log.info("check healthy server ip : " + request.getRemoteAddr()); buildHealthyURL(); diff --git a/src/main/java/net/geedge/confagent/controller/LokiController.java b/src/main/java/net/geedge/confagent/controller/LokiController.java index be8db71..b4ea551 100644 --- a/src/main/java/net/geedge/confagent/controller/LokiController.java +++ b/src/main/java/net/geedge/confagent/controller/LokiController.java @@ -191,7 +191,7 @@ public class LokiController extends BaseController{ if(rltList.size()>0) { OutputStream outputStream = null; try { - log.info("loki 返回内容:"+rltList.toString()); + log.info("loki 返回内容:" + JSONObject.toJSONString(rltList)); outputStream = response.getOutputStream(); Tool.IoUtil.writeUtf8(outputStream,true, rltList.get(0)); outputStream.flush(); diff --git a/src/main/java/net/geedge/confagent/thread/RequestThread.java b/src/main/java/net/geedge/confagent/thread/RequestThread.java index 0e31fdc..7dc04e4 100644 --- a/src/main/java/net/geedge/confagent/thread/RequestThread.java +++ b/src/main/java/net/geedge/confagent/thread/RequestThread.java @@ -99,13 +99,13 @@ public class RequestThread implements Callable{ // resOutputStream.flush();//flush 输出流 return str; } catch (Exception e) { + log.error("request error : ", e); try { // response.sendError(500, "request error"); return "request error"; } catch (Exception e1) { log.error("proxy request error",e1); } - log.error("request error : ",e); }finally { // Tool.IoUtil.close(reqInputStream,resOutputStream,connOutputStream,connInputStream); Tool.IoUtil.close(reqInputStream,connOutputStream,connInputStream); diff --git a/src/main/java/net/geedge/confagent/util/ConfagentUtil.java b/src/main/java/net/geedge/confagent/util/ConfagentUtil.java index b7487ea..6715e7c 100644 --- a/src/main/java/net/geedge/confagent/util/ConfagentUtil.java +++ b/src/main/java/net/geedge/confagent/util/ConfagentUtil.java @@ -25,16 +25,22 @@ public class ConfagentUtil { @Value("${confagent.tokenFile:config/token.auth}") protected String tokenFile; //token文件位置 private static String rootPath = Tool.WebPathUtil.getRootPath(); + + public static String tokenInMemory; /** * @Description 读取token文件中的token * @Author rui * @Date 2021/3/25 */ public String readToken() throws IORuntimeException { - File tf = Tool.FileUtil.file(rootPath, tokenFile); - log.info("token file path : {}" ,tf.getAbsolutePath()); - String token = Tool.FileUtil.readString(tf, Tool.CharsetUtil.UTF_8); - return token; + if (Tool.StrUtil.isNotEmpty(tokenInMemory)) { + return tokenInMemory; + } else { + File tf = Tool.FileUtil.file(rootPath, tokenFile); + log.info("token file path : {}" ,tf.getAbsolutePath()); + String token = Tool.FileUtil.readString(tf, Tool.CharsetUtil.UTF_8); + return token; + } } /** -- cgit v1.2.3