|
@ -3,6 +3,7 @@ package com.yihu.jw.gateway.filter;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.netflix.zuul.ZuulFilter;
|
|
|
import com.netflix.zuul.context.RequestContext;
|
|
|
import com.yihu.jw.restmodel.ResultStatus;
|
|
|
import com.yihu.jw.restmodel.web.Envelop;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
@ -11,7 +12,6 @@ import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Primary;
|
|
|
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.security.oauth2.common.OAuth2AccessToken;
|
|
|
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
|
|
import org.springframework.security.oauth2.provider.token.TokenStore;
|
|
@ -21,7 +21,6 @@ import org.springframework.util.StringUtils;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.io.IOException;
|
|
|
import java.util.Set;
|
|
|
|
|
|
/**
|
|
|
* Created by progr1mmer on 2017/12/27
|
|
@ -85,14 +84,14 @@ public class BasicZuulFilter extends ZuulFilter {
|
|
|
private Object authenticate(RequestContext ctx, HttpServletRequest request, String path) {
|
|
|
String accessToken = this.extractToken(request);
|
|
|
if (null == accessToken) {
|
|
|
return this.forbidden(ctx, HttpStatus.FORBIDDEN.value(), "token can not be null");
|
|
|
return this.forbidden(ctx, ResultStatus.NULL_TOKEN, "token can not be null");
|
|
|
}
|
|
|
OAuth2AccessToken oAuth2AccessToken = tokenStore.readAccessToken(accessToken);
|
|
|
if (null == oAuth2AccessToken) {
|
|
|
return this.forbidden(ctx, HttpStatus.FORBIDDEN.value(), "invalid token");
|
|
|
return this.forbidden(ctx, ResultStatus.ERROR_TOKEN, "invalid token");
|
|
|
}
|
|
|
if (oAuth2AccessToken.isExpired()) {
|
|
|
return this.forbidden(ctx, HttpStatus.PAYMENT_REQUIRED.value(), "expired token"); //返回402 登陆过期
|
|
|
return this.forbidden(ctx, ResultStatus.ERROR_TOKEN, "expired token");
|
|
|
}
|
|
|
//将token的认证信息附加到请求中,转发给下游微服务
|
|
|
OAuth2Authentication auth = tokenStore.readAuthentication(accessToken);
|
|
@ -101,7 +100,7 @@ public class BasicZuulFilter extends ZuulFilter {
|
|
|
// Set<String> resourceIds = auth.getOAuth2Request().getResourceIds();
|
|
|
String urls = redisTemplate.opsForValue().get("wlyy2:auth:token:"+accessToken);
|
|
|
if(StringUtils.isEmpty(urls)){
|
|
|
return this.forbidden(ctx, HttpStatus.FORBIDDEN.value(), "invalid token does not contain request resource " + path);
|
|
|
return this.forbidden(ctx, ResultStatus.NO_PERMI, "invalid token does not contain request resource " + path);
|
|
|
}
|
|
|
//获取所有token资源
|
|
|
String resourceIds[] = urls.split(",");
|
|
@ -119,7 +118,7 @@ public class BasicZuulFilter extends ZuulFilter {
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
return this.forbidden(ctx, HttpStatus.FORBIDDEN.value(), "invalid token does not contain request resource " + path);
|
|
|
return this.forbidden(ctx, ResultStatus.NO_PERMI, "invalid token does not contain request resource " + path);
|
|
|
}
|
|
|
|
|
|
private String extractToken(HttpServletRequest request) {
|
|
@ -139,7 +138,7 @@ public class BasicZuulFilter extends ZuulFilter {
|
|
|
//requestContext.setResponseStatusCode(status);
|
|
|
requestContext.getResponse().getWriter().write(objectMapper.writeValueAsString(envelop));
|
|
|
} catch (IOException e) {
|
|
|
requestContext.setResponseStatusCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
|
|
|
requestContext.setResponseStatusCode(ResultStatus.FAILED_RESP);
|
|
|
logger.error(e.getMessage());
|
|
|
}
|
|
|
return false;
|