| @@ -0,0 +1,16 @@ | |||
| package com.iformall.annotation; | |||
| import java.lang.annotation.*; | |||
| /** | |||
| * api接口,忽略Token验证 | |||
| * @author stormeye.wu | |||
| * @email wuguoqiang@iformall.com | |||
| * @date 2017-03-23 15:44 | |||
| */ | |||
| @Target(ElementType.METHOD) | |||
| @Retention(RetentionPolicy.RUNTIME) | |||
| @Documented | |||
| public @interface TenantIgnore { | |||
| } | |||
| @@ -0,0 +1,48 @@ | |||
| package com.iformall.interceptor; | |||
| import javax.servlet.http.HttpServletRequest; | |||
| import javax.servlet.http.HttpServletResponse; | |||
| import org.apache.shiro.SecurityUtils; | |||
| import org.apache.shiro.session.Session; | |||
| import org.apache.shiro.subject.Subject; | |||
| import org.springframework.stereotype.Component; | |||
| import org.springframework.web.method.HandlerMethod; | |||
| import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; | |||
| import com.iformall.annotation.TenantIgnore; | |||
| import com.iformall.common.TenantThreadLocal; | |||
| import com.iformall.shiro.UserSession; | |||
| /** | |||
| * 权限(Token)验证 | |||
| * @author stormeye.wu | |||
| * @email wuguoqiang@iformall.com | |||
| * @date 2017-03-23 15:38 | |||
| */ | |||
| @Component | |||
| public class CurrentTenantInterceptor extends HandlerInterceptorAdapter { | |||
| @Override | |||
| public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | |||
| Subject s = SecurityUtils.getSubject(); | |||
| if (null != s ) { | |||
| Session session = s.getSession(); | |||
| if (null != session ) { | |||
| TenantThreadLocal.setCurrentThreadTenant((String)session.getAttribute(UserSession.tenantId), | |||
| (String)session.getAttribute(UserSession.parentTenantId)); | |||
| } | |||
| } | |||
| TenantIgnore annotation = null; | |||
| if(handler instanceof HandlerMethod) { | |||
| annotation = ((HandlerMethod) handler).getMethodAnnotation(TenantIgnore.class); | |||
| } | |||
| //如果有@TenantIgnore注解,则把tenant信息全部清掉 | |||
| if(annotation != null){ | |||
| TenantThreadLocal.remove(); | |||
| } | |||
| return true; | |||
| } | |||
| } | |||
| @@ -69,6 +69,7 @@ public class MyShiroRealm extends AuthorizingRealm { | |||
| if (StringUtils.isNotBlank(user.getParentTenantId())) { | |||
| session.setAttribute(UserSession.parentTenantId, user.getParentTenantId()); | |||
| } | |||
| return authenticationInfo; | |||
| } | |||
| @@ -1,5 +1,6 @@ | |||
| package com.iformall.tenant; | |||
| import com.iformall.common.TenantThreadLocal; | |||
| import com.iformall.plugin.TenantInfo; | |||
| import com.iformall.shiro.UserSession; | |||
| import org.apache.ibatis.mapping.MappedStatement; | |||
| @@ -10,12 +11,13 @@ import org.slf4j.LoggerFactory; | |||
| public class TenantInfoImpl implements TenantInfo { | |||
| private final Logger logger = LoggerFactory.getLogger(this.getClass()); | |||
| @Override | |||
| public String getTenantId() { | |||
| String tenantId = null; | |||
| try { | |||
| tenantId = (String) SecurityUtils.getSubject().getSession().getAttribute(UserSession.tenantId); | |||
| //tenantId = (String) SecurityUtils.getSubject().getSession().getAttribute(UserSession.tenantId); | |||
| tenantId = TenantThreadLocal.getTenantId(); | |||
| } catch (InvalidSessionException e) { | |||
| logger.error("InvalidSession: " + e.getMessage()); | |||
| } | |||
| @@ -26,7 +28,8 @@ public class TenantInfoImpl implements TenantInfo { | |||
| public String getParentTenantId() { | |||
| String parentTenantId = null; | |||
| try { | |||
| parentTenantId = (String) SecurityUtils.getSubject().getSession().getAttribute(UserSession.parentTenantId); | |||
| //parentTenantId = (String) SecurityUtils.getSubject().getSession().getAttribute(UserSession.parentTenantId); | |||
| parentTenantId = TenantThreadLocal.getTenantId(); | |||
| } catch (InvalidSessionException e) { | |||
| logger.error("InvalidSession: " + e.getMessage()); | |||
| } | |||
| @@ -0,0 +1,16 @@ | |||
| package com.iformall.annotation; | |||
| import java.lang.annotation.*; | |||
| /** | |||
| * api接口,忽略Token验证 | |||
| * @author stormeye.wu | |||
| * @email wuguoqiang@iformall.com | |||
| * @date 2017-03-23 15:44 | |||
| */ | |||
| @Target(ElementType.METHOD) | |||
| @Retention(RetentionPolicy.RUNTIME) | |||
| @Documented | |||
| public @interface TenantIgnore { | |||
| } | |||
| @@ -2,7 +2,9 @@ package com.iformall.interceptor; | |||
| import com.iformall.annotation.AuthIgnore; | |||
| import com.iformall.annotation.TenantIgnore; | |||
| import com.iformall.common.ErrorCode; | |||
| import com.iformall.common.TenantThreadLocal; | |||
| import com.iformall.domain.po.WxBuser; | |||
| import com.iformall.exception.MallinkException; | |||
| import com.iformall.service.WxBuserTokenService; | |||
| @@ -61,11 +63,28 @@ public class AuthorizationInterceptor extends HandlerInterceptorAdapter { | |||
| if (wxBuser == null || wxBuser.getExpireTime().getTime() < System.currentTimeMillis()) { | |||
| throw new MallinkException(ErrorCode.NET_TOKEN_INVALID.getCode(), "URL:" + request.getRequestURL() + " token失效,请重新登录"); | |||
| } | |||
| //设置userId到request里,后续根据userId,获取用户信息 | |||
| request.setAttribute(Constant.LOGIN_USER_KEY, wxBuser.getId()); | |||
| request.setAttribute(Constant.TENANT_ID, wxBuser.getTenantId()); | |||
| //如果是要不需要自动设置tenantI信息 | |||
| TenantIgnore tenantIgnoreAnnotation = null; | |||
| if(handler instanceof HandlerMethod) { | |||
| tenantIgnoreAnnotation = ((HandlerMethod) handler).getMethodAnnotation(TenantIgnore.class); | |||
| } | |||
| //如果有@TenantIgnore注解,则把tenant信息全部清掉 | |||
| if(tenantIgnoreAnnotation != null){ | |||
| TenantThreadLocal.remove(); | |||
| }else { | |||
| TenantThreadLocal.setCurrentThreadTenant(wxBuser.getTenantId(), wxBuser.getParentTenantId()); | |||
| } | |||
| return true; | |||
| } | |||
| } | |||
| @@ -1,5 +1,6 @@ | |||
| package com.iformall.tenant; | |||
| import com.iformall.common.TenantThreadLocal; | |||
| import com.iformall.plugin.TenantInfo; | |||
| import com.iformall.utils.Constant; | |||
| import org.apache.ibatis.mapping.MappedStatement; | |||
| @@ -17,8 +18,9 @@ public class TenantInfoImpl implements TenantInfo { | |||
| public String getTenantId() { | |||
| String tenantId = null; | |||
| try { | |||
| HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | |||
| tenantId = (String) request.getAttribute(Constant.TENANT_ID); | |||
| //HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | |||
| //tenantId = (String) request.getAttribute(Constant.TENANT_ID); | |||
| tenantId = TenantThreadLocal.getTenantId(); | |||
| return tenantId; | |||
| } catch (Exception e) { | |||
| logger.error("getTenantId: " + e.getMessage()); | |||
| @@ -30,8 +32,9 @@ public class TenantInfoImpl implements TenantInfo { | |||
| public String getParentTenantId() { | |||
| String parentTenantId = null; | |||
| try { | |||
| HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | |||
| parentTenantId = (String) request.getAttribute(Constant.PARENT_TENANT_ID); | |||
| //HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | |||
| //parentTenantId = (String) request.getAttribute(Constant.PARENT_TENANT_ID); | |||
| parentTenantId = TenantThreadLocal.getParentTenantId(); | |||
| return parentTenantId; | |||
| } catch (Exception e) { | |||
| logger.error(e.getMessage()); | |||
| @@ -0,0 +1,16 @@ | |||
| package com.iformall.annotation; | |||
| import java.lang.annotation.*; | |||
| /** | |||
| * api接口,忽略Token验证 | |||
| * @author stormeye.wu | |||
| * @email wuguoqiang@iformall.com | |||
| * @date 2017-03-23 15:44 | |||
| */ | |||
| @Target(ElementType.METHOD) | |||
| @Retention(RetentionPolicy.RUNTIME) | |||
| @Documented | |||
| public @interface TenantIgnore { | |||
| } | |||
| @@ -2,7 +2,9 @@ package com.iformall.interceptor; | |||
| import com.iformall.annotation.AuthIgnore; | |||
| import com.iformall.annotation.TenantIgnore; | |||
| import com.iformall.common.ErrorCode; | |||
| import com.iformall.common.TenantThreadLocal; | |||
| import com.iformall.domain.po.WxCUser; | |||
| import com.iformall.exception.MallinkException; | |||
| import com.iformall.service.WxCUserTokenService; | |||
| @@ -68,6 +70,21 @@ public class AuthorizationInterceptor extends HandlerInterceptorAdapter { | |||
| if (StringUtils.isNotBlank(wxCUser.getParentTenantId())) { | |||
| request.setAttribute(Constant.PARENT_TENANT_ID, wxCUser.getParentTenantId()); | |||
| } | |||
| //如果是要不需要自动设置tenantI信息 | |||
| TenantIgnore tenantIgnoreAnnotation = null; | |||
| if(handler instanceof HandlerMethod) { | |||
| tenantIgnoreAnnotation = ((HandlerMethod) handler).getMethodAnnotation(TenantIgnore.class); | |||
| } | |||
| //如果有@TenantIgnore注解,则把tenant信息全部清掉 | |||
| if(tenantIgnoreAnnotation != null){ | |||
| TenantThreadLocal.remove(); | |||
| }else { | |||
| TenantThreadLocal.setCurrentThreadTenant(wxCUser.getTenantId(), wxCUser.getParentTenantId()); | |||
| } | |||
| return true; | |||
| } | |||
| @@ -1,5 +1,6 @@ | |||
| package com.iformall.tenant; | |||
| import com.iformall.common.TenantThreadLocal; | |||
| import com.iformall.plugin.TenantInfo; | |||
| import com.iformall.utils.Constant; | |||
| import org.apache.ibatis.mapping.MappedStatement; | |||
| @@ -17,8 +18,9 @@ public class TenantInfoImpl implements TenantInfo { | |||
| public String getTenantId() { | |||
| String tenantId = null; | |||
| try { | |||
| HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | |||
| tenantId = (String) request.getAttribute(Constant.TENANT_ID); | |||
| //HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | |||
| //tenantId = (String) request.getAttribute(Constant.TENANT_ID); | |||
| tenantId = TenantThreadLocal.getTenantId(); | |||
| return tenantId; | |||
| } catch (Exception e) { | |||
| logger.error(e.getMessage()); | |||
| @@ -30,8 +32,9 @@ public class TenantInfoImpl implements TenantInfo { | |||
| public String getParentTenantId() { | |||
| String parentTenantId = null; | |||
| try { | |||
| HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | |||
| parentTenantId = (String) request.getAttribute(Constant.PARENT_TENANT_ID); | |||
| //HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | |||
| //parentTenantId = (String) request.getAttribute(Constant.PARENT_TENANT_ID); | |||
| parentTenantId = TenantThreadLocal.getParentTenantId(); | |||
| return parentTenantId; | |||
| } catch (Exception e) { | |||
| logger.error(e.getMessage()); | |||
| @@ -0,0 +1,26 @@ | |||
| package com.iformall.common; | |||
| public class TenantThreadLocal { | |||
| private static ThreadLocal<String> local = new ThreadLocal<String>(); | |||
| private static ThreadLocal<String> parentLocal = new ThreadLocal<String>(); | |||
| public static void setCurrentThreadTenant(String tenantId,String parentId) { | |||
| local.set(tenantId); | |||
| parentLocal.set(parentId); | |||
| } | |||
| public static String getTenantId() { | |||
| return local.get(); | |||
| } | |||
| public static String getParentTenantId() { | |||
| return parentLocal.get(); | |||
| } | |||
| public static void remove() { | |||
| local.remove(); | |||
| parentLocal.remove(); | |||
| } | |||
| } | |||