parent
							
								
									0958cf003c
								
							
						
					
					
						commit
						01b1e7b72d
					
				@ -0,0 +1,90 @@ | 
				
			|||||||
 | 
					package com.gitee.sop.gatewaycommon.zuul; | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.gitee.sop.gatewaycommon.bean.ApiConfig; | 
				
			||||||
 | 
					import com.gitee.sop.gatewaycommon.param.ApiParam; | 
				
			||||||
 | 
					import com.gitee.sop.gatewaycommon.param.ParamBuilder; | 
				
			||||||
 | 
					import com.gitee.sop.gatewaycommon.util.RequestUtil; | 
				
			||||||
 | 
					import com.gitee.sop.gatewaycommon.util.ResponseUtil; | 
				
			||||||
 | 
					import com.gitee.sop.gatewaycommon.validate.Validator; | 
				
			||||||
 | 
					import com.netflix.zuul.context.RequestContext; | 
				
			||||||
 | 
					import lombok.extern.slf4j.Slf4j; | 
				
			||||||
 | 
					import org.springframework.beans.factory.annotation.Autowired; | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import javax.servlet.http.HttpServletRequest; | 
				
			||||||
 | 
					import javax.servlet.http.HttpServletResponse; | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/** | 
				
			||||||
 | 
					 * 负责签名校验 | 
				
			||||||
 | 
					 * @author tanghc | 
				
			||||||
 | 
					 */ | 
				
			||||||
 | 
					@Slf4j | 
				
			||||||
 | 
					public class ValidateService { | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Autowired | 
				
			||||||
 | 
					    private ParamBuilder<RequestContext> paramBuilder; | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Autowired | 
				
			||||||
 | 
					    private Validator validator; | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /** | 
				
			||||||
 | 
					     * 校验操作 | 
				
			||||||
 | 
					     * | 
				
			||||||
 | 
					     * @param request  request | 
				
			||||||
 | 
					     * @param response response | 
				
			||||||
 | 
					     * @param callback 校验后操作 | 
				
			||||||
 | 
					     */ | 
				
			||||||
 | 
					    public void validate(HttpServletRequest request, HttpServletResponse response, ValidateCallback callback) { | 
				
			||||||
 | 
					        RequestContext currentContext = RequestContext.getCurrentContext(); | 
				
			||||||
 | 
					        currentContext.setRequest(RequestUtil.wrapRequest(request)); | 
				
			||||||
 | 
					        currentContext.setResponse(response); | 
				
			||||||
 | 
					        doValidate(currentContext, callback); | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /** | 
				
			||||||
 | 
					     * 签名校验 | 
				
			||||||
 | 
					     * | 
				
			||||||
 | 
					     * @param currentContext currentContext | 
				
			||||||
 | 
					     */ | 
				
			||||||
 | 
					    private void doValidate(RequestContext currentContext, ValidateCallback callback) { | 
				
			||||||
 | 
					        // 解析参数
 | 
				
			||||||
 | 
					        ApiParam param = paramBuilder.build(currentContext); | 
				
			||||||
 | 
					        ZuulContext.setApiParam(param); | 
				
			||||||
 | 
					        Exception error = null; | 
				
			||||||
 | 
					        // 验证操作,这里有负责验证签名参数
 | 
				
			||||||
 | 
					        try { | 
				
			||||||
 | 
					            validator.validate(param); | 
				
			||||||
 | 
					        } catch (Exception e) { | 
				
			||||||
 | 
					            error = e; | 
				
			||||||
 | 
					        } | 
				
			||||||
 | 
					        param.fitNameVersion(); | 
				
			||||||
 | 
					        if (error == null) { | 
				
			||||||
 | 
					            callback.onSuccess(currentContext); | 
				
			||||||
 | 
					        } else { | 
				
			||||||
 | 
					            callback.onError(currentContext, param, error); | 
				
			||||||
 | 
					        } | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public interface ValidateCallback { | 
				
			||||||
 | 
					        /** | 
				
			||||||
 | 
					         * 校验成功触发 | 
				
			||||||
 | 
					         * | 
				
			||||||
 | 
					         * @param currentContext 上下文 | 
				
			||||||
 | 
					         */ | 
				
			||||||
 | 
					        void onSuccess(RequestContext currentContext); | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /** | 
				
			||||||
 | 
					         * 校验失败触发 | 
				
			||||||
 | 
					         * | 
				
			||||||
 | 
					         * @param currentContext 上下文 | 
				
			||||||
 | 
					         * @param param          参数 | 
				
			||||||
 | 
					         * @param throwable      异常 | 
				
			||||||
 | 
					         */ | 
				
			||||||
 | 
					        default void onError(RequestContext currentContext, ApiParam param, Throwable throwable) { | 
				
			||||||
 | 
					            log.error("验证失败,ip:{}, params:{}, errorMsg:{}", param.fetchIp(), param.toJSONString(), throwable.getMessage()); | 
				
			||||||
 | 
					            String errorResult = ApiConfig.getInstance().getZuulResultExecutor().buildErrorResult(currentContext, throwable); | 
				
			||||||
 | 
					            ResponseUtil.writeJson(currentContext.getResponse(), errorResult); | 
				
			||||||
 | 
					        } | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					} | 
				
			||||||
@ -1,43 +1,66 @@ | 
				
			|||||||
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 org.springframework.stereotype.Controller; | 
					import org.springframework.stereotype.Controller; | 
				
			||||||
import org.springframework.web.bind.annotation.PathVariable; | 
					import org.springframework.web.bind.annotation.PathVariable; | 
				
			||||||
import org.springframework.web.bind.annotation.RequestMapping; | 
					import org.springframework.web.bind.annotation.RequestMapping; | 
				
			||||||
 | 
					
 | 
				
			||||||
import javax.servlet.ServletException; | 
					 | 
				
			||||||
import javax.servlet.http.HttpServletRequest; | 
					import javax.servlet.http.HttpServletRequest; | 
				
			||||||
import javax.servlet.http.HttpServletResponse; | 
					import javax.servlet.http.HttpServletResponse; | 
				
			||||||
import java.io.IOException; | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
/** | 
					/** | 
				
			||||||
 | 
					 * 网关入口 | 
				
			||||||
 | 
					 * | 
				
			||||||
 * @author tanghc | 
					 * @author tanghc | 
				
			||||||
 */ | 
					 */ | 
				
			||||||
 | 
					@Slf4j | 
				
			||||||
@Controller | 
					@Controller | 
				
			||||||
public class RedirectController { | 
					public class RedirectController { | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Autowired | 
				
			||||||
 | 
					    private ValidateService validateService; | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Value("${zuul.servlet-path:/zuul}") | 
					    @Value("${zuul.servlet-path:/zuul}") | 
				
			||||||
    private String path; | 
					    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}/") | 
					    @RequestMapping("/{method}/{version}/") | 
				
			||||||
    public void redirect( | 
					    public void redirect( | 
				
			||||||
            @PathVariable("method") String method | 
					            @PathVariable("method") String method | 
				
			||||||
            , @PathVariable("version") String version | 
					            , @PathVariable("version") String version | 
				
			||||||
            , HttpServletRequest request | 
					            , HttpServletRequest request | 
				
			||||||
            , HttpServletResponse response | 
					            , HttpServletResponse response | 
				
			||||||
    ) throws ServletException, IOException { | 
					    ) { | 
				
			||||||
        request.setAttribute(SopConstants.REDIRECT_METHOD_KEY, method); | 
					        request.setAttribute(SopConstants.REDIRECT_METHOD_KEY, method); | 
				
			||||||
        request.setAttribute(SopConstants.REDIRECT_VERSION_KEY, version); | 
					        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); | 
					 | 
				
			||||||
    } | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
} | 
					} | 
				
			||||||
 | 
				
			|||||||
					Loading…
					
					
				
		Reference in new issue