Ver Fonte

修复Token过长的BUG。

yangzhijie há 5 anos atrás
pai
commit
eb89b9dde6

+ 32 - 49
.classpath

@@ -1,49 +1,32 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<classpath>
-	<classpathentry kind="src" output="target/classes" path="src/main/java">
-		<attributes>
-			<attribute name="optional" value="true"/>
-			<attribute name="maven.pomderived" value="true"/>
-		</attributes>
-	</classpathentry>
-	<classpathentry kind="src" output="target/test-classes" path="src/test/java">
-		<attributes>
-			<attribute name="optional" value="true"/>
-			<attribute name="maven.pomderived" value="true"/>
-			<attribute name="test" value="true"/>
-		</attributes>
-	</classpathentry>
-	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
-		<attributes>
-			<attribute name="maven.pomderived" value="true"/>
-		</attributes>
-	</classpathentry>
-	<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
-		<attributes>
-			<attribute name="maven.pomderived" value="true"/>
-		</attributes>
-	</classpathentry>
-	<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
-		<attributes>
-			<attribute name="maven.pomderived" value="true"/>
-		</attributes>
-	</classpathentry>
-	<classpathentry kind="src" path="target/generated-sources/annotations">
-		<attributes>
-			<attribute name="optional" value="true"/>
-			<attribute name="maven.pomderived" value="true"/>
-			<attribute name="ignore_optional_problems" value="true"/>
-			<attribute name="m2e-apt" value="true"/>
-		</attributes>
-	</classpathentry>
-	<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
-		<attributes>
-			<attribute name="optional" value="true"/>
-			<attribute name="maven.pomderived" value="true"/>
-			<attribute name="ignore_optional_problems" value="true"/>
-			<attribute name="m2e-apt" value="true"/>
-			<attribute name="test" value="true"/>
-		</attributes>
-	</classpathentry>
-	<classpathentry kind="output" path="target/classes"/>
-</classpath>
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" output="target/classes" path="src/main/java">
+		<attributes>
+			<attribute name="optional" value="true"/>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="src" output="target/test-classes" path="src/test/java">
+		<attributes>
+			<attribute name="optional" value="true"/>
+			<attribute name="maven.pomderived" value="true"/>
+			<attribute name="test" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
+		<attributes>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
+		<attributes>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
+		<attributes>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="output" path="target/classes"/>
+</classpath>

+ 85 - 85
src/main/java/com/leanwo/gateway/util/FeignConfig.java

@@ -1,86 +1,86 @@
-package com.leanwo.gateway.util;
-
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
-import java.util.UUID;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.BeansException;
-import org.springframework.beans.factory.ObjectFactory;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
-import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
-import org.springframework.cloud.openfeign.support.SpringDecoder;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.http.MediaType;
-import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
-
-import com.auth0.jwt.JWT;
-import com.auth0.jwt.algorithms.Algorithm;
-import com.auth0.jwt.exceptions.JWTCreationException;
-import com.leanwo.gateway.exception.AuthorizeException;
-import com.leanwo.gateway.exception.TokenException;
-
-import feign.RequestInterceptor;
-import feign.RequestTemplate;
-import feign.codec.Decoder;
-
-
-/**
- * Feign配置 使用FeignClient进行服务间调用,传递headers信息
- */
-@Configuration
-public class FeignConfig implements RequestInterceptor {
-
-    private final static Logger logger = LoggerFactory.getLogger(FeignConfig.class.getName());
-
-
-    
-    @Override
-    public void apply(RequestTemplate requestTemplate) {
-        LoginContext loginContext = LoginContextUtil.getLoginContext();
-
-        if (loginContext == null) {
-            throw new AuthorizeException("您没有权限调用业务服务器。");
-        }
-        String token = loginContext.getToken();
-        logger.info("jwtToken:" + token);
-        if (StringUtil.isNotEmpty(token)) {
-            // header里面添加jwt-token
-            requestTemplate.header("token", loginContext.getToken());
-            requestTemplate.header("account", loginContext.getAccountId().toString());
-            logger.info("header:" + requestTemplate.headers());
-        }
-        if(loginContext.getAccountId() != null) {
-        	requestTemplate.header("account", loginContext.getAccountId().toString());
-        	logger.info("account:" + loginContext.getAccountId());
-        }
-    }
-    
-    
-    @Bean
-    public Decoder feignDecoder() {
-        return new ResponseEntityDecoder(new SpringDecoder(feignHttpMessageConverter()));
-    }
-
-    public ObjectFactory<HttpMessageConverters> feignHttpMessageConverter() {
-        final HttpMessageConverters httpMessageConverters = new HttpMessageConverters(new GateWayMappingJackson2HttpMessageConverter());
-        return new ObjectFactory<HttpMessageConverters>() {
-            @Override
-            public HttpMessageConverters getObject() throws BeansException {
-                return httpMessageConverters;
-            }
-        };
-    }
-
-    public class GateWayMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
-        GateWayMappingJackson2HttpMessageConverter(){
-            List<MediaType> mediaTypes = new ArrayList<>();
-            mediaTypes.add(MediaType.valueOf(MediaType.TEXT_HTML_VALUE + ";charset=UTF-8")); 
-            setSupportedMediaTypes(mediaTypes);
-        }
-    }
+package com.leanwo.gateway.util;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.UUID;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.ObjectFactory;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
+import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
+import org.springframework.cloud.openfeign.support.SpringDecoder;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.http.MediaType;
+import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
+
+import com.auth0.jwt.JWT;
+import com.auth0.jwt.algorithms.Algorithm;
+import com.auth0.jwt.exceptions.JWTCreationException;
+import com.leanwo.gateway.exception.AuthorizeException;
+import com.leanwo.gateway.exception.TokenException;
+
+import feign.RequestInterceptor;
+import feign.RequestTemplate;
+import feign.codec.Decoder;
+
+
+/**
+ * Feign配置 使用FeignClient进行服务间调用,传递headers信息
+ */
+@Configuration
+public class FeignConfig implements RequestInterceptor {
+
+    private final static Logger logger = LoggerFactory.getLogger(FeignConfig.class.getName());
+
+
+    
+    @Override
+    public void apply(RequestTemplate requestTemplate) {
+        LoginContext loginContext = LoginContextUtil.getLoginContext();
+
+        if (loginContext == null) {
+            throw new AuthorizeException("您没有权限调用业务服务器。");
+        }
+        String token = TokenUtil.getToken();
+        logger.info("jwtToken:" + token);
+        if (StringUtil.isNotEmpty(token)) {
+            // header里面添加jwt-token
+            requestTemplate.header("token", token);
+            requestTemplate.header("account", loginContext.getAccountId().toString());
+            logger.info("header:" + requestTemplate.headers());
+        }
+        if(loginContext.getAccountId() != null) {
+        	requestTemplate.header("account", loginContext.getAccountId().toString());
+        	logger.info("account:" + loginContext.getAccountId());
+        }
+    }
+    
+    
+    @Bean
+    public Decoder feignDecoder() {
+        return new ResponseEntityDecoder(new SpringDecoder(feignHttpMessageConverter()));
+    }
+
+    public ObjectFactory<HttpMessageConverters> feignHttpMessageConverter() {
+        final HttpMessageConverters httpMessageConverters = new HttpMessageConverters(new GateWayMappingJackson2HttpMessageConverter());
+        return new ObjectFactory<HttpMessageConverters>() {
+            @Override
+            public HttpMessageConverters getObject() throws BeansException {
+                return httpMessageConverters;
+            }
+        };
+    }
+
+    public class GateWayMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
+        GateWayMappingJackson2HttpMessageConverter(){
+            List<MediaType> mediaTypes = new ArrayList<>();
+            mediaTypes.add(MediaType.valueOf(MediaType.TEXT_HTML_VALUE + ";charset=UTF-8")); 
+            setSupportedMediaTypes(mediaTypes);
+        }
+    }
 }

+ 135 - 286
src/main/java/com/leanwo/gateway/util/LoginContext.java

@@ -1,286 +1,135 @@
-package com.leanwo.gateway.util;
-
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
-
-public class LoginContext {
-
-	/** 账套ID. */
-	private Long accountId;
-
-	/** 账套日期. */
-	private Date accountDate;
-	
-	/** 登陆人员ID. */
-	private Long userId;
-
-	/** 登陆角色. */
-	private Long roleId;
-
-	/** 登陆公司. */
-	private Long clientId;
-
-	/** 登陆部门. */
-	private Long orgId;
-
-	/** 可查询的所有公司. */
-	private List<Long> clientIds = new ArrayList<Long>();
-
-	/** 可查询公司对应的部门. */
-	private List<Long> orgnizationIds = new ArrayList<Long>();
-	
-	/** 集团公司 + 本公司的上...上级公司 + 本公司的上级公司 + 本公司 */
-	private List<Long> parentClientIds = new ArrayList<Long>();
-
-	/**  集团公司 + 所有的子公司 + 所有的子子公司. */
-	private List<Long> rootSubClientIds = new ArrayList<Long>();
-	
-	/** 语言Id. */
-	private Long languageId;
-
-	/** 集团公司ID. */
-	private Long topClientId;
-
-	/** 登录Token. */
-	private String token;
-	
-	
-	
-	/**
-	 * Gets the 集团公司 + 本公司的上..上级公司 + 本公司的上级公司 + 本公司.
-	 *
-	 * @return the 集团公司 + 本公司的上
-	 */
-	public List<Long> getParentClientIds() {
-		return parentClientIds;
-	}
-
-	/**
-	 * Sets the 集团公司 + 本公司的上..上级公司 + 本公司的上级公司 + 本公司.
-	 *
-	 * @param parentClientIds the new 集团公司 + 本公司的上
-	 */
-	public void setParentClientIds(List<Long> parentClientIds) {
-		this.parentClientIds = parentClientIds;
-	}
-
-	/**
-	 * Gets the 集团公司ID.
-	 *
-	 * @return the 集团公司ID
-	 */
-	public Long getTopClientId() {
-		return topClientId;
-	}
-
-	/**
-	 * Sets the 集团公司ID.
-	 *
-	 * @param topClientId the new 集团公司ID
-	 */
-	public void setTopClientId(Long topClientId) {
-		this.topClientId = topClientId;
-	}
-
-	/**
-	 * Gets the 登陆人员ID.
-	 *
-	 * @return the 登陆人员ID
-	 */
-	public Long getUserId() {
-		return userId;
-	}
-
-	/**
-	 * Sets the 登陆人员ID.
-	 *
-	 * @param userId the new 登陆人员ID
-	 */
-	public void setUserId(Long userId) {
-		this.userId = userId;
-	}
-
-	/**
-	 * Gets the 登陆角色.
-	 *
-	 * @return the 登陆角色
-	 */
-	public Long getRoleId() {
-		return roleId;
-	}
-
-	/**
-	 * Sets the 登陆角色.
-	 *
-	 * @param roleId the new 登陆角色
-	 */
-	public void setRoleId(Long roleId) {
-		this.roleId = roleId;
-	}
-
-	/**
-	 * Gets the 登陆公司.
-	 *
-	 * @return the 登陆公司
-	 */
-	public Long getClientId() {
-		return clientId;
-	}
-
-	/**
-	 * Sets the 登陆公司.
-	 *
-	 * @param clientId the new 登陆公司
-	 */
-	public void setClientId(Long clientId) {
-		this.clientId = clientId;
-	}
-
-	/**
-	 * Gets the 登陆部门.
-	 *
-	 * @return the 登陆部门
-	 */
-	public Long getOrgId() {
-		return orgId;
-	}
-
-	/**
-	 * Sets the 登陆部门.
-	 *
-	 * @param orgId the new 登陆部门
-	 */
-	public void setOrgId(Long orgId) {
-		this.orgId = orgId;
-	}
-
-	/**
-	 * Gets the 可查询公司对应的部门.
-	 *
-	 * @return the 可查询公司对应的部门
-	 */
-	public List<Long> getOrgnizationIds() {
-		return orgnizationIds;
-	}
-
-	/**
-	 * Sets the 可查询公司对应的部门.
-	 *
-	 * @param orgnizationIds the new 可查询公司对应的部门
-	 */
-	public void setOrgnizationIds(List<Long> orgnizationIds) {
-		this.orgnizationIds = orgnizationIds;
-	}
-
-	/**
-	 * Gets the 可查询的所有公司.
-	 *
-	 * @return the 可查询的所有公司
-	 */
-	public List<Long> getClientIds() {
-		return clientIds;
-	}
-
-	/**
-	 * Sets the 可查询的所有公司.
-	 *
-	 * @param clientIds the new 可查询的所有公司
-	 */
-	public void setClientIds(List<Long> clientIds) {
-		this.clientIds = clientIds;
-	}
-
-	/**
-	 * Gets the 集团公司 + 所有的子公司 + 所有的子子公司.
-	 *
-	 * @return the 集团公司 + 所有的子公司 + 所有的子子公司
-	 */
-	public List<Long> getRootSubClientIds() {
-		return rootSubClientIds;
-	}
-
-	/**
-	 * Sets the 集团公司 + 所有的子公司 + 所有的子子公司.
-	 *
-	 * @param rootSubClientIds the new 集团公司 + 所有的子公司 + 所有的子子公司
-	 */
-	public void setRootSubClientIds(List<Long> rootSubClientIds) {
-		this.rootSubClientIds = rootSubClientIds;
-	}
-
-	/**
-	 * Gets the 账套ID.
-	 *
-	 * @return the 账套ID
-	 */
-	public Long getAccountId() {
-		return accountId;
-	}
-
-	/**
-	 * Sets the 账套ID.
-	 *
-	 * @param accountId the new 账套ID
-	 */
-	public void setAccountId(Long accountId) {
-		this.accountId = accountId;
-	}
-
-	/**
-	 * Gets the 语言Id.
-	 *
-	 * @return the 语言Id
-	 */
-	public Long getLanguageId() {
-		return languageId;
-	}
-
-	/**
-	 * Sets the 语言Id.
-	 *
-	 * @param languageId the new 语言Id
-	 */
-	public void setLanguageId(Long languageId) {
-		this.languageId = languageId;
-	}
-
-	/**
-	 * Gets the 账套日期.
-	 *
-	 * @return the 账套日期
-	 */
-	public Date getAccountDate() {
-		return accountDate;
-	}
-
-	/**
-	 * Sets the 账套日期.
-	 *
-	 * @param accountDate the new 账套日期
-	 */
-	public void setAccountDate(Date accountDate) {
-		this.accountDate = accountDate;
-	}
-
-	/**
-	 * Gets the 登录Token.
-	 *
-	 * @return the 登录Token
-	 */
-	public String getToken() {
-		return token;
-	}
-
-	/**
-	 * Sets the 登录Token.
-	 *
-	 * @param token the new 登录Token
-	 */
-	public void setToken(String token) {
-		this.token = token;
-	}
-
-
-
-}
+package com.leanwo.gateway.util;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+public class LoginContext {
+
+	/** 账套ID. */
+	private Long accountId;
+
+	/** 账套日期. */
+	private Date accountDate;
+	
+	/** 登陆人员ID. */
+	private Long userId;
+
+	/** 登陆角色. */
+	private Long roleId;
+
+	/** 登陆公司. */
+	private Long clientId;
+
+	/** 登陆部门. */
+	private Long orgId;
+
+	/**
+	 * Gets the 登陆人员ID.
+	 *
+	 * @return the 登陆人员ID
+	 */
+	public Long getUserId() {
+		return userId;
+	}
+
+	/**
+	 * Sets the 登陆人员ID.
+	 *
+	 * @param userId the new 登陆人员ID
+	 */
+	public void setUserId(Long userId) {
+		this.userId = userId;
+	}
+
+	/**
+	 * Gets the 登陆角色.
+	 *
+	 * @return the 登陆角色
+	 */
+	public Long getRoleId() {
+		return roleId;
+	}
+
+	/**
+	 * Sets the 登陆角色.
+	 *
+	 * @param roleId the new 登陆角色
+	 */
+	public void setRoleId(Long roleId) {
+		this.roleId = roleId;
+	}
+
+	/**
+	 * Gets the 登陆公司.
+	 *
+	 * @return the 登陆公司
+	 */
+	public Long getClientId() {
+		return clientId;
+	}
+
+	/**
+	 * Sets the 登陆公司.
+	 *
+	 * @param clientId the new 登陆公司
+	 */
+	public void setClientId(Long clientId) {
+		this.clientId = clientId;
+	}
+
+	/**
+	 * Gets the 登陆部门.
+	 *
+	 * @return the 登陆部门
+	 */
+	public Long getOrgId() {
+		return orgId;
+	}
+
+	/**
+	 * Sets the 登陆部门.
+	 *
+	 * @param orgId the new 登陆部门
+	 */
+	public void setOrgId(Long orgId) {
+		this.orgId = orgId;
+	}
+
+
+	/**
+	 * Gets the 账套ID.
+	 *
+	 * @return the 账套ID
+	 */
+	public Long getAccountId() {
+		return accountId;
+	}
+
+	/**
+	 * Sets the 账套ID.
+	 *
+	 * @param accountId the new 账套ID
+	 */
+	public void setAccountId(Long accountId) {
+		this.accountId = accountId;
+	}
+
+	/**
+	 * Gets the 账套日期.
+	 *
+	 * @return the 账套日期
+	 */
+	public Date getAccountDate() {
+		return accountDate;
+	}
+
+	/**
+	 * Sets the 账套日期.
+	 *
+	 * @param accountDate the new 账套日期
+	 */
+	public void setAccountDate(Date accountDate) {
+		this.accountDate = accountDate;
+	}
+}

+ 34 - 0
src/main/java/com/leanwo/gateway/util/TokenUtil.java

@@ -0,0 +1,34 @@
+package com.leanwo.gateway.util;
+
+/**
+ * 线程变量Token
+ * @author YangZhiJie 2021-03-30
+ *
+ */
+public class TokenUtil {
+	
+	private static ThreadLocal<String> TOKEN_CONTEXT_HOLDER = new ThreadLocal<String>();
+	
+	/**
+	 * 从线程获取Token
+	 * @return
+	 */
+	public static String getToken(){
+		return TOKEN_CONTEXT_HOLDER.get();
+	}
+	
+	/**
+	 * 设置线程的Token
+	 * @param token 
+	 */
+	public static void setToken(String token){
+		TOKEN_CONTEXT_HOLDER.set(token);
+	}
+	
+	/**
+	 * 清空线程的Token
+	 */
+	public static void clear(){
+		TOKEN_CONTEXT_HOLDER.set(null);
+	}
+}

+ 0 - 2
src/main/resources/Application.yml

@@ -1,8 +1,6 @@
 #端口号
 server:
   port: 80
-  maxPostSize: -10
-  maxHttpHeaderSize: 102400
   token:
     issuer: leanwo
     secret: leanwo123123