|
|
@@ -1,103 +0,0 @@
|
|
|
-package com.usoftchina.saas.gateway.config;
|
|
|
-
|
|
|
-import com.netflix.zuul.ZuulFilter;
|
|
|
-import com.netflix.zuul.context.RequestContext;
|
|
|
-import com.netflix.zuul.exception.ZuulException;
|
|
|
-import com.usoftchina.saas.auth.api.AuthApi;
|
|
|
-import com.usoftchina.saas.auth.common.jwt.JwtHelper;
|
|
|
-import com.usoftchina.saas.auth.common.jwt.JwtInfo;
|
|
|
-import com.usoftchina.saas.base.Result;
|
|
|
-import com.usoftchina.saas.exception.BizException;
|
|
|
-import com.usoftchina.saas.exception.ExceptionCode;
|
|
|
-import com.usoftchina.saas.utils.JsonUtils;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.http.MediaType;
|
|
|
-import org.springframework.util.CollectionUtils;
|
|
|
-import org.springframework.util.StringUtils;
|
|
|
-
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
-
|
|
|
-/**
|
|
|
- * @author yingp
|
|
|
- * @date 2018/9/30
|
|
|
- */
|
|
|
-public class AccessFilter extends ZuulFilter {
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private AuthApi authApi;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private AuthConfig authConfig;
|
|
|
-
|
|
|
- @Override
|
|
|
- public String filterType() {
|
|
|
- return "pre";
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public int filterOrder() {
|
|
|
- return 1;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean shouldFilter() {
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 具体过滤逻辑
|
|
|
- *
|
|
|
- * @return
|
|
|
- * @throws ZuulException
|
|
|
- */
|
|
|
- @Override
|
|
|
- public Object run() throws ZuulException {
|
|
|
- RequestContext ctx = RequestContext.getCurrentContext();
|
|
|
- HttpServletRequest request = ctx.getRequest();
|
|
|
- if (isIgnore(request.getRequestURI())) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- String token = request.getHeader(authConfig.getAuthHeader());
|
|
|
- if (StringUtils.isEmpty(token)) {
|
|
|
- setFailedRequest(ctx, 401, Result.error(ExceptionCode.JWT_ILLEGAL_ARGUMENT));
|
|
|
- return null;
|
|
|
- }
|
|
|
- try {
|
|
|
- JwtInfo infoFromToken = JwtHelper.getInfoFromToken(token, authConfig.getPublicKey());
|
|
|
- // TODO resource + role
|
|
|
- } catch (BizException e) {
|
|
|
- setFailedRequest(ctx, 401, Result.error(e));
|
|
|
- return null;
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- private boolean isIgnore(String requestUri) {
|
|
|
- if (!CollectionUtils.isEmpty(authConfig.getIgnores())) {
|
|
|
- for (String ignore : authConfig.getIgnores()) {
|
|
|
- if (requestUri.startsWith(ignore)) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 异常请求
|
|
|
- *
|
|
|
- * @param ctx
|
|
|
- * @param code
|
|
|
- * @param result
|
|
|
- */
|
|
|
- public void setFailedRequest(RequestContext ctx, int code, Result result) {
|
|
|
- ctx.setSendZuulResponse(false);
|
|
|
- HttpServletResponse httpResponse = ctx.getResponse();
|
|
|
- httpResponse.setCharacterEncoding("UTF-8");
|
|
|
- httpResponse.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
|
|
|
- httpResponse.setStatus(code);
|
|
|
- ctx.setResponseBody(JsonUtils.toJsonString(result));
|
|
|
- ctx.setResponse(httpResponse);
|
|
|
- }
|
|
|
-}
|