@ -1,5 +1,6 @@
package cn.jlw.Interceptor ;
import cn.hutool.extra.spring.SpringUtil ;
import cn.jlw.util.ToolUtils ;
import com.alibaba.fastjson.JSONArray ;
import com.auth0.jwt.JWT ;
@ -8,6 +9,11 @@ import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException ;
import com.auth0.jwt.exceptions.JWTVerificationException ;
import com.auth0.jwt.interfaces.DecodedJWT ;
import com.google.common.cache.CacheBuilder ;
import com.google.common.cache.CacheLoader ;
import com.google.common.cache.LoadingCache ;
import com.ibeetl.admin.core.rbac.UserLoginInfo ;
import com.ibeetl.admin.core.service.CoreUserService ;
import com.ibeetl.jlw.entity.AbstractToken ;
import com.ibeetl.jlw.service.WebPlatformService ;
import org.apache.commons.lang3.StringUtils ;
@ -26,8 +32,10 @@ import java.util.HashMap;
import java.util.Map ;
import java.util.UUID ;
import java.util.concurrent.ConcurrentHashMap ;
import java.util.concurrent.TimeUnit ;
import static cn.jlw.token.TokenService.tokenMap ;
import static com.ibeetl.admin.core.util.servlet.ServletUtils.getRequest ;
/ * *
* @author TLT
@ -40,7 +48,18 @@ public class AuthenticationInterceptor implements HandlerInterceptor {
@Autowired
ActionLog actionLog ;
public static Map < String , String > indexTokenMap = new ConcurrentHashMap < > ( ) ;
// public static Map<String,String> indexTokenMap = new ConcurrentHashMap<>();
// public static DefaultRedisMap<String,String> indexTokenMap = new DefaultRedisMap<>("core:token:indexTokenMap", SpringUtil.getBean("stringRedisTemplate"));
public static LoadingCache < String , String > indexTokenMap = CacheBuilder . newBuilder ( )
. expireAfterAccess ( 8 , TimeUnit . HOURS )
. build ( new CacheLoader < String , String > ( ) {
// 处理缓存键不存在缓存值时的处理逻辑
@Override
public String load ( String key ) {
return "不存在的key" ;
}
} ) ;
public static Map < String , String > keyIdOpenIdMap = new ConcurrentHashMap < > ( ) ;
public static Map < String , Object [ ] > workMap = new ConcurrentHashMap < > ( ) ; //用来存储后台工作的人员正在进行的操作
@ -58,25 +77,26 @@ public class AuthenticationInterceptor implements HandlerInterceptor {
public static boolean run ( ActionLog actionLog , HttpServletRequest httpServletRequest , HttpServletResponse httpServletResponse , Object object ) throws Exception {
if ( null ! = httpServletRequest
& & null ! = httpServletRequest . getRequestURI ( )
& & httpServletRequest . getRequestURI ( ) . endsWith ( "pzwj.exe" ) ) {
if ( null ! = httpServletRequest ) {
String requestURI = httpServletRequest . getRequestURI ( ) ;
if ( null ! = requestURI & & requestURI . endsWith ( "pzwj.exe" ) ) {
String fileName = "配置工具.exe" ;
String fileName = "配置工具.exe" ;
if ( null ! = httpServletRequest & & null ! = httpServletRequest . getHeader ( "User-Agent" ) ) {
if ( httpServletRequest . getHeader ( "User-Agent" ) . toLowerCase ( ) . indexOf ( "firefox" ) > 0 ) {
fileName = new String ( fileName . getBytes ( StandardCharsets . UTF_8 ) , "ISO8859-1" ) ; // firefox浏览器
} else if ( httpServletRequest . getHeader ( "User-Agent" ) . toUpperCase ( ) . indexOf ( "CHROME" ) > 0 ) {
fileName = new String ( fileName . getBytes ( StandardCharsets . UTF_8 ) , "ISO8859-1" ) ; // 谷歌
} else {
if ( null ! = httpServletRequest & & null ! = httpServletRequest . getHeader ( "User-Agent" ) ) {
if ( httpServletRequest . getHeader ( "User-Agent" ) . toLowerCase ( ) . indexOf ( "firefox" ) > 0 ) {
fileName = new String ( fileName . getBytes ( StandardCharsets . UTF_8 ) , "ISO8859-1" ) ; // firefox浏览器
} else if ( httpServletRequest . getHeader ( "User-Agent" ) . toUpperCase ( ) . indexOf ( "CHROME" ) > 0 ) {
fileName = new String ( fileName . getBytes ( StandardCharsets . UTF_8 ) , "ISO8859-1" ) ; // 谷歌
} else {
fileName = URLEncoder . encode ( fileName , "UTF-8" ) ; // IE浏览器 或其他浏览器
}
} else {
fileName = URLEncoder . encode ( fileName , "UTF-8" ) ; // IE浏览器 或其他浏览器
}
} else {
fileName = URLEncoder . encode ( fileName , "UTF-8" ) ; // IE浏览器 或其他浏览器
}
httpServletResponse . setHeader ( "Content-Disposition" , "attachment; filename=" + fileName ) ;
httpServletResponse . setHeader ( "Content-Disposition" , "attachment; filename=" + fileName ) ;
}
}
// 如果不是映射到方法直接通过
@ -175,23 +195,65 @@ public class AuthenticationInterceptor implements HandlerInterceptor {
}
if ( StringUtils . isBlank ( token ) & & httpServletRequest . getRequestURL ( ) . toString ( ) . contains ( "index.do" ) ) { //从参数中获取
if ( null ! = httpServletRequest . getParameter ( "key_id" ) ) {
token = indexTokenMap . get ( httpServletRequest . getParameter ( "key_id" ) ) ;
String key_id = httpServletRequest . getParameter ( "key_id" ) ;
if ( null ! = key_id ) {
token = indexTokenMap . getUnchecked ( key_id ) ;
}
}
String loginTime = "" ;
String [ ] jwtStrings = jwtTokenSplitDateTime ( token ) ;
String loginTime = jwtStrings [ 1 ] ;
token = jwtStrings [ 0 ] ;
httpServletRequest . setAttribute ( "token" , token ) ;
httpServletRequest . setAttribute ( "loginTime" , loginTime ) ;
return token ;
}
/ * *
* 功 能 描 述 : < br >
* 处 理 jwtToken 串 和 时 间 戳
*
* @param token
* @return { @link String [ ] }
* @Author : lx
* @Date : 2022 / 12 / 3 22 : 58
* /
public static String [ ] jwtTokenSplitDateTime ( String token ) {
String [ ] str = new String [ 2 ] ;
//断token的尾巴
if ( StringUtils . isNotBlank ( token ) ) {
if ( token . split ( "\\." ) . length > 3 ) {
loginTime = token . split ( "\\." ) [ 3 ] ;
String [ ] split = token . split ( "\\." ) ;
if ( split . length > 3 ) {
String loginTime = split [ 3 ] ;
token = token . replace ( "." + loginTime , "" ) ;
str [ 0 ] = token ;
str [ 1 ] = loginTime ;
}
}
return str ;
}
httpServletRequest . setAttribute ( "token" , token ) ;
httpServletRequest . setAttribute ( "loginTime" , loginTime ) ;
/ * *
* 功 能 描 述 : < br >
* 根 据 KeyId 填 充 当 前 的 request . session 属 性 。 缺 点 : 一 次 登 录 可 能 要 浪 费 两 块 空 间
* keyId 只 能 用 一 次
*
* @Author : lx
* @Date : 2022 / 12 / 3 22 : 37
* /
public static String fullSessionInfoWithKeyId ( ) {
return token ;
WebPlatformService webPlatformService = SpringUtil . getBean ( WebPlatformService . class ) ;
CoreUserService coreUserService = SpringUtil . getBean ( CoreUserService . class ) ;
String jwtToken = getToken ( getRequest ( ) ) ;
String coreUserId = JWT . decode ( jwtToken ) . getAudience ( ) . get ( 0 ) ;
UserLoginInfo userLoginInfo = webPlatformService
. buildLoginInfo ( coreUserService . getUserById ( Long . valueOf ( coreUserId ) ) ) ;
webPlatformService . setLoginUser ( userLoginInfo ) ;
return jwtToken ;
}
}