Parker 4 years ago
commit d1ace77e08

@ -13,9 +13,17 @@
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.opsli.common.api;
package org.opsli.core.api;
import org.apache.commons.lang3.StringUtils;
import org.opsli.core.utils.UserTokenUtil;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
/**
* 线 Token
*
@ -34,7 +42,24 @@ public class TokenThreadLocal {
}
public static String get() {
return TOKEN_DATA.get();
String token = TOKEN_DATA.get();
// 2021-03-10
// 这里纠正 Token 在被多聚合项目 aop切面 remove后 无法获得Token bug
// 如果 token 为空 则尝试去 request 获取
if(StringUtils.isEmpty(token)){
try {
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
if (sra != null) {
HttpServletRequest request = sra.getRequest();
token = UserTokenUtil.getRequestToken(request);
}
}catch (Exception ignored){}
}
return token;
}
public static void remove() {

@ -23,7 +23,7 @@ import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.opsli.common.api.TokenThreadLocal;
import org.opsli.core.api.TokenThreadLocal;
import org.opsli.common.exception.ServiceException;
import org.opsli.core.utils.LogUtil;
import org.opsli.core.utils.UserTokenUtil;

@ -1,7 +1,6 @@
package org.opsli.core.security.shiro.realm;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
@ -9,7 +8,7 @@ import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.common.api.TokenThreadLocal;
import org.opsli.core.api.TokenThreadLocal;
import org.opsli.common.exception.TokenException;
import org.opsli.core.msg.TokenMsg;
import org.opsli.core.security.shiro.token.JwtToken;
@ -17,8 +16,6 @@ import org.opsli.core.utils.UserTokenUtil;
import org.opsli.core.utils.UserUtil;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.List;
/**

@ -24,7 +24,7 @@ import org.opsli.api.base.result.ResultVo;
import org.opsli.api.web.system.user.UserApi;
import org.opsli.api.wrapper.system.menu.MenuModel;
import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.common.api.TokenThreadLocal;
import org.opsli.core.api.TokenThreadLocal;
import org.opsli.common.exception.TokenException;
import org.opsli.core.autoconfigure.properties.GlobalProperties;
import org.opsli.core.cache.local.CacheUtil;
@ -34,11 +34,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import static org.opsli.common.constants.OrderConstants.UTIL_ORDER;
@ -79,18 +74,6 @@ public class UserUtil {
public static UserModel getUser(){
String token = TokenThreadLocal.get();
// 如果 token 为空 则尝试去 request 获取
if(StringUtils.isEmpty(token)){
try {
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
if (sra != null) {
HttpServletRequest request = sra.getRequest();
token = UserTokenUtil.getRequestToken(request);
}
}catch (Exception ignored){}
}
// 如果还是没获取到token 则抛出异常
if(StringUtils.isEmpty(token)){
// Token失效请重新登录

@ -26,7 +26,7 @@ import org.opsli.api.wrapper.system.tenant.TenantModel;
import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.common.annotation.InterfaceCrypto;
import org.opsli.common.annotation.Limiter;
import org.opsli.common.api.TokenThreadLocal;
import org.opsli.core.api.TokenThreadLocal;
import org.opsli.common.enums.AlertType;
import org.opsli.common.enums.OptionsType;
import org.opsli.common.exception.TokenException;

Loading…
Cancel
Save