|
@@ -3,16 +3,19 @@ package org.hsweb.web.core.authorize;
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|
import org.aspectj.lang.reflect.MethodSignature;
|
|
import org.aspectj.lang.reflect.MethodSignature;
|
|
import org.hsweb.web.core.authorize.annotation.Authorize;
|
|
import org.hsweb.web.core.authorize.annotation.Authorize;
|
|
|
|
+import org.hsweb.web.core.authorize.oauth2.OAuth2Manager;
|
|
import org.hsweb.web.core.authorize.validator.SimpleAuthorizeValidator;
|
|
import org.hsweb.web.core.authorize.validator.SimpleAuthorizeValidator;
|
|
import org.hsweb.web.bean.po.user.User;
|
|
import org.hsweb.web.bean.po.user.User;
|
|
import org.hsweb.web.core.exception.AuthorizeException;
|
|
import org.hsweb.web.core.exception.AuthorizeException;
|
|
import org.hsweb.web.core.session.HttpSessionManager;
|
|
import org.hsweb.web.core.session.HttpSessionManager;
|
|
import org.hsweb.web.core.utils.AopUtils;
|
|
import org.hsweb.web.core.utils.AopUtils;
|
|
|
|
+import org.hsweb.web.core.utils.ThreadLocalUtils;
|
|
import org.hsweb.web.core.utils.WebUtil;
|
|
import org.hsweb.web.core.utils.WebUtil;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.hsweb.commons.ClassUtils;
|
|
import org.hsweb.commons.ClassUtils;
|
|
import org.hsweb.commons.StringUtils;
|
|
import org.hsweb.commons.StringUtils;
|
|
|
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpSession;
|
|
import javax.servlet.http.HttpSession;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.LinkedHashSet;
|
|
import java.util.LinkedHashSet;
|
|
@@ -26,6 +29,20 @@ import java.util.concurrent.ConcurrentMap;
|
|
*/
|
|
*/
|
|
public class AopAuthorizeValidator extends SimpleAuthorizeValidator {
|
|
public class AopAuthorizeValidator extends SimpleAuthorizeValidator {
|
|
|
|
|
|
|
|
+ private HttpSessionManager httpSessionManager;
|
|
|
|
+
|
|
|
|
+ private OAuth2Manager oAuth2Manager;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ public void setHttpSessionManager(HttpSessionManager httpSessionManager) {
|
|
|
|
+ this.httpSessionManager = httpSessionManager;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Autowired(required = false)
|
|
|
|
+ public void setoAuth2Manager(OAuth2Manager oAuth2Manager) {
|
|
|
|
+ this.oAuth2Manager = oAuth2Manager;
|
|
|
|
+ }
|
|
|
|
+
|
|
protected ConcurrentMap<String, AuthorizeValidatorConfig> configCache = new ConcurrentHashMap<>();
|
|
protected ConcurrentMap<String, AuthorizeValidatorConfig> configCache = new ConcurrentHashMap<>();
|
|
|
|
|
|
protected AuthorizeValidatorConfig getConfig(ProceedingJoinPoint pjp) {
|
|
protected AuthorizeValidatorConfig getConfig(ProceedingJoinPoint pjp) {
|
|
@@ -54,20 +71,29 @@ public class AopAuthorizeValidator extends SimpleAuthorizeValidator {
|
|
return config;
|
|
return config;
|
|
}
|
|
}
|
|
|
|
|
|
- private HttpSessionManager httpSessionManager;
|
|
|
|
-
|
|
|
|
- @Autowired
|
|
|
|
- public void setHttpSessionManager(HttpSessionManager httpSessionManager) {
|
|
|
|
- this.httpSessionManager = httpSessionManager;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
public boolean validate(ProceedingJoinPoint pjp) {
|
|
public boolean validate(ProceedingJoinPoint pjp) {
|
|
AuthorizeValidatorConfig config = getConfig(pjp);
|
|
AuthorizeValidatorConfig config = getConfig(pjp);
|
|
if (config == null) return true;
|
|
if (config == null) return true;
|
|
- HttpSession session = WebUtil.getHttpServletRequest().getSession(false);
|
|
|
|
- if (session == null) throw new AuthorizeException("未登录", 401);
|
|
|
|
- User user = httpSessionManager.getUserBySessionId(session.getId());
|
|
|
|
- if (user == null) throw new AuthorizeException("未登录", 401);
|
|
|
|
|
|
+ User user = null;
|
|
|
|
+ HttpServletRequest request = WebUtil.getHttpServletRequest();
|
|
|
|
+ //api OAuth2 认证
|
|
|
|
+ if (config.isApiSupport()) {
|
|
|
|
+ if (oAuth2Manager != null) {
|
|
|
|
+ String token = oAuth2Manager.getAccessTokenByRequest(request);
|
|
|
|
+ if (token != null) {
|
|
|
|
+ user = oAuth2Manager.getUserByAccessToken(token);
|
|
|
|
+ if (user == null) {
|
|
|
|
+ throw new AuthorizeException("invalid_token", 401);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (user == null) {
|
|
|
|
+ HttpSession session = request.getSession(false);
|
|
|
|
+ if (session == null) throw new AuthorizeException("未登录", 401);
|
|
|
|
+ user = httpSessionManager.getUserBySessionId(session.getId());
|
|
|
|
+ if (user == null) throw new AuthorizeException("未登录", 401);
|
|
|
|
+ }
|
|
if (config.isEmpty()) return true;
|
|
if (config.isEmpty()) return true;
|
|
Map<String, Object> param = new LinkedHashMap<>();
|
|
Map<String, Object> param = new LinkedHashMap<>();
|
|
MethodSignature signature = (MethodSignature) pjp.getSignature();
|
|
MethodSignature signature = (MethodSignature) pjp.getSignature();
|