|
@@ -1,5 +1,16 @@
|
|
package cn.com.qmth.stmms.api.interceptor;
|
|
package cn.com.qmth.stmms.api.interceptor;
|
|
|
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
+
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.web.method.HandlerMethod;
|
|
|
|
+import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
|
|
|
|
+
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
+
|
|
import cn.com.qmth.stmms.api.exception.ApiException;
|
|
import cn.com.qmth.stmms.api.exception.ApiException;
|
|
import cn.com.qmth.stmms.biz.basic.service.PrivilegeService;
|
|
import cn.com.qmth.stmms.biz.basic.service.PrivilegeService;
|
|
import cn.com.qmth.stmms.biz.basic.service.RolePrivilegeService;
|
|
import cn.com.qmth.stmms.biz.basic.service.RolePrivilegeService;
|
|
@@ -12,24 +23,12 @@ import cn.com.qmth.stmms.common.annotation.RoleRequire;
|
|
import cn.com.qmth.stmms.common.authorization.AuthorizationService;
|
|
import cn.com.qmth.stmms.common.authorization.AuthorizationService;
|
|
import cn.com.qmth.stmms.common.domain.ApiUser;
|
|
import cn.com.qmth.stmms.common.domain.ApiUser;
|
|
import cn.com.qmth.stmms.common.domain.AuthInfo;
|
|
import cn.com.qmth.stmms.common.domain.AuthInfo;
|
|
-import cn.com.qmth.stmms.common.domain.WebUser;
|
|
|
|
import cn.com.qmth.stmms.common.enums.Role;
|
|
import cn.com.qmth.stmms.common.enums.Role;
|
|
import cn.com.qmth.stmms.common.session.service.SessionService;
|
|
import cn.com.qmth.stmms.common.session.service.SessionService;
|
|
import cn.com.qmth.stmms.common.signature.SignatureInfo;
|
|
import cn.com.qmth.stmms.common.signature.SignatureInfo;
|
|
import cn.com.qmth.stmms.common.signature.SignatureType;
|
|
import cn.com.qmth.stmms.common.signature.SignatureType;
|
|
import cn.com.qmth.stmms.common.utils.EncryptUtils;
|
|
import cn.com.qmth.stmms.common.utils.EncryptUtils;
|
|
import cn.com.qmth.stmms.common.utils.RequestUtils;
|
|
import cn.com.qmth.stmms.common.utils.RequestUtils;
|
|
-import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
-import org.apache.commons.lang.StringUtils;
|
|
|
|
-import org.slf4j.Logger;
|
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
-import org.springframework.web.method.HandlerMethod;
|
|
|
|
-import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
|
|
|
|
-
|
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
|
-import java.util.Date;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
* API接口访问拦截器
|
|
* API接口访问拦截器
|
|
@@ -65,79 +64,70 @@ public class ApiInterceptor extends HandlerInterceptorAdapter {
|
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
|
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
|
|
throws Exception {
|
|
throws Exception {
|
|
HandlerMethod method = (HandlerMethod) handler;
|
|
HandlerMethod method = (HandlerMethod) handler;
|
|
- try {
|
|
|
|
- return validate(request, response, method.getMethodAnnotation(RoleRequire.class));
|
|
|
|
- } catch (ApiException e) {
|
|
|
|
- response.sendError(e.getCode(), StringUtils.trimToEmpty(e.getMessage()));
|
|
|
|
- return false;
|
|
|
|
- } catch (Exception ee) {
|
|
|
|
- response.sendError(HttpServletResponse.SC_UNAUTHORIZED, StringUtils.trimToEmpty(ee.getMessage()));
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
|
|
+ return validate(request, response, method.getMethodAnnotation(RoleRequire.class));
|
|
}
|
|
}
|
|
|
|
|
|
private boolean validate(HttpServletRequest request, HttpServletResponse response, RoleRequire authConfig) {
|
|
private boolean validate(HttpServletRequest request, HttpServletResponse response, RoleRequire authConfig) {
|
|
-// if (authConfig != null && authConfig.value().length > 0) {
|
|
|
|
- AuthInfo simple;
|
|
|
|
- // 优先按签名模式解析
|
|
|
|
- SignatureInfo info = authorizationService.buildSignature(request);
|
|
|
|
- if (info != null) {
|
|
|
|
- if (info.getType() == SignatureType.SECRET) {
|
|
|
|
- School school = schoolService.findByAccessKey(info.getInvoker());
|
|
|
|
- if (school == null || !info.validate(school.getAccessSecret())) {
|
|
|
|
- throw ApiException.SIGNATURE_INVALID;
|
|
|
|
- }
|
|
|
|
- if (!school.isEnable()) {
|
|
|
|
- throw ApiException.USER_DISABLED;
|
|
|
|
- }
|
|
|
|
- if (authConfig != null && authConfig.value().length > 0&& !matchRole(authConfig, Role.SCHOOL_DEV)) {
|
|
|
|
- throw ApiException.USER_ROLE_INVALID;
|
|
|
|
- }
|
|
|
|
- return buildApiUser(request, response, school);
|
|
|
|
- } else if (info.getType() == SignatureType.TOKEN) {
|
|
|
|
- User user = userService.findByLoginName(info.getInvoker());
|
|
|
|
- if (user == null ||!info.validate(user.getAccessToken())) {
|
|
|
|
- try {
|
|
|
|
- ObjectMapper mapper = new ObjectMapper();
|
|
|
|
- log.warn("api token signature invalud, signature={}, user={}",
|
|
|
|
- mapper.writeValueAsString(info),
|
|
|
|
- user != null ? mapper.writeValueAsString(user) : "{}");
|
|
|
|
- } catch (Exception ignored) {
|
|
|
|
- }
|
|
|
|
- throw ApiException.SIGNATURE_INVALID;
|
|
|
|
- }
|
|
|
|
- if (!user.isEnable()) {
|
|
|
|
- throw ApiException.USER_DISABLED;
|
|
|
|
- }
|
|
|
|
- if (authConfig != null && authConfig.value().length > 0 && !matchRole(authConfig, user.getRole())) {
|
|
|
|
- throw ApiException.USER_ROLE_INVALID;
|
|
|
|
- }
|
|
|
|
- return buildApiUser(request, response, user);
|
|
|
|
- } else {
|
|
|
|
|
|
+ // if (authConfig != null && authConfig.value().length > 0) {
|
|
|
|
+ AuthInfo simple;
|
|
|
|
+ // 优先按签名模式解析
|
|
|
|
+ SignatureInfo info = authorizationService.buildSignature(request);
|
|
|
|
+ if (info != null) {
|
|
|
|
+ if (info.getType() == SignatureType.SECRET) {
|
|
|
|
+ School school = schoolService.findByAccessKey(info.getInvoker());
|
|
|
|
+ if (school == null || !info.validate(school.getAccessSecret())) {
|
|
throw ApiException.SIGNATURE_INVALID;
|
|
throw ApiException.SIGNATURE_INVALID;
|
|
}
|
|
}
|
|
- }
|
|
|
|
- // 再尝试按简单模式解析
|
|
|
|
- else if ((simple = authorizationService.buildAuthInfo(request)) != null) {
|
|
|
|
- User user = userService.findByLoginName(simple.getLoginname());
|
|
|
|
- if (user == null) {
|
|
|
|
- throw ApiException.SIGNATURE_INVALID;
|
|
|
|
|
|
+ if (!school.isEnable()) {
|
|
|
|
+ throw ApiException.USER_DISABLED;
|
|
|
|
+ }
|
|
|
|
+ if (authConfig != null && authConfig.value().length > 0 && !matchRole(authConfig, Role.SCHOOL_DEV)) {
|
|
|
|
+ throw ApiException.USER_ROLE_INVALID;
|
|
}
|
|
}
|
|
- if( !EncryptUtils.md5(simple.getPassword()).equals(user.getPassword())){
|
|
|
|
- throw ApiException.PASSWORD_ERROR;
|
|
|
|
|
|
+ return buildApiUser(request, response, school);
|
|
|
|
+ } else if (info.getType() == SignatureType.TOKEN) {
|
|
|
|
+ User user = userService.findByLoginName(info.getInvoker());
|
|
|
|
+ if (user == null || !info.validate(user.getAccessToken())) {
|
|
|
|
+ try {
|
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
|
+ log.warn("api token signature invalud, signature={}, user={}", mapper.writeValueAsString(info),
|
|
|
|
+ user != null ? mapper.writeValueAsString(user) : "{}");
|
|
|
|
+ } catch (Exception ignored) {
|
|
|
|
+ }
|
|
|
|
+ throw ApiException.SIGNATURE_INVALID;
|
|
}
|
|
}
|
|
if (!user.isEnable()) {
|
|
if (!user.isEnable()) {
|
|
throw ApiException.USER_DISABLED;
|
|
throw ApiException.USER_DISABLED;
|
|
}
|
|
}
|
|
- if (authConfig != null && authConfig.value().length > 0&& !matchRole(authConfig, user.getRole())) {
|
|
|
|
|
|
+ if (authConfig != null && authConfig.value().length > 0 && !matchRole(authConfig, user.getRole())) {
|
|
throw ApiException.USER_ROLE_INVALID;
|
|
throw ApiException.USER_ROLE_INVALID;
|
|
}
|
|
}
|
|
return buildApiUser(request, response, user);
|
|
return buildApiUser(request, response, user);
|
|
} else {
|
|
} else {
|
|
- throw ApiException.AUTHORIZATION_UNEXIST;
|
|
|
|
|
|
+ throw ApiException.SIGNATURE_INVALID;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // 再尝试按简单模式解析
|
|
|
|
+ else if ((simple = authorizationService.buildAuthInfo(request)) != null) {
|
|
|
|
+ User user = userService.findByLoginName(simple.getLoginname());
|
|
|
|
+ if (user == null) {
|
|
|
|
+ throw ApiException.SIGNATURE_INVALID;
|
|
}
|
|
}
|
|
-// }
|
|
|
|
-// return true;
|
|
|
|
|
|
+ if (!EncryptUtils.md5(simple.getPassword()).equals(user.getPassword())) {
|
|
|
|
+ throw ApiException.PASSWORD_ERROR;
|
|
|
|
+ }
|
|
|
|
+ if (!user.isEnable()) {
|
|
|
|
+ throw ApiException.USER_DISABLED;
|
|
|
|
+ }
|
|
|
|
+ if (authConfig != null && authConfig.value().length > 0 && !matchRole(authConfig, user.getRole())) {
|
|
|
|
+ throw ApiException.USER_ROLE_INVALID;
|
|
|
|
+ }
|
|
|
|
+ return buildApiUser(request, response, user);
|
|
|
|
+ } else {
|
|
|
|
+ throw ApiException.AUTHORIZATION_UNEXIST;
|
|
|
|
+ }
|
|
|
|
+ // }
|
|
|
|
+ // return true;
|
|
}
|
|
}
|
|
|
|
|
|
private boolean matchRole(RoleRequire authConfig, Role role) {
|
|
private boolean matchRole(RoleRequire authConfig, Role role) {
|