|
|
@ -1,19 +1,21 @@ |
|
|
|
package com.gitee.sop.gateway.controller; |
|
|
|
package com.gitee.sop.gateway.controller; |
|
|
|
|
|
|
|
|
|
|
|
import com.gitee.sop.gatewaycommon.bean.SopConstants; |
|
|
|
import com.gitee.sop.gatewaycommon.bean.SopConstants; |
|
|
|
|
|
|
|
import com.gitee.sop.gatewaycommon.zuul.ValidateService; |
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
|
|
|
|
|
|
|
|
import javax.servlet.ServletException; |
|
|
|
|
|
|
|
import javax.servlet.annotation.WebServlet; |
|
|
|
import javax.servlet.annotation.WebServlet; |
|
|
|
import javax.servlet.http.HttpServlet; |
|
|
|
import javax.servlet.http.HttpServlet; |
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
import java.io.IOException; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 传统web开发入口 |
|
|
|
* 传统web开发入口 |
|
|
|
* @author tanghc |
|
|
|
* @author tanghc |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
@Slf4j |
|
|
|
@WebServlet(urlPatterns = "/rest/*") |
|
|
|
@WebServlet(urlPatterns = "/rest/*") |
|
|
|
public class RestServlet extends HttpServlet { |
|
|
|
public class RestServlet extends HttpServlet { |
|
|
|
|
|
|
|
|
|
|
@ -25,20 +27,34 @@ public class RestServlet extends HttpServlet { |
|
|
|
@Value("${sop.restful.path:/rest}") |
|
|
|
@Value("${sop.restful.path:/rest}") |
|
|
|
private String restPath; |
|
|
|
private String restPath; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
|
|
private ValidateService validateService; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 验证回调,可自定义实现接口 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private ValidateService.ValidateCallback callback = (currentContext -> { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
currentContext.getRequest().getRequestDispatcher(path).forward(currentContext.getRequest(), currentContext.getResponse()); |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
log.error("请求转发异常", e); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
|
|
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) { |
|
|
|
doPost(request, response); |
|
|
|
doPost(request, response); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
|
|
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) { |
|
|
|
String url = request.getRequestURL().toString(); |
|
|
|
String url = request.getRequestURL().toString(); |
|
|
|
int index = url.indexOf(restPath); |
|
|
|
int index = url.indexOf(restPath); |
|
|
|
// 取/rest的后面部分
|
|
|
|
// 取/rest的后面部分
|
|
|
|
String path = url.substring(index + restPath.length()); |
|
|
|
String path = url.substring(index + restPath.length()); |
|
|
|
request.setAttribute(SopConstants.REDIRECT_METHOD_KEY, path); |
|
|
|
request.setAttribute(SopConstants.REDIRECT_METHOD_KEY, path); |
|
|
|
request.setAttribute(SopConstants.REDIRECT_VERSION_KEY, EMPTY_VERSION); |
|
|
|
request.setAttribute(SopConstants.REDIRECT_VERSION_KEY, EMPTY_VERSION); |
|
|
|
request.getRequestDispatcher(this.path).forward(request, response); |
|
|
|
validateService.validate(request, response, callback); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |