Przeglądaj źródła

update:401/404异常返回统一格式

yonge 5 lat temu
rodzic
commit
2100112761

+ 1 - 4
mec-auth/mec-auth-server/src/main/java/com/ym/mec/auth/config/ResourceServerConfig.java

@@ -1,7 +1,5 @@
 package com.ym.mec.auth.config;
 
-import javax.servlet.http.HttpServletResponse;
-
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
@@ -27,8 +25,7 @@ public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
 
 	@Override
 	public void configure(HttpSecurity http) throws Exception {
-		http.csrf().disable().exceptionHandling()
-				.authenticationEntryPoint((request, response, authException) -> response.sendError(HttpServletResponse.SC_UNAUTHORIZED)).and()
+		http.csrf().disable().exceptionHandling().accessDeniedHandler(baseAccessDeniedHandler).authenticationEntryPoint(baseAuthenticationEntryPoint).and()
 				.authorizeRequests().antMatchers("/task/**").hasIpAddress("0.0.0.0/0").anyRequest().authenticated().and().httpBasic();
 	}
 

+ 3 - 4
mec-common/common-core/src/main/java/com/ym/mec/common/service/BaseService.java

@@ -7,14 +7,13 @@ package com.ym.mec.common.service;
  * @author pengdc
  * @create 2015年7月13日
  */
-import com.ym.mec.common.page.PageInfo;
-import com.ym.mec.common.page.QueryInfo;
-
-import java.io.IOException;
 import java.io.Serializable;
 import java.util.List;
 import java.util.Map;
 
+import com.ym.mec.common.page.PageInfo;
+import com.ym.mec.common.page.QueryInfo;
+
 public interface BaseService<PK extends Serializable, T> {
 	/**
 	 * 通过主键id获取对象

+ 99 - 0
mec-gateway/mec-gateway-web/src/main/java/com/ym/mec/gateway/web/controller/ErrorHandlerController.java

@@ -0,0 +1,99 @@
+package com.ym.mec.gateway.web.controller;
+
+import org.apache.commons.lang3.exception.ExceptionUtils;
+import org.springframework.boot.web.servlet.error.ErrorController;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.RestController;
+
+import com.netflix.zuul.context.RequestContext;
+import com.netflix.zuul.exception.ZuulException;
+
+@RestController
+public class ErrorHandlerController implements ErrorController {
+
+	@Override
+	public String getErrorPath() {
+		return "/error";
+	}
+
+	@RequestMapping("/error")
+	@ResponseBody
+	public Object error() {
+		RequestContext ctx = RequestContext.getCurrentContext();
+		ZuulException exception = (ZuulException) ctx.getThrowable();
+		
+		Throwable e = ExceptionUtils.getRootCause(exception);
+		if (e == null) {
+			e = exception;
+		}
+
+		HttpResponseResult result = new HttpResponseResult();
+		result.setCode(exception.nStatusCode);
+		result.setMsg(exception.getMessage());
+		result.setStatus(false);
+
+		return result;
+	}
+
+	class HttpResponseResult {
+
+		public final static int TIME_OUT = -1;
+
+		private boolean status = true;
+		private String msg;
+		private Object data;
+		private int code;
+
+		public HttpResponseResult(boolean status, int code, Object data, String message) {
+			this.status = status;
+			this.msg = message;
+			this.data = data;
+			this.code = code;
+		}
+
+		public HttpResponseResult() {
+		}
+
+		public int getCode() {
+			return code;
+		}
+
+		public void setCode(int code) {
+			this.code = code;
+		}
+
+		public boolean getStatus() {
+			return status;
+		}
+
+		public void setStatus(boolean status) {
+			this.status = status;
+		}
+
+		public String getMsg() {
+			return msg;
+		}
+
+		public void setMsg(String msg) {
+			this.msg = msg;
+		}
+
+		public Object getData() {
+			return data;
+		}
+
+		public void setData(Object data) {
+			this.data = data;
+		}
+
+		public void success() {
+			this.status = true;
+		}
+
+		public void fail() {
+			this.status = false;
+		}
+
+	}
+}

+ 7 - 5
mec-gateway/mec-gateway-web/src/main/resources/application.yml

@@ -10,7 +10,9 @@ eureka:
     serviceUrl:
       defaultZone: http://admin:admin123@localhost:8761/eureka/eureka/
 
-      
+ribbon:
+  ReadTimeout: 10000
+  SocketTimeout: 3000      
 
 ### 配置网关反向代理    
 zuul:
@@ -47,10 +49,10 @@ zuul:
   ignored-services: eureka-server
   #重试
   retryable: false
-  #请求处理超时
-  ReadTimeout: 6000
-  #连接超时
-  ConnectTimeout: 1000
+  #请求处理超时--只针对url的路由
+  ReadTimeout: 60000
+  #连接超时--只针对url的路由
+  ConnectTimeout: 10000
   sensitiveHeaders: 
   
 hystrix: