添加客户API调用量使用90%发送kafka消息
This commit is contained in:
+441
-395
@@ -14,6 +14,7 @@ import com.fangjia.open.model.RestfulModelAndView;
|
|||||||
import com.fangjia.service.open.ApiUserAuthorityService;
|
import com.fangjia.service.open.ApiUserAuthorityService;
|
||||||
import com.fangjia.service.open.ApiVisitCountService;
|
import com.fangjia.service.open.ApiVisitCountService;
|
||||||
import com.fangjia.service.sql.ApiUserService;
|
import com.fangjia.service.sql.ApiUserService;
|
||||||
|
import com.fangjia.service.util.kafka.KafkaProducerUtils;
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@@ -30,422 +31,467 @@ import java.util.*;
|
|||||||
* API拦截器
|
* API拦截器
|
||||||
*
|
*
|
||||||
* @author
|
* @author
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class AuthenticationInterceptor implements Interceptor {
|
public class AuthenticationInterceptor implements Interceptor {
|
||||||
|
|
||||||
|
|
||||||
private ApiUserService apiUserService;
|
private ApiUserService apiUserService;
|
||||||
private ApiUserAuthorityService apiUserAuthorityService;
|
|
||||||
private ApiVisitCountService apiVisitCountService;
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(AuthenticationInterceptor.class);
|
|
||||||
private static final Date OLD_USER_SPLITTED_DATE = DateUtil.parseFormatDate("2016-06-06");
|
|
||||||
private static final List<String> HAS_SUB_SERVICE_URIS = new ArrayList<String>();
|
|
||||||
private static final String SUB_SERVICE_SEPARATOR = "#";
|
|
||||||
private static final String SERVICE_CODE_PARAM_NAME = "serviceCode";
|
|
||||||
static {
|
|
||||||
HAS_SUB_SERVICE_URIS.add("/property/transaction");
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* token过期时间 单位:s
|
|
||||||
*/
|
|
||||||
private static final long TOKEN_EXPIRED_TIME = 60 * 60 * 2;
|
|
||||||
|
|
||||||
|
private KafkaProducerUtils kafkaProducerUtils;
|
||||||
|
private ApiUserAuthorityService apiUserAuthorityService;
|
||||||
|
private ApiVisitCountService apiVisitCountService;
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(AuthenticationInterceptor.class);
|
||||||
|
private static final Date OLD_USER_SPLITTED_DATE = DateUtil.parseFormatDate("2016-06-06");
|
||||||
|
private static final List<String> HAS_SUB_SERVICE_URIS = new ArrayList<String>();
|
||||||
|
private static final String SUB_SERVICE_SEPARATOR = "#";
|
||||||
|
private static final String SERVICE_CODE_PARAM_NAME = "serviceCode";
|
||||||
|
private static final String SMS_DATA_TOPIC = "sms-data-topic";
|
||||||
|
private static final String SMS_DATA_TOPIC_PROFIX = "sendSms90-";
|
||||||
|
|
||||||
public Object intercept(ActionInvocation invocation) throws ActionInvocationException {
|
static {
|
||||||
|
HAS_SUB_SERVICE_URIS.add("/property/transaction");
|
||||||
|
}
|
||||||
|
|
||||||
Authority authority = getAuthority(invocation);
|
/**
|
||||||
ActionContext context = invocation.getActionContext();
|
* token过期时间 单位:s
|
||||||
HttpServletRequest request = context.getRequest();
|
|
||||||
RestfulModelAndView ret = new RestfulModelAndView();
|
|
||||||
ret.setViewType("rest");
|
|
||||||
|
|
||||||
try {
|
|
||||||
HttpCode httpCode = null;
|
|
||||||
if(!OpenConfig.getConfig().isOpenAllAuthority()) {
|
|
||||||
String token = request.getParameter("token");
|
|
||||||
if(isNeedToken(authority)){
|
|
||||||
httpCode = authorized(token, request);
|
|
||||||
}else if(StringUtils.isNotBlank(token)){
|
|
||||||
ApiUser apiUser = parseApiUserByToken(FangjiaApiUtil.parse2Token(token));
|
|
||||||
if(apiUser != null){
|
|
||||||
User user = apiUserService.findUserById(apiUser.getId());
|
|
||||||
AuthenticationBean.get(request, apiUser, user, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//功能方法没有设置权限或者有该方法权限
|
|
||||||
if (authority == null || authority.role() == Role.NONE || null == httpCode) {
|
|
||||||
Object obj;
|
|
||||||
try{
|
|
||||||
obj = invocation.invoke();
|
|
||||||
}catch (Exception e){
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
return obj;
|
|
||||||
}else {
|
|
||||||
if(httpCode.getErrorCode() == 4003){
|
|
||||||
BasicModel basicModel = BasicModel.buildBadRequestModel(httpCode.getErrorMessage().toString());
|
|
||||||
ret.setModel(basicModel);
|
|
||||||
return ret;
|
|
||||||
}else{
|
|
||||||
String msg = httpCode.getMessage() + "(" + httpCode.getErrorMessage() + ")";
|
|
||||||
BasicModel basicModel = BasicModel.buildUnAuthorizedModel().setMsg(msg);
|
|
||||||
ret.setModel(basicModel);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
LOGGER.error("Authentication error : ", e);
|
|
||||||
ret.setModel(BasicModel.buildServerErrorModel());
|
|
||||||
ret.setViewParam("code", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isNeedToken(Authority authority){
|
|
||||||
return authority != null && authority.role() != null && authority.role() != Role.NONE;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isReachUpperLimit(ApiUserAuthority auth, String uri, String serviceCode) {
|
|
||||||
|
|
||||||
if (auth == null || auth.getLimitCountMap() == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
Date visitDate = DateUtil.truncateDay(new Date());
|
|
||||||
Integer upperLimit = auth.getLimitCountMap().get(uri);
|
|
||||||
Long apiVisitCount;
|
|
||||||
if (upperLimit != null && upperLimit > 0) {
|
|
||||||
apiVisitCount = apiVisitCountService.findApiVisitCount(auth.getUserId(), uri, null, visitDate);
|
|
||||||
if(apiVisitCount >= upperLimit){
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(hasSubService(uri) && StringUtils.isNotBlank(serviceCode)){
|
|
||||||
upperLimit = auth.getLimitCountMap().get(uri + SUB_SERVICE_SEPARATOR + serviceCode);
|
|
||||||
if (upperLimit != null && upperLimit > 0) {
|
|
||||||
apiVisitCount = apiVisitCountService.findApiVisitCount(auth.getUserId(), uri, serviceCode, visitDate);
|
|
||||||
if(apiVisitCount >= upperLimit){
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isReachDeadline(ApiUserAuthority auth, String uri, String serviceCode){
|
|
||||||
|
|
||||||
if(auth.getDeadlineMap() == null){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
Date deadline = auth.getDeadlineMap().get(uri);
|
|
||||||
if(null != deadline && deadline.before(new Date())){
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if(hasSubService(uri) && StringUtils.isNotBlank(serviceCode)){
|
|
||||||
deadline = auth.getDeadlineMap().get(uri + SUB_SERVICE_SEPARATOR + serviceCode);
|
|
||||||
if(null != deadline && deadline.before(new Date())){
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isReachSumLimit(ApiUserAuthority auth, String uri, String serviceCode) {
|
|
||||||
|
|
||||||
if (auth == null || auth.getTotalCountMap() == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(auth.getTotalCountMap() != null && auth.getTotalCountMap().get(uri) != null){
|
|
||||||
Integer totalLimit = auth.getTotalCountMap().get(uri);
|
|
||||||
Long totalApiVisitCount;
|
|
||||||
if(totalLimit != null && totalLimit > 0){
|
|
||||||
totalApiVisitCount = apiVisitCountService.findApiVisitTotalCount(auth.getUserId(), uri, serviceCode);
|
|
||||||
if(totalApiVisitCount >= totalLimit){
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(hasSubService(uri) && StringUtils.isNotBlank(serviceCode)){
|
|
||||||
Integer totalLimit = auth.getTotalCountMap().get(uri + SUB_SERVICE_SEPARATOR + serviceCode);
|
|
||||||
if(totalLimit != null && totalLimit > 0){
|
|
||||||
Long totalApiVisitCount = apiVisitCountService.findApiVisitTotalCount(auth.getUserId(), uri, serviceCode);
|
|
||||||
if(totalApiVisitCount >= totalLimit){
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Authority getAuthority(ActionInvocation invocation) {
|
|
||||||
Authority authority;
|
|
||||||
// 获取方法上的权限注解
|
|
||||||
authority = invocation.getAction().getActionMethod().getAnnotation(Authority.class);
|
|
||||||
if (null != authority) {
|
|
||||||
return authority;
|
|
||||||
}
|
|
||||||
// 获取类上的权限注解
|
|
||||||
authority = invocation.getControllerInstance().getClass().getAnnotation(Authority.class);
|
|
||||||
return authority;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 检验是否有访问该Action的权限
|
|
||||||
*
|
|
||||||
* @param tokenStr
|
|
||||||
* @param request
|
|
||||||
* @return
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
private HttpCode authorized(String tokenStr, HttpServletRequest request) throws Exception {
|
|
||||||
|
|
||||||
// 检查是否为api用户
|
|
||||||
HttpCode ret = new HttpCode(OpenHttpCodeEnum.AUTH_FAILD);
|
|
||||||
Token token = FangjiaApiUtil.parse2Token(tokenStr);
|
|
||||||
ApiUser apiUser = parseApiUserByToken(token);
|
|
||||||
if (null == apiUser) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
if(apiUser.getDeadTime() != null && DateUtil.truncateDay(new Date()).after(apiUser.getDeadTime())){
|
|
||||||
return new HttpCode(OpenHttpCodeEnum.USER_EXPIRED);
|
|
||||||
}
|
|
||||||
if(!isOldUser(apiUser) && isExpired(token)){
|
|
||||||
return new HttpCode(OpenHttpCodeEnum.TOKEN_EXPIRED);
|
|
||||||
}
|
|
||||||
if (1 == apiUser.getStatus()) {
|
|
||||||
return new HttpCode(OpenHttpCodeEnum.AUTH_FAILD, "你的账户已失效");
|
|
||||||
}
|
|
||||||
if(StringUtils.isNotBlank(apiUser.getIp())){
|
|
||||||
String ip = AnalyticsUtil.getIpAddress(request);
|
|
||||||
if(!isAuthIp(ip,apiUser)){
|
|
||||||
ret = new HttpCode(OpenHttpCodeEnum.AUTH_FAILD,"ip[" + ip + "]无权限访问API");
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
String uri = request.getRequestURI();
|
|
||||||
ApiUserAuthority auth = this.apiUserAuthorityService.findByUserId(apiUser.getId());
|
|
||||||
if(auth != null){
|
|
||||||
User user = apiUserService.findUserById(apiUser.getId());
|
|
||||||
AuthenticationBean.get(request, apiUser, user, auth);
|
|
||||||
}
|
|
||||||
uri = OpenConfig.getConfig().isDevelopmentEnvironment() ? uri.replace("/fangjia-open", "") : uri;
|
|
||||||
// 检查是否有访问该Action的权限
|
|
||||||
boolean authAction = null != auth && auth.getOpenResourseUris().contains(uri);
|
|
||||||
String cityName = request.getParameter("city");
|
|
||||||
if(StringUtils.isBlank(cityName)){
|
|
||||||
cityName = request.getParameter("cityName");
|
|
||||||
}
|
|
||||||
if (StringUtils.isNotBlank(cityName)) {
|
|
||||||
boolean authCity = StringUtils.isNotBlank(apiUser.getCity()) && !StringUtils.equalsIgnoreCase(apiUser.getCity(), "all");
|
|
||||||
if(authCity){
|
|
||||||
cityName = request.getParameter("city");
|
|
||||||
cityName = RegionData.formatCity(cityName);
|
|
||||||
if(!RegionData.isCity(cityName)){
|
|
||||||
ret = new HttpCode(OpenHttpCodeEnum.ARGUMENT_INVALID);
|
|
||||||
ret.setErrorMessage("城市非法");
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
boolean authCity = isAuthCity(cityName, apiUser);
|
|
||||||
if (!authCity) {
|
|
||||||
ret = new HttpCode(OpenHttpCodeEnum.AUTH_FAILD, "您未开通城市" + (StringUtils.isNotBlank(cityName) ? cityName : StringUtils.EMPTY) + "的权限");
|
|
||||||
}
|
|
||||||
if (authAction && authCity) {
|
|
||||||
if(hasSubService(uri)){
|
|
||||||
List<String> serviceCodes = getServiceCode(request);
|
|
||||||
for(String serviceCode : serviceCodes){
|
|
||||||
if(!auth.getOpenResourseUris().contains(uri + SUB_SERVICE_SEPARATOR + serviceCode)){
|
|
||||||
ret = new HttpCode(OpenHttpCodeEnum.AUTH_FAILD ,"无权限访问服务:" + serviceCode);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
if(isReachDeadline(auth, uri, serviceCode)){
|
|
||||||
ret = new HttpCode(OpenHttpCodeEnum.SERVICE_EXPIRED, serviceCode + "服务已过期");
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
if(isReachUpperLimit(auth, uri, serviceCode)){
|
|
||||||
ret = new HttpCode(OpenHttpCodeEnum.UPPER_LIMIT ,serviceCode + "今日已达到配额上限");
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
// 检测是否达到总数访问上限
|
|
||||||
if (isReachSumLimit(auth, uri, serviceCode)) {
|
|
||||||
ret = new HttpCode(OpenHttpCodeEnum.UPPER_LIMIT, serviceCode + "已达到配额上限");
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}else{
|
|
||||||
if(isReachDeadline(auth, uri, null)){
|
|
||||||
ret = new HttpCode(OpenHttpCodeEnum.UPPER_LIMIT, "服务已过期");
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
// 检测是否达到今天访问上限
|
|
||||||
if (isReachUpperLimit(auth, uri, null)) {
|
|
||||||
ret = new HttpCode(OpenHttpCodeEnum.UPPER_LIMIT, "今日已达到配额上限");
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
// 检测是否达到总数访问上限
|
|
||||||
if (isReachSumLimit(auth, uri, null)) {
|
|
||||||
ret = new HttpCode(OpenHttpCodeEnum.UPPER_LIMIT, "已达到配额上限");
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param request
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
private List<String> getServiceCode(HttpServletRequest request){
|
private static final long TOKEN_EXPIRED_TIME = 60 * 60 * 2;
|
||||||
|
|
||||||
String serviceCode = request.getParameter(SERVICE_CODE_PARAM_NAME);
|
|
||||||
if(StringUtils.isBlank(serviceCode)){
|
|
||||||
return Collections.emptyList();
|
|
||||||
}
|
|
||||||
return Arrays.asList(serviceCode.split(",|,"));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
public Object intercept(ActionInvocation invocation) throws ActionInvocationException {
|
||||||
* 判断该API是否拥有子权限
|
|
||||||
* @param uri
|
Authority authority = getAuthority(invocation);
|
||||||
* @return
|
ActionContext context = invocation.getActionContext();
|
||||||
|
HttpServletRequest request = context.getRequest();
|
||||||
|
RestfulModelAndView ret = new RestfulModelAndView();
|
||||||
|
ret.setViewType("rest");
|
||||||
|
|
||||||
|
try {
|
||||||
|
HttpCode httpCode = null;
|
||||||
|
if (!OpenConfig.getConfig().isOpenAllAuthority()) {
|
||||||
|
String token = request.getParameter("token");
|
||||||
|
if (isNeedToken(authority)) {
|
||||||
|
httpCode = authorized(token, request);
|
||||||
|
} else if (StringUtils.isNotBlank(token)) {
|
||||||
|
ApiUser apiUser = parseApiUserByToken(FangjiaApiUtil.parse2Token(token));
|
||||||
|
if (apiUser != null) {
|
||||||
|
User user = apiUserService.findUserById(apiUser.getId());
|
||||||
|
AuthenticationBean.get(request, apiUser, user, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//功能方法没有设置权限或者有该方法权限
|
||||||
|
if (authority == null || authority.role() == Role.NONE || null == httpCode) {
|
||||||
|
Object obj;
|
||||||
|
try {
|
||||||
|
obj = invocation.invoke();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
|
} else {
|
||||||
|
if (httpCode.getErrorCode() == 4003) {
|
||||||
|
BasicModel basicModel = BasicModel.buildBadRequestModel(httpCode.getErrorMessage().toString());
|
||||||
|
ret.setModel(basicModel);
|
||||||
|
return ret;
|
||||||
|
} else {
|
||||||
|
String msg = httpCode.getMessage() + "(" + httpCode.getErrorMessage() + ")";
|
||||||
|
BasicModel basicModel = BasicModel.buildUnAuthorizedModel().setMsg(msg);
|
||||||
|
ret.setModel(basicModel);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOGGER.error("Authentication error : ", e);
|
||||||
|
ret.setModel(BasicModel.buildServerErrorModel());
|
||||||
|
ret.setViewParam("code", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isNeedToken(Authority authority) {
|
||||||
|
return authority != null && authority.role() != null && authority.role() != Role.NONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isReachUpperLimit(ApiUserAuthority auth, String uri, String serviceCode) {
|
||||||
|
|
||||||
|
if (auth == null || auth.getLimitCountMap() == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Date visitDate = DateUtil.truncateDay(new Date());
|
||||||
|
Integer upperLimit = auth.getLimitCountMap().get(uri);
|
||||||
|
Long apiVisitCount;
|
||||||
|
if (upperLimit != null && upperLimit > 0) {
|
||||||
|
apiVisitCount = apiVisitCountService.findApiVisitCount(auth.getUserId(), uri, null, visitDate);
|
||||||
|
if (apiVisitCount >= upperLimit) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (hasSubService(uri) && StringUtils.isNotBlank(serviceCode)) {
|
||||||
|
upperLimit = auth.getLimitCountMap().get(uri + SUB_SERVICE_SEPARATOR + serviceCode);
|
||||||
|
if (upperLimit != null && upperLimit > 0) {
|
||||||
|
apiVisitCount = apiVisitCountService.findApiVisitCount(auth.getUserId(), uri, serviceCode, visitDate);
|
||||||
|
if (apiVisitCount >= upperLimit) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isReachDeadline(ApiUserAuthority auth, String uri, String serviceCode) {
|
||||||
|
|
||||||
|
if (auth.getDeadlineMap() == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Date deadline = auth.getDeadlineMap().get(uri);
|
||||||
|
if (null != deadline && deadline.before(new Date())) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (hasSubService(uri) && StringUtils.isNotBlank(serviceCode)) {
|
||||||
|
deadline = auth.getDeadlineMap().get(uri + SUB_SERVICE_SEPARATOR + serviceCode);
|
||||||
|
if (null != deadline && deadline.before(new Date())) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isReachSumLimit(ApiUserAuthority auth, String uri, String serviceCode) {
|
||||||
|
|
||||||
|
if (auth == null || auth.getTotalCountMap() == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (auth.getTotalCountMap() != null && auth.getTotalCountMap().get(uri) != null) {
|
||||||
|
Integer totalLimit = auth.getTotalCountMap().get(uri);
|
||||||
|
Long totalApiVisitCount;
|
||||||
|
if (totalLimit != null && totalLimit > 0) {
|
||||||
|
totalApiVisitCount = apiVisitCountService.findApiVisitTotalCount(auth.getUserId(), uri, serviceCode);
|
||||||
|
if (totalApiVisitCount >= totalLimit) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (hasSubService(uri) && StringUtils.isNotBlank(serviceCode)) {
|
||||||
|
Integer totalLimit = auth.getTotalCountMap().get(uri + SUB_SERVICE_SEPARATOR + serviceCode);
|
||||||
|
if (totalLimit != null && totalLimit > 0) {
|
||||||
|
Long totalApiVisitCount = apiVisitCountService.findApiVisitTotalCount(auth.getUserId(), uri, serviceCode);
|
||||||
|
if (totalApiVisitCount >= totalLimit) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isReach90SumLimit(ApiUserAuthority auth, String uri, String serviceCode) {
|
||||||
|
|
||||||
|
if (auth == null || auth.getTotalCountMap() == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (auth.getTotalCountMap() != null && auth.getTotalCountMap().get(uri) != null) {
|
||||||
|
Integer totalLimit = auth.getTotalCountMap().get(uri);
|
||||||
|
Long totalApiVisitCount;
|
||||||
|
if (totalLimit != null && totalLimit > 0) {
|
||||||
|
totalApiVisitCount = apiVisitCountService.findApiVisitTotalCount(auth.getUserId(), uri, serviceCode);
|
||||||
|
if (totalApiVisitCount.intValue() == (int) (totalLimit * 0.9)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (hasSubService(uri) && StringUtils.isNotBlank(serviceCode)) {
|
||||||
|
Integer totalLimit = auth.getTotalCountMap().get(uri + SUB_SERVICE_SEPARATOR + serviceCode);
|
||||||
|
if (totalLimit != null && totalLimit > 0) {
|
||||||
|
Long totalApiVisitCount = apiVisitCountService.findApiVisitTotalCount(auth.getUserId(), uri, serviceCode);
|
||||||
|
if (totalApiVisitCount.intValue() == (int) (totalLimit * 0.9)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Authority getAuthority(ActionInvocation invocation) {
|
||||||
|
Authority authority;
|
||||||
|
// 获取方法上的权限注解
|
||||||
|
authority = invocation.getAction().getActionMethod().getAnnotation(Authority.class);
|
||||||
|
if (null != authority) {
|
||||||
|
return authority;
|
||||||
|
}
|
||||||
|
// 获取类上的权限注解
|
||||||
|
authority = invocation.getControllerInstance().getClass().getAnnotation(Authority.class);
|
||||||
|
return authority;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检验是否有访问该Action的权限
|
||||||
|
*
|
||||||
|
* @param tokenStr
|
||||||
|
* @param request
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private boolean hasSubService(String uri){
|
private HttpCode authorized(String tokenStr, HttpServletRequest request) throws Exception {
|
||||||
return HAS_SUB_SERVICE_URIS.contains(uri);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
// 检查是否为api用户
|
||||||
* 2016年06月06日以前的用户是老用户,token不过期
|
HttpCode ret = new HttpCode(OpenHttpCodeEnum.AUTH_FAILD);
|
||||||
* @param apiUser
|
Token token = FangjiaApiUtil.parse2Token(tokenStr);
|
||||||
* @return
|
ApiUser apiUser = parseApiUserByToken(token);
|
||||||
*/
|
if (null == apiUser) {
|
||||||
private boolean isOldUser(ApiUser apiUser){
|
return ret;
|
||||||
return OLD_USER_SPLITTED_DATE.after(apiUser.getCreationDate());
|
}
|
||||||
}
|
if (apiUser.getDeadTime() != null && DateUtil.truncateDay(new Date()).after(apiUser.getDeadTime())) {
|
||||||
|
return new HttpCode(OpenHttpCodeEnum.USER_EXPIRED);
|
||||||
|
}
|
||||||
|
if (!isOldUser(apiUser) && isExpired(token)) {
|
||||||
|
return new HttpCode(OpenHttpCodeEnum.TOKEN_EXPIRED);
|
||||||
|
}
|
||||||
|
if (1 == apiUser.getStatus()) {
|
||||||
|
return new HttpCode(OpenHttpCodeEnum.AUTH_FAILD, "你的账户已失效");
|
||||||
|
}
|
||||||
|
if (StringUtils.isNotBlank(apiUser.getIp())) {
|
||||||
|
String ip = AnalyticsUtil.getIpAddress(request);
|
||||||
|
if (!isAuthIp(ip, apiUser)) {
|
||||||
|
ret = new HttpCode(OpenHttpCodeEnum.AUTH_FAILD, "ip[" + ip + "]无权限访问API");
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
String uri = request.getRequestURI();
|
||||||
* 判断Token是否过期
|
ApiUserAuthority auth = this.apiUserAuthorityService.findByUserId(apiUser.getId());
|
||||||
* @param token
|
User user = apiUserService.findUserById(apiUser.getId());
|
||||||
* @return
|
if (auth != null) {
|
||||||
*/
|
AuthenticationBean.get(request, apiUser, user, auth);
|
||||||
private boolean isExpired(Token token){
|
}
|
||||||
return System.currentTimeMillis() / 1000 - token.getTimestamp() > TOKEN_EXPIRED_TIME;
|
uri = OpenConfig.getConfig().isDevelopmentEnvironment() ? uri.replace("/fangjia-open", "") : uri;
|
||||||
}
|
// 检查是否有访问该Action的权限
|
||||||
|
boolean authAction = null != auth && auth.getOpenResourseUris().contains(uri);
|
||||||
|
String cityName = request.getParameter("city");
|
||||||
|
if (StringUtils.isBlank(cityName)) {
|
||||||
|
cityName = request.getParameter("cityName");
|
||||||
|
}
|
||||||
|
if (StringUtils.isNotBlank(cityName)) {
|
||||||
|
boolean authCity = StringUtils.isNotBlank(apiUser.getCity()) && !StringUtils.equalsIgnoreCase(apiUser.getCity(), "all");
|
||||||
|
if (authCity) {
|
||||||
|
cityName = request.getParameter("city");
|
||||||
|
cityName = RegionData.formatCity(cityName);
|
||||||
|
if (!RegionData.isCity(cityName)) {
|
||||||
|
ret = new HttpCode(OpenHttpCodeEnum.ARGUMENT_INVALID);
|
||||||
|
ret.setErrorMessage("城市非法");
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
boolean authCity = isAuthCity(cityName, apiUser);
|
||||||
|
if (!authCity) {
|
||||||
|
ret = new HttpCode(OpenHttpCodeEnum.AUTH_FAILD, "您未开通城市" + (StringUtils.isNotBlank(cityName) ? cityName : StringUtils.EMPTY) + "的权限");
|
||||||
|
}
|
||||||
|
if (authAction && authCity) {
|
||||||
|
if (hasSubService(uri)) {
|
||||||
|
List<String> serviceCodes = getServiceCode(request);
|
||||||
|
for (String serviceCode : serviceCodes) {
|
||||||
|
if (!auth.getOpenResourseUris().contains(uri + SUB_SERVICE_SEPARATOR + serviceCode)) {
|
||||||
|
ret = new HttpCode(OpenHttpCodeEnum.AUTH_FAILD, "无权限访问服务:" + serviceCode);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
if (isReachDeadline(auth, uri, serviceCode)) {
|
||||||
|
ret = new HttpCode(OpenHttpCodeEnum.SERVICE_EXPIRED, serviceCode + "服务已过期");
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
if (isReachUpperLimit(auth, uri, serviceCode)) {
|
||||||
|
ret = new HttpCode(OpenHttpCodeEnum.UPPER_LIMIT, serviceCode + "今日已达到配额上限");
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
// 检测是否达到总数访问上限
|
||||||
|
if (isReachSumLimit(auth, uri, serviceCode)) {
|
||||||
|
ret = new HttpCode(OpenHttpCodeEnum.UPPER_LIMIT, serviceCode + "已达到配额上限");
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
// 检测是否达到总数的90%
|
||||||
|
if (isReach90SumLimit(auth, uri, serviceCode)) {
|
||||||
|
kafkaProducerUtils.send(SMS_DATA_TOPIC, SMS_DATA_TOPIC_PROFIX + apiUser.getPhone() + "-" + user.getUserName() + "-" + uri);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
} else {
|
||||||
*
|
if (isReachDeadline(auth, uri, null)) {
|
||||||
* @param ip 验证客户IP是否在注册的白名单中
|
ret = new HttpCode(OpenHttpCodeEnum.UPPER_LIMIT, "服务已过期");
|
||||||
* @param apiUser
|
return ret;
|
||||||
* @return
|
}
|
||||||
*/
|
// 检测是否达到今天访问上限
|
||||||
private boolean isAuthIp(String ip,ApiUser apiUser){
|
if (isReachUpperLimit(auth, uri, null)) {
|
||||||
if(StringUtils.isBlank(ip) || StringUtils.isBlank(apiUser.getIp())){
|
ret = new HttpCode(OpenHttpCodeEnum.UPPER_LIMIT, "今日已达到配额上限");
|
||||||
return false;
|
return ret;
|
||||||
}
|
}
|
||||||
String[] ips = apiUser.getIp().split("\n");
|
// 检测是否达到总数访问上限
|
||||||
for(String authIp:ips){
|
if (isReachSumLimit(auth, uri, null)) {
|
||||||
if(StringUtils.isNotBlank(authIp) && ip.startsWith(authIp.replaceAll("\\s", ""))){
|
ret = new HttpCode(OpenHttpCodeEnum.UPPER_LIMIT, "已达到配额上限");
|
||||||
return true;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
// 检测是否达到总数的90%
|
||||||
return false;
|
if (isReach90SumLimit(auth, uri, null)) {
|
||||||
}
|
kafkaProducerUtils.send(SMS_DATA_TOPIC, SMS_DATA_TOPIC_PROFIX + apiUser.getPhone() + "-" + user.getUserName() + "-" + uri);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
return ret;
|
||||||
* 是否有权限访问该城市
|
}
|
||||||
*
|
|
||||||
* @param cityName
|
|
||||||
* @param apiUser
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
private boolean isAuthCity(String cityName, ApiUser apiUser) {
|
|
||||||
// 若访问当前接口不需要城市参数,则返回true
|
|
||||||
if (StringUtils.isBlank(cityName)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if(!RegionData.isCity(cityName)){
|
|
||||||
String city = RegionData.formatCity(cityName);
|
|
||||||
if(null != city){
|
|
||||||
cityName = city;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 若当前接口需要城市,检查是否有访问当前城市的权限
|
|
||||||
boolean authCity = StringUtils.isNotBlank(apiUser.getCity()) && !StringUtils.equalsIgnoreCase(apiUser.getCity(), "all");
|
|
||||||
if (authCity) {
|
|
||||||
ServerArea city = ServerAreaData.getServerAreaByDisplayDefaultNull(cityName);
|
|
||||||
if (null != city) {
|
|
||||||
return apiUser.getCity().contains(city.getSpell());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private ApiUser parseApiUserByToken(Token token){
|
|
||||||
|
|
||||||
if(token == null){
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
ApiUser apiUser = apiUserService.findApiUserById(token.getUserId());
|
|
||||||
if(apiUser == null){
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if(!apiUser.getAppKey().equals(token.getAppKey())){
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return apiUser;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 新版token验证
|
* @param request
|
||||||
*
|
* @return
|
||||||
* @param token
|
*/
|
||||||
* @return
|
private List<String> getServiceCode(HttpServletRequest request) {
|
||||||
*/
|
|
||||||
private ApiUser checkNewAuthorized(String token) {
|
|
||||||
|
|
||||||
Map<String, String> tokenMap = FangjiaApiUtil.parseToken(token);
|
String serviceCode = request.getParameter(SERVICE_CODE_PARAM_NAME);
|
||||||
if (tokenMap == null) {
|
if (StringUtils.isBlank(serviceCode)) {
|
||||||
return null;
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
// token验证后,拿取出的字段再去数据库验证一遍
|
return Arrays.asList(serviceCode.split(",|,"));
|
||||||
if (StringUtils.isNotBlank(tokenMap.get("userid")) && !"null".equals(tokenMap.get("userid"))) {
|
}
|
||||||
ApiUser apiUser = apiUserService.findApiUserById(Long.parseLong(tokenMap.get("userid")));
|
|
||||||
if (apiUser != null) {
|
/**
|
||||||
if (tokenMap.get("appKey").equals(apiUser.getAppKey())) {
|
* 判断该API是否拥有子权限
|
||||||
return apiUser;
|
*
|
||||||
}
|
* @param uri
|
||||||
}
|
* @return
|
||||||
}
|
*/
|
||||||
return null;
|
private boolean hasSubService(String uri) {
|
||||||
}
|
return HAS_SUB_SERVICE_URIS.contains(uri);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 2016年06月06日以前的用户是老用户,token不过期
|
||||||
|
*
|
||||||
|
* @param apiUser
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private boolean isOldUser(ApiUser apiUser) {
|
||||||
|
return OLD_USER_SPLITTED_DATE.after(apiUser.getCreationDate());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断Token是否过期
|
||||||
|
*
|
||||||
|
* @param token
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private boolean isExpired(Token token) {
|
||||||
|
return System.currentTimeMillis() / 1000 - token.getTimestamp() > TOKEN_EXPIRED_TIME;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ip 验证客户IP是否在注册的白名单中
|
||||||
|
* @param apiUser
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private boolean isAuthIp(String ip, ApiUser apiUser) {
|
||||||
|
if (StringUtils.isBlank(ip) || StringUtils.isBlank(apiUser.getIp())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
String[] ips = apiUser.getIp().split("\n");
|
||||||
|
for (String authIp : ips) {
|
||||||
|
if (StringUtils.isNotBlank(authIp) && ip.startsWith(authIp.replaceAll("\\s", ""))) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否有权限访问该城市
|
||||||
|
*
|
||||||
|
* @param cityName
|
||||||
|
* @param apiUser
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private boolean isAuthCity(String cityName, ApiUser apiUser) {
|
||||||
|
// 若访问当前接口不需要城市参数,则返回true
|
||||||
|
if (StringUtils.isBlank(cityName)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (!RegionData.isCity(cityName)) {
|
||||||
|
String city = RegionData.formatCity(cityName);
|
||||||
|
if (null != city) {
|
||||||
|
cityName = city;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 若当前接口需要城市,检查是否有访问当前城市的权限
|
||||||
|
boolean authCity = StringUtils.isNotBlank(apiUser.getCity()) && !StringUtils.equalsIgnoreCase(apiUser.getCity(), "all");
|
||||||
|
if (authCity) {
|
||||||
|
ServerArea city = ServerAreaData.getServerAreaByDisplayDefaultNull(cityName);
|
||||||
|
if (null != city) {
|
||||||
|
return apiUser.getCity().contains(city.getSpell());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ApiUser parseApiUserByToken(Token token) {
|
||||||
|
|
||||||
|
if (token == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
ApiUser apiUser = apiUserService.findApiUserById(token.getUserId());
|
||||||
|
if (apiUser == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (!apiUser.getAppKey().equals(token.getAppKey())) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return apiUser;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void setApiUserService(ApiUserService apiUserService) {
|
/**
|
||||||
this.apiUserService = apiUserService;
|
* 新版token验证
|
||||||
}
|
*
|
||||||
|
* @param token
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private ApiUser checkNewAuthorized(String token) {
|
||||||
|
|
||||||
public void setApiUserAuthorityService(ApiUserAuthorityService apiUserAuthorityService) {
|
Map<String, String> tokenMap = FangjiaApiUtil.parseToken(token);
|
||||||
this.apiUserAuthorityService = apiUserAuthorityService;
|
if (tokenMap == null) {
|
||||||
}
|
return null;
|
||||||
|
}
|
||||||
|
// token验证后,拿取出的字段再去数据库验证一遍
|
||||||
|
if (StringUtils.isNotBlank(tokenMap.get("userid")) && !"null".equals(tokenMap.get("userid"))) {
|
||||||
|
ApiUser apiUser = apiUserService.findApiUserById(Long.parseLong(tokenMap.get("userid")));
|
||||||
|
if (apiUser != null) {
|
||||||
|
if (tokenMap.get("appKey").equals(apiUser.getAppKey())) {
|
||||||
|
return apiUser;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public void setApiVisitCountService(ApiVisitCountService apiVisitCountService) {
|
|
||||||
this.apiVisitCountService = apiVisitCountService;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
public void setApiUserService(ApiUserService apiUserService) {
|
||||||
|
this.apiUserService = apiUserService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setApiUserAuthorityService(ApiUserAuthorityService apiUserAuthorityService) {
|
||||||
|
this.apiUserAuthorityService = apiUserAuthorityService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setApiVisitCountService(ApiVisitCountService apiVisitCountService) {
|
||||||
|
this.apiVisitCountService = apiVisitCountService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setKafkaProducerUtils(KafkaProducerUtils kafkaProducerUtils) {
|
||||||
|
this.kafkaProducerUtils = kafkaProducerUtils;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+7
@@ -2,6 +2,7 @@ package com.fangjia.service.util.kafka;
|
|||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.fangjia.dal.mongo.tool.model.OpenUriLog;
|
import com.fangjia.dal.mongo.tool.model.OpenUriLog;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.kafka.clients.producer.KafkaProducer;
|
import org.apache.kafka.clients.producer.KafkaProducer;
|
||||||
import org.apache.kafka.clients.producer.ProducerConfig;
|
import org.apache.kafka.clients.producer.ProducerConfig;
|
||||||
import org.apache.kafka.clients.producer.ProducerRecord;
|
import org.apache.kafka.clients.producer.ProducerRecord;
|
||||||
@@ -39,6 +40,12 @@ public class KafkaProducerUtils implements DisposableBean {
|
|||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public void send(String topic,String msg){
|
||||||
|
if (StringUtils.isNotBlank(topic) && StringUtils.isNotBlank(msg)){
|
||||||
|
ProducerRecord<String, String> record = new ProducerRecord<String, String>(topic, msg);
|
||||||
|
this.producer.send(record);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void destroy() throws Exception {
|
public void destroy() throws Exception {
|
||||||
|
|||||||
Reference in New Issue
Block a user