Search in sources :

Example 1 with ResponseData

use of com.topcom.zuulapi.vo.ResponseData in project topcom-cloud by 545314690.

the class AccessFilter method run.

@Override
public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    HttpServletRequest request = ctx.getRequest();
    log.info(String.format("%s request to %s", request.getMethod(), request.getRequestURL().toString()));
    String accessToken = this.getToken(request);
    if (accessToken == null) {
        accessToken = request.getParameter("accessToken");
    }
    ctx.getResponse().setContentType("text/html;charset=UTF-8");
    if (accessToken == null) {
        ctx.setSendZuulResponse(false);
        ctx.setResponseStatusCode(401);
        // 返回错误内容
        ctx.setResponseBody(new ResponseData(401, "accessToken is null").toString());
        return null;
    } else {
        try {
            ResponseData responseData = authClient.check(request.getRequestURI(), request.getMethod(), accessToken);
            log.info(responseData.toString());
            if (!responseData.getStatus()) {
                ctx.setSendZuulResponse(false);
                ctx.setResponseStatusCode(responseData.getCode());
                // 返回错误内容
                ctx.setResponseBody(responseData.toString());
                return responseData;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return null;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ResponseData(com.topcom.zuulapi.vo.ResponseData) RequestContext(com.netflix.zuul.context.RequestContext)

Aggregations

RequestContext (com.netflix.zuul.context.RequestContext)1 ResponseData (com.topcom.zuulapi.vo.ResponseData)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1