Browse Source

亿道次元EIS 发布之后连接socket报错测试,数据源拦截器调整

xiaost 1 year ago
parent
commit
604ca82f9e

+ 3 - 0
src/main/java/com/uas/eis/UasEisApplication.java

@@ -1,7 +1,9 @@
 package com.uas.eis;
 
+import com.uas.eis.core.config.DataSourceProperties;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.cache.annotation.EnableCaching;
 import org.springframework.context.annotation.Import;
 import org.springframework.scheduling.annotation.EnableScheduling;
@@ -13,6 +15,7 @@ import com.uas.eis.core.support.TokenPropertiesListener;
 @EnableCaching
 @EnableScheduling
 @Import({DynamicDataSourceRegister.class})
+@EnableConfigurationProperties(DataSourceProperties.class)
 public class UasEisApplication {
 	public static void main(String[] args) {
 		SpringApplication application = new SpringApplication(UasEisApplication.class);

+ 4 - 1
src/main/java/com/uas/eis/core/WebAppConfig.java

@@ -5,6 +5,7 @@ import com.uas.eis.core.support.ApiSignLoginInterceptor;
 import com.uas.eis.core.support.DataSourceInterceptor;
 import com.uas.eis.core.support.LoginInterceptor;
 import com.uas.eis.core.support.MesHelperApiLoginInterceptor;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.http.converter.HttpMessageConverter;
@@ -21,6 +22,8 @@ import java.util.List;
 @Configuration
 public class WebAppConfig extends WebMvcConfigurationSupport{
 
+	@Autowired
+	private DataSourceInterceptor dataSourceInterceptor;
 	@Bean
 	public LoginInterceptor loginInterceptor(){
 		return new LoginInterceptor();
@@ -38,11 +41,11 @@ public class WebAppConfig extends WebMvcConfigurationSupport{
 
 	@Override
 	public void addInterceptors(InterceptorRegistry registry){
+		registry.addInterceptor(dataSourceInterceptor).addPathPatterns("/*/**");
 		registry.addInterceptor(apiSignLoginInterceptor()).addPathPatterns("/api/**","/mes/**")
 				.excludePathPatterns("/login", "/erp/**");
 		registry.addInterceptor(mesHelperApiLoginInterceptor()).addPathPatterns("/MES/helper/**");
 		//registry.addInterceptor(loginInterceptor()).addPathPatterns("/**").excludePathPatterns("/EIS/login");
-		registry.addInterceptor(new DataSourceInterceptor()).addPathPatterns("/*/**");
 	}
 	
 	@Bean

+ 48 - 0
src/main/java/com/uas/eis/core/config/DataSourceProperties.java

@@ -0,0 +1,48 @@
+package com.uas.eis.core.config;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+@Component
+@ConfigurationProperties(prefix = "spring.datasource")
+public class DataSourceProperties {
+    public String getUrl() {
+        return url;
+    }
+
+    public void setUrl(String url) {
+        this.url = url;
+    }
+
+    public String getUsername() {
+        return username;
+    }
+
+    public void setUsername(String username) {
+        this.username = username;
+    }
+
+    public String getPassword() {
+        return password;
+    }
+
+    public void setPassword(String password) {
+        this.password = password;
+    }
+
+    public String getDriverClassName() {
+        return driverClassName;
+    }
+
+    public void setDriverClassName(String driverClassName) {
+        this.driverClassName = driverClassName;
+    }
+
+    private String url;
+    private String username;
+    private String password;
+    private String driverClassName;
+
+    // standard getters and setters
+    // ...
+}

+ 1 - 1
src/main/java/com/uas/eis/core/support/ApiSignLoginInterceptor.java

@@ -39,7 +39,7 @@ public class ApiSignLoginInterceptor extends HandlerInterceptorAdapter {
         String timestamp = request.getHeader(TIMESTAMP_KEY);
         String accessKey = request.getHeader(ACCESS_KEY);
         String requestId = request.getHeader(RequestId);
-       // System.out.println("master:"+SpObserver.getSp());
+        System.out.println("master:"+SpObserver.getSp());
         //String accessSecret = tokenConfig.get(accessKey);
         //改用中心账套表取账户密码
         Object accessSecret_O = baseDao.getFieldDataByCondition("APIEMPLOYEE", "AE_SECRET", "AE_KEY='" + accessKey + "'");

+ 22 - 3
src/main/java/com/uas/eis/core/support/DataSourceInterceptor.java

@@ -3,17 +3,36 @@ package com.uas.eis.core.support;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import com.uas.eis.core.config.DataSourceProperties;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.bind.RelaxedPropertyResolver;
+import org.springframework.stereotype.Component;
 import org.springframework.web.servlet.HandlerInterceptor;
 import org.springframework.web.servlet.ModelAndView;
 
 import com.uas.eis.core.config.SpObserver;
 
+@Component
 public class DataSourceInterceptor implements HandlerInterceptor{
-	
-	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
+	@Value("${spring.datasource.username}")
+	private String username;
+
+	private final DataSourceProperties dataSourceProperties;
+     @Autowired
+    public DataSourceInterceptor(DataSourceProperties dataSourceProperties) {
+        this.dataSourceProperties = dataSourceProperties;
+    }
+
+    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
 		String master = request.getParameter("master");
-		if(master != null)
+		if(master != null) {
 			SpObserver.putSp(master);
+		}else{
+			String a = dataSourceProperties.getUsername();
+           SpObserver.putSp(a);
+		}
+
 		return true;
 	}