wangwei 6 rokov pred
rodič
commit
b072aa88be

+ 27 - 35
examcloud-exchange-starter/src/main/java/cn/com/qmth/examcloud/exchange/config/EnterpriseAccessInterceptor.java

@@ -33,14 +33,12 @@ import cn.com.qmth.examcloud.web.support.StatusResponse;
  */
 public final class EnterpriseAccessInterceptor implements HandlerInterceptor {
 
-	private static final ExamCloudLog LOG = ExamCloudLogFactory
-			.getLog(EnterpriseAccessInterceptor.class);
+	private static final ExamCloudLog LOG = ExamCloudLogFactory.getLog(EnterpriseAccessInterceptor.class);
 
 	/**
 	 * 接口日志
 	 */
-	protected static final ExamCloudLog INTERFACE_LOG = ExamCloudLogFactory
-			.getLog("INTERFACE_LOGGER");
+	protected static final ExamCloudLog INTERFACE_LOG = ExamCloudLogFactory.getLog("INTERFACE_LOGGER");
 
 	/**
 	 * redis client
@@ -49,13 +47,24 @@ public final class EnterpriseAccessInterceptor implements HandlerInterceptor {
 
 	private CommonCloudService commonCloudService;
 
+	/**
+	 * 构造函数
+	 * 
+	 * @param redisClient
+	 * @param commonCloudService
+	 */
+	public EnterpriseAccessInterceptor(RedisClient redisClient, CommonCloudService commonCloudService) {
+		super();
+		this.redisClient = redisClient;
+		this.commonCloudService = commonCloudService;
+	}
+
 	@Override
-	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
-			Object handler) throws Exception {
+	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
+			throws Exception {
 		LOG.debug("preHandle... ...");
 
-		ApiInfo apiInfo = (ApiInfo) request
-				.getAttribute(HttpServletRequestAttribute.$_API_INFO.name());
+		ApiInfo apiInfo = (ApiInfo) request.getAttribute(HttpServletRequestAttribute.$_API_INFO.name());
 
 		Class<?> ctrClass = apiInfo.getBeanType();
 
@@ -70,8 +79,7 @@ public final class EnterpriseAccessInterceptor implements HandlerInterceptor {
 		if (StringUtils.isBlank(appId)) {
 			// 403
 			response.setStatus(HttpStatus.FORBIDDEN.value());
-			ServletUtil.returnJson(new StatusResponse("403", "'App-Id'('appId') is blank"),
-					response);
+			ServletUtil.returnJson(new StatusResponse("403", "'App-Id'('appId') is blank"), response);
 			return false;
 		}
 
@@ -82,8 +90,7 @@ public final class EnterpriseAccessInterceptor implements HandlerInterceptor {
 		if (StringUtils.isBlank(rootOrgId)) {
 			// 403
 			response.setStatus(HttpStatus.FORBIDDEN.value());
-			ServletUtil.returnJson(new StatusResponse("403", "'Root-Org-Id'('rootOrgId') is blank"),
-					response);
+			ServletUtil.returnJson(new StatusResponse("403", "'Root-Org-Id'('rootOrgId') is blank"), response);
 			return false;
 		}
 		Long rootOrgIdLong = null;
@@ -92,9 +99,7 @@ public final class EnterpriseAccessInterceptor implements HandlerInterceptor {
 		} catch (Exception e) {
 			// 403
 			response.setStatus(HttpStatus.FORBIDDEN.value());
-			ServletUtil.returnJson(
-					new StatusResponse("403", "'Root-Org-Id'('rootOrgId') must be a long"),
-					response);
+			ServletUtil.returnJson(new StatusResponse("403", "'Root-Org-Id'('rootOrgId') must be a long"), response);
 			return false;
 		}
 
@@ -105,8 +110,7 @@ public final class EnterpriseAccessInterceptor implements HandlerInterceptor {
 		if (StringUtils.isBlank(accessToken)) {
 			// 403
 			response.setStatus(HttpStatus.FORBIDDEN.value());
-			ServletUtil.returnJson(
-					new StatusResponse("403", "'Access-Token'('access_token') is blank"), response);
+			ServletUtil.returnJson(new StatusResponse("403", "'Access-Token'('access_token') is blank"), response);
 			return false;
 		}
 
@@ -123,18 +127,15 @@ public final class EnterpriseAccessInterceptor implements HandlerInterceptor {
 		} catch (Exception e) {
 			// 403
 			response.setStatus(HttpStatus.FORBIDDEN.value());
-			ServletUtil.returnJson(new StatusResponse("403", "'timestamp' must be a long"),
-					response);
+			ServletUtil.returnJson(new StatusResponse("403", "'timestamp' must be a long"), response);
 			return false;
 		}
 
-		request.setAttribute(HttpServletRequestAttribute.$_ENTERPRISE_ROOT_ORG_ID.name(),
-				rootOrgIdLong);
+		request.setAttribute(HttpServletRequestAttribute.$_ENTERPRISE_ROOT_ORG_ID.name(), rootOrgIdLong);
 
 		String key = "$_A_:" + rootOrgId + "_" + appId;
 
-		ThirdPartyAccessBean thirdPartyAccessBean = redisClient.get(key,
-				ThirdPartyAccessBean.class);
+		ThirdPartyAccessBean thirdPartyAccessBean = redisClient.get(key, ThirdPartyAccessBean.class);
 
 		if (null == thirdPartyAccessBean) {
 			thirdPartyAccessBean = getThirdPartyAccessInfo(rootOrgIdLong, appId);
@@ -159,8 +160,7 @@ public final class EnterpriseAccessInterceptor implements HandlerInterceptor {
 		if (!hexAscii.equalsIgnoreCase(accessToken)) {
 			// 403
 			response.setStatus(HttpStatus.FORBIDDEN.value());
-			ServletUtil.returnJson(
-					new StatusResponse("403", "'Access-Token'('access_token') is wrong"), response);
+			ServletUtil.returnJson(new StatusResponse("403", "'Access-Token'('access_token') is wrong"), response);
 			return false;
 		}
 
@@ -184,17 +184,9 @@ public final class EnterpriseAccessInterceptor implements HandlerInterceptor {
 	}
 
 	@Override
-	public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
-			Object handler, Exception ex) throws Exception {
+	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
+			throws Exception {
 		LOG.debug("afterCompletion... ...");
 	}
 
-	public void setRedisClient(RedisClient redisClient) {
-		this.redisClient = redisClient;
-	}
-
-	public void setCommonCloudService(CommonCloudService commonCloudService) {
-		this.commonCloudService = commonCloudService;
-	}
-
 }

+ 9 - 5
examcloud-exchange-starter/src/main/java/cn/com/qmth/examcloud/exchange/config/ExamCloudWebMvcConfigurer.java

@@ -6,6 +6,7 @@ import org.springframework.web.servlet.config.annotation.CorsRegistry;
 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
+import cn.com.qmth.examcloud.core.basic.api.CommonCloudService;
 import cn.com.qmth.examcloud.web.interceptor.FirstInterceptor;
 import cn.com.qmth.examcloud.web.redis.RedisClient;
 import cn.com.qmth.examcloud.web.security.RequestPermissionInterceptor;
@@ -25,24 +26,27 @@ public class ExamCloudWebMvcConfigurer implements WebMvcConfigurer {
 	@Autowired
 	ResourceManager resourceManager;
 
+	@Autowired
+	CommonCloudService commonCloudService;
+
 	@Autowired
 	RedisClient redisClient;
 
 	@Override
 	public void addInterceptors(InterceptorRegistry registry) {
 		registry.addInterceptor(new FirstInterceptor()).addPathPatterns("/**");
-		registry.addInterceptor(new EnterpriseAccessInterceptor()).addPathPatterns("/**");
+		registry.addInterceptor(new EnterpriseAccessInterceptor(redisClient, commonCloudService))
+				.addPathPatterns("/**");
 		registry.addInterceptor(new RpcInterceptor(resourceManager)).addPathPatterns("/**");
 
-		RequestPermissionInterceptor permissionInterceptor = new RequestPermissionInterceptor(
-				resourceManager, redisClient);
+		RequestPermissionInterceptor permissionInterceptor = new RequestPermissionInterceptor(resourceManager,
+				redisClient);
 		registry.addInterceptor(permissionInterceptor).addPathPatterns("/**");
 	}
 
 	@Override
 	public void addCorsMappings(CorsRegistry registry) {
-		registry.addMapping("/**").allowedOrigins("*").allowCredentials(false).allowedMethods("*")
-				.maxAge(3600);
+		registry.addMapping("/**").allowedOrigins("*").allowCredentials(false).allowedMethods("*").maxAge(3600);
 	}
 
 }