This commit is contained in:
tanghc
2020-01-03 17:41:47 +08:00
parent 0958cf003c
commit 01b1e7b72d
21 changed files with 206 additions and 51 deletions

View File

@@ -1,43 +1,66 @@
package com.gitee.sop.gateway.controller;
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.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 网关入口
*
* @author tanghc
*/
@Slf4j
@Controller
public class RedirectController {
@Autowired
private ValidateService validateService;
@Value("${zuul.servlet-path:/zuul}")
private String path;
/**
* 验证回调,可自定义实现接口
*/
private ValidateService.ValidateCallback callback = (currentContext -> {
try {
currentContext.getRequest().getRequestDispatcher(path).forward(currentContext.getRequest(), currentContext.getResponse());
} catch (Exception e) {
log.error("请求转发异常", e);
}
});
/**
* 网关入口
*
* @param request request
* @param response response
*/
@RequestMapping("/")
public void index(HttpServletRequest request, HttpServletResponse response) {
validateService.validate(request, response, callback);
}
@RequestMapping("/{method}/{version}/")
public void redirect(
@PathVariable("method") String method
, @PathVariable("version") String version
, HttpServletRequest request
, HttpServletResponse response
) throws ServletException, IOException {
) {
request.setAttribute(SopConstants.REDIRECT_METHOD_KEY, method);
request.setAttribute(SopConstants.REDIRECT_VERSION_KEY, version);
request.getRequestDispatcher(path).forward(request, response);
validateService.validate(request, response, callback);
}
@RequestMapping("/")
public void index(
HttpServletRequest request
, HttpServletResponse response
) throws ServletException, IOException {
request.getRequestDispatcher(path).forward(request, response);
}
}

View File

@@ -1,8 +1,6 @@
package com.gitee.sop.gateway.controller;
import com.gitee.sop.gatewaycommon.bean.SopConstants;
import com.gitee.sop.gatewaycommon.param.ParamNames;
import com.gitee.sop.gatewaycommon.util.RouteUtil;
import org.springframework.beans.factory.annotation.Value;
import javax.servlet.ServletException;