summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfangshunjian <[email protected]>2021-05-14 19:46:32 +0800
committerfangshunjian <[email protected]>2021-05-14 19:46:32 +0800
commit7d49f63d963c0d7ac28e4a72a4aea662f5bfcbb3 (patch)
tree84403a55869912548976d9c9e4361bb45e303383
parent616a2062093816e320cb138408e8ee4e707b9fc0 (diff)
fix: 修复配置文件路径不正确的bug
-rw-r--r--src/main/java/net/geedge/confagent/config/ConfagentConfiguration.java7
-rw-r--r--src/main/java/net/geedge/confagent/controller/AuthController.java46
-rw-r--r--src/main/java/net/geedge/confagent/controller/BaseController.java5
-rw-r--r--src/main/java/net/geedge/confagent/controller/HealthyController.java8
-rw-r--r--src/main/java/net/geedge/confagent/controller/PrometheusController.java9
-rw-r--r--src/main/java/net/geedge/confagent/interceptor/TokenInterceptor.java29
-rw-r--r--src/main/java/net/geedge/confagent/util/ConfagentUtil.java49
-rw-r--r--src/main/java/net/geedge/confagent/util/Tool.java57
-rw-r--r--src/main/resources/config/auth.yml (renamed from src/main/resources/auth.yml)0
-rw-r--r--src/main/resources/config/token.auth (renamed from src/main/resources/token.auth)0
10 files changed, 139 insertions, 71 deletions
diff --git a/src/main/java/net/geedge/confagent/config/ConfagentConfiguration.java b/src/main/java/net/geedge/confagent/config/ConfagentConfiguration.java
index ec7aab1..4406529 100644
--- a/src/main/java/net/geedge/confagent/config/ConfagentConfiguration.java
+++ b/src/main/java/net/geedge/confagent/config/ConfagentConfiguration.java
@@ -2,6 +2,7 @@ package net.geedge.confagent.config;
import net.geedge.confagent.interceptor.TokenInterceptor;
import org.apache.catalina.connector.Connector;
+import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
@@ -12,6 +13,10 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class ConfagentConfiguration implements WebMvcConfigurer {
+
+ @Autowired
+ private TokenInterceptor tokenInterceptor;
+
@Bean(name={"tomcatServletWebServerFactory"})
public ConfigurableServletWebServerFactory webServerFactory() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
@@ -29,6 +34,6 @@ public class ConfagentConfiguration implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
- registry.addInterceptor(new TokenInterceptor()).addPathPatterns("/**");
+ registry.addInterceptor(tokenInterceptor).addPathPatterns("/**");
}
}
diff --git a/src/main/java/net/geedge/confagent/controller/AuthController.java b/src/main/java/net/geedge/confagent/controller/AuthController.java
index 73e6f6e..096bdb5 100644
--- a/src/main/java/net/geedge/confagent/controller/AuthController.java
+++ b/src/main/java/net/geedge/confagent/controller/AuthController.java
@@ -1,41 +1,47 @@
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.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"})
public class AuthController extends BaseController{
- private Log log = Log.get();
+ private final static Log log = Log.get();
@Value("${confagent.authFile:auth.yml}")
private String authFile;
-
+
+ private static String rootPath = Tool.WebPathUtil.getRootPath();
/**
* @Description 每次请求auth接口重新生成 token并记录到文件
* @Author rui
* @Date 2021/3/23
*/
- @PostMapping
+ @RequestMapping
@UnCheckToken
- public R auth(@RequestBody AuthEntity auth){
+ public R auth(AuthEntity auth){
if(Tool.StrUtil.isBlank(auth.getName())||Tool.StrUtil.isBlank(auth.getPin())){
return R.error(RCode.AUTH_NAME_PIN_ISNULL);
}
- Properties properties = Tool.YamlUtil.yamlToProperties(resourcePath,authFile);
+ File af = Tool.FileUtil.file(rootPath, authFile);
+ log.info("auth file path : {}" ,af.getAbsolutePath());
+ Properties properties = Tool.YamlUtil.yamlToProperties(af.getAbsolutePath());
String name = properties.getProperty("auth.name","nezha");
String pin = properties.getProperty("auth.pin","nezha");
@@ -72,11 +78,11 @@ public class AuthController extends BaseController{
*/
@GetMapping("logout")
public R logout(){
-
- String path = Tool.YamlUtil.getFileAbsolutePath(resourcePath, tokenFile);
- File file = Tool.FileUtil.file(path);
- if(Tool.FileUtil.exist(file)){
- Tool.FileUtil.del(file);
+ File tf = Tool.FileUtil.file(rootPath, tokenFile);
+ log.info("token file path : {}" ,tf.getAbsolutePath());
+
+ if(Tool.FileUtil.exist(tf)){
+ Tool.FileUtil.del(tf);
}
if(Tool.FileUtil.exist(prometheusConfPath)){
Tool.YamlUtil.writeAsMap(null,prometheusConfPath);
@@ -89,9 +95,9 @@ public class AuthController extends BaseController{
}
private void writeToken(String token){
- String path = Tool.YamlUtil.getFileAbsolutePath(resourcePath, tokenFile);
- File file = Tool.FileUtil.file(path);
- Tool.IoUtil.write(Tool.FileUtil.getOutputStream(file), Tool.CharsetUtil.UTF_8,true,token);
+ File tf = Tool.FileUtil.file(rootPath, tokenFile);
+ log.info("token file path : {}" ,tf.getAbsolutePath());
+ Tool.FileUtil.writeUtf8String(token, tf);
}
}
diff --git a/src/main/java/net/geedge/confagent/controller/BaseController.java b/src/main/java/net/geedge/confagent/controller/BaseController.java
index b9d8370..1936245 100644
--- a/src/main/java/net/geedge/confagent/controller/BaseController.java
+++ b/src/main/java/net/geedge/confagent/controller/BaseController.java
@@ -15,7 +15,7 @@ public abstract class BaseController {
protected static final String SNMPEXPORTER_LISTEN_ADDR = "web.listen-address";
protected static final String BLACKBOXEXPORTER_LISTEN_ADDR = "web.listen-address";
- @Value("${confagent.tokenFile:token.auth}")
+ @Value("${confagent.tokenFile:config/token.auth}")
protected String tokenFile; //token文件位置
@Value("${confagent.prometheus.cmdLine}")
@@ -42,9 +42,6 @@ public abstract class BaseController {
protected static final int DEFAULT_BLACKBOXEXPORTER_PORT=9115; //black box exporter 默认监听端口
- @Value("${confagent.resourcePath:./config}")
- protected String resourcePath;
-
@Value("${confagent.prometheus.config}")
protected String prometheusConfPath;
diff --git a/src/main/java/net/geedge/confagent/controller/HealthyController.java b/src/main/java/net/geedge/confagent/controller/HealthyController.java
index 39301d3..9a51984 100644
--- a/src/main/java/net/geedge/confagent/controller/HealthyController.java
+++ b/src/main/java/net/geedge/confagent/controller/HealthyController.java
@@ -8,6 +8,8 @@ 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.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -22,7 +24,9 @@ import java.util.Map;
@RequestMapping("healthy")
public class HealthyController extends BaseController{
- private Log log = Log.get();
+ @Autowired
+ private ConfagentUtil confagentUtil;
+ private final static Log log = Log.get();
private UrlBuilder prometheusHealthy;
private UrlBuilder snmpExporterHealthy;
@@ -57,7 +61,7 @@ public class HealthyController extends BaseController{
result.put("blackbox_exporter", checkState(blackboxExporterHealthy.toString()));
if(Tool.StrUtil.isNotBlank(token)){
- if(ConfagentUtil.checkToken(token).getCode() == RCode.SUCCESS.getCode()){
+ if(confagentUtil.checkToken(token).getCode() == RCode.SUCCESS.getCode()){
result.put("auth","TRUE");
}else{
result.put("auth","FALSE");
diff --git a/src/main/java/net/geedge/confagent/controller/PrometheusController.java b/src/main/java/net/geedge/confagent/controller/PrometheusController.java
index 5b6a948..b5c5693 100644
--- a/src/main/java/net/geedge/confagent/controller/PrometheusController.java
+++ b/src/main/java/net/geedge/confagent/controller/PrometheusController.java
@@ -12,6 +12,8 @@ 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.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
@@ -29,8 +31,11 @@ import java.util.*;
@RestController
@RequestMapping("/prometheus")
public class PrometheusController extends BaseController{
- private Log log = Log.get();
+ private final static Log log = Log.get();
+ @Autowired
+ private ConfagentUtil confagentUtil;
+
@Value("${confagent.prometheus.query.auth:true}")
private Boolean queryAuth;
@@ -116,7 +121,7 @@ public class PrometheusController extends BaseController{
String promPath = request.getServletPath().replace("/prometheus/proxy","");
String token = request.getHeader("Authorization");
- R r = ConfagentUtil.checkToken(token);
+ R r = confagentUtil.checkToken(token);
Boolean isQuery=false;
diff --git a/src/main/java/net/geedge/confagent/interceptor/TokenInterceptor.java b/src/main/java/net/geedge/confagent/interceptor/TokenInterceptor.java
index 8d687bd..c2965b2 100644
--- a/src/main/java/net/geedge/confagent/interceptor/TokenInterceptor.java
+++ b/src/main/java/net/geedge/confagent/interceptor/TokenInterceptor.java
@@ -1,43 +1,46 @@
package net.geedge.confagent.interceptor;
+import java.io.IOException;
+import java.io.PrintWriter;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.web.method.HandlerMethod;
+import org.springframework.web.servlet.HandlerInterceptor;
+
import cn.hutool.log.Log;
import net.geedge.confagent.annotation.UnCheckToken;
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.stereotype.Component;
-import org.springframework.web.method.HandlerMethod;
-import org.springframework.web.servlet.HandlerInterceptor;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
-import java.io.PrintWriter;
@Component
public class TokenInterceptor implements HandlerInterceptor {
- private Log log = Log.get();
+ private final static Log log = Log.get();
+ @Autowired
+ private ConfagentUtil confagentUtil;
+
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if(handler instanceof HandlerMethod){
HandlerMethod handlerMethod = (HandlerMethod) handler;
Boolean hasAnnotation = Tool.AnnotationUtil.hasAnnotation(handlerMethod.getMethod(), UnCheckToken.class);
-
if(hasAnnotation){
return true;
}
-
hasAnnotation = Tool.AnnotationUtil.hasAnnotation(handlerMethod.getBeanType(),UnCheckToken.class);
-
if(hasAnnotation){
return true;
}
}
String token = request.getHeader("Authorization");
- R r = ConfagentUtil.checkToken(token);
+ R r = confagentUtil.checkToken(token);
if(r.getCode() == RCode.SUCCESS.getCode()){
return true;
}
diff --git a/src/main/java/net/geedge/confagent/util/ConfagentUtil.java b/src/main/java/net/geedge/confagent/util/ConfagentUtil.java
index 58faacc..b7487ea 100644
--- a/src/main/java/net/geedge/confagent/util/ConfagentUtil.java
+++ b/src/main/java/net/geedge/confagent/util/ConfagentUtil.java
@@ -1,35 +1,39 @@
package net.geedge.confagent.util;
-import cn.hutool.core.io.IORuntimeException;
-import cn.hutool.log.Log;
-import org.springframework.context.EnvironmentAware;
-import org.springframework.core.env.Environment;
-import org.springframework.stereotype.Component;
-
-import javax.servlet.http.HttpServletResponse;
-import java.io.*;
+import java.io.File;
+import java.io.IOException;
+import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-@Component
-public class ConfagentUtil implements EnvironmentAware {
- private static Environment environment;
- private static Log log = Log.get();
+import javax.servlet.http.HttpServletResponse;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.core.annotation.Order;
+import cn.hutool.core.io.IORuntimeException;
+import cn.hutool.log.Log;
+
+@Configuration
+@Order(value = 1)
+public class ConfagentUtil {
+ private final static Log log = Log.get();
+
+ @Value("${confagent.tokenFile:config/token.auth}")
+ protected String tokenFile; //token文件位置
+ private static String rootPath = Tool.WebPathUtil.getRootPath();
/**
* @Description 读取token文件中的token
* @Author rui
* @Date 2021/3/25
*/
- public static String readToken() throws IORuntimeException {
- String resourcePath = environment.getProperty("confagent.resourcePath","./config");
- String tokenFile = environment.getProperty("confagent.tokenFile","token.auth");
-
- String token = Tool.FileUtil.readString(Tool.YamlUtil.getFileAbsolutePath(resourcePath,tokenFile), Tool.CharsetUtil.UTF_8);
-
+ 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;
}
@@ -38,12 +42,12 @@ public class ConfagentUtil implements EnvironmentAware {
* @Author rui
* @Date 2021/3/25
*/
- public static R checkToken(String token){
+ public R checkToken(String token){
if(Tool.StrUtil.isBlank(token)){
return R.error(RCode.AUTH_TOKEN_ISNULL);
}
try{
- String readToken = readToken();
+ String readToken = this.readToken();
if(Tool.StrUtil.equals(readToken,token)){
return R.ok();
}
@@ -96,11 +100,6 @@ public class ConfagentUtil implements EnvironmentAware {
}
- @Override
- public void setEnvironment(Environment env) {
- environment = env;
- }
-
public static void writeResponse(HttpServletResponse response, Object o) {
OutputStream outputStream = null;
diff --git a/src/main/java/net/geedge/confagent/util/Tool.java b/src/main/java/net/geedge/confagent/util/Tool.java
index ad195e1..0ef68a4 100644
--- a/src/main/java/net/geedge/confagent/util/Tool.java
+++ b/src/main/java/net/geedge/confagent/util/Tool.java
@@ -1,11 +1,17 @@
package net.geedge.confagent.util;
-import javax.tools.JavaCompiler;
-import javax.tools.JavaFileObject;
-import java.awt.*;
-import java.lang.ref.*;
+import java.awt.Graphics;
+import java.awt.Robot;
+import java.io.File;
+import java.io.UnsupportedEncodingException;
+import java.lang.ref.PhantomReference;
+import java.lang.ref.Reference;
+import java.lang.ref.ReferenceQueue;
+import java.lang.ref.SoftReference;
+import java.lang.ref.WeakReference;
import java.lang.reflect.Type;
import java.math.BigDecimal;
+import java.net.URLDecoder;
import java.nio.ByteBuffer;
import java.time.LocalDateTime;
import java.time.temporal.Temporal;
@@ -15,6 +21,9 @@ import java.util.Collection;
import java.util.Iterator;
import java.util.Spliterator;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+
public class Tool {
/**
@@ -1061,4 +1070,44 @@ public class Tool {
public static class JSONUtil extends net.geedge.confagent.util.JSONUtil{}
public static class YamlUtil extends net.geedge.confagent.util.YamlUtil{}
+
+ /**
+ * 获取项目中各种路径
+ * @author ThinkPad
+ *
+ */
+ public static class WebPathUtil {
+ /**
+ * 如果已打成jar包,则返回jar包所在目录
+ * 如果未打成jar,则返回target所在目录
+ * @return
+ */
+ public static String getClassPath() {
+ try {
+ // 项目的编译文件的根目录
+ String path = URLDecoder.decode(Tool.class.getResource("/").getPath(), "utf-8");
+ if (path.startsWith("file:")) {
+ int i = path.indexOf(".jar!");
+ if(i == -1){
+ i = path.indexOf(".xjar!");
+ }
+ path = path.substring(0, i);
+ path = path.replaceFirst("file:", "");
+ // 项目所在的目录
+ return Tool.FileUtil.newFile(path).getParentFile().getAbsolutePath();
+ }else {
+ //classpath 路径
+ return Tool.FileUtil.newFile(path).getAbsolutePath();
+ }
+ } catch (UnsupportedEncodingException e) {
+ return null;
+ }
+ }
+
+ public static String getRootPath() {
+ File file = Tool.FileUtil.file(WebPathUtil.getClassPath());
+ return file.getAbsolutePath();
+ }
+
+ }
}
diff --git a/src/main/resources/auth.yml b/src/main/resources/config/auth.yml
index 6f07be4..6f07be4 100644
--- a/src/main/resources/auth.yml
+++ b/src/main/resources/config/auth.yml
diff --git a/src/main/resources/token.auth b/src/main/resources/config/token.auth
index e9a9ea1..e9a9ea1 100644
--- a/src/main/resources/token.auth
+++ b/src/main/resources/config/token.auth