|
|
|
@ -6,6 +6,7 @@ import cc.hiver.core.common.utils.ResponseUtil; |
|
|
|
import cc.hiver.core.common.utils.SecurityUtil; |
|
|
|
import cc.hiver.core.common.vo.TokenMember; |
|
|
|
import cc.hiver.core.common.vo.TokenUser; |
|
|
|
import cc.hiver.core.common.vo.TokenWorker; |
|
|
|
import cc.hiver.core.config.properties.HiverAppTokenProperties; |
|
|
|
import cc.hiver.core.config.properties.HiverTokenProperties; |
|
|
|
import cn.hutool.core.util.StrUtil; |
|
|
|
@ -73,8 +74,12 @@ public class TokenAuthenticationFilter extends BasicAuthenticationFilter { |
|
|
|
if (StrUtil.isBlank(appHeader)) { |
|
|
|
appHeader = request.getParameter(SecurityConstant.APP_HEADER); |
|
|
|
} |
|
|
|
String appYSHeader = request.getHeader(SecurityConstant.APP_YS_HEADER); |
|
|
|
if (StrUtil.isBlank(appYSHeader)) { |
|
|
|
appYSHeader = request.getParameter(SecurityConstant.APP_YS_HEADER); |
|
|
|
} |
|
|
|
Boolean notValid = (StrUtil.isBlank(header) || (!tokenProperties.getRedis() && !header.startsWith(SecurityConstant.TOKEN_SPLIT))) |
|
|
|
&& StrUtil.isBlank(appHeader); |
|
|
|
&& StrUtil.isBlank(appHeader) && StrUtil.isBlank(appYSHeader); |
|
|
|
if (notValid) { |
|
|
|
chain.doFilter(request, response); |
|
|
|
return; |
|
|
|
@ -83,8 +88,10 @@ public class TokenAuthenticationFilter extends BasicAuthenticationFilter { |
|
|
|
UsernamePasswordAuthenticationToken authentication = null; |
|
|
|
if (StrUtil.isNotBlank(header)) { |
|
|
|
authentication = getAuthentication(header, response); |
|
|
|
} else { |
|
|
|
} else if(StrUtil.isNotBlank(appHeader)){ |
|
|
|
authentication = getAppAuthentication(appHeader, response); |
|
|
|
} else { |
|
|
|
authentication = getAppYSAuthentication(appYSHeader, response); |
|
|
|
} |
|
|
|
if (authentication == null) { |
|
|
|
return; |
|
|
|
@ -194,4 +201,43 @@ public class TokenAuthenticationFilter extends BasicAuthenticationFilter { |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
private UsernamePasswordAuthenticationToken getAppYSAuthentication(String appYSHeader, HttpServletResponse response) { |
|
|
|
TokenWorker tokenWorker = null; |
|
|
|
List<GrantedAuthority> authorities = new ArrayList<>(); |
|
|
|
|
|
|
|
if (appTokenProperties.getRedis()) { |
|
|
|
// redis
|
|
|
|
String v = redisTemplate.get(SecurityConstant.TOKEN_WORKER_PRE + appYSHeader); |
|
|
|
if (StrUtil.isBlank(v)) { |
|
|
|
ResponseUtil.out(response, ResponseUtil.resultMap(false, 401, "扛包工登录已失效,请重新登录")); |
|
|
|
return null; |
|
|
|
} |
|
|
|
tokenWorker = new Gson().fromJson(v, TokenWorker.class); |
|
|
|
// 权限
|
|
|
|
// 重新设置失效时间
|
|
|
|
redisTemplate.set(SecurityConstant.WORKER_TOKEN + tokenWorker.getWorkerName(), appYSHeader, appTokenProperties.getTokenExpireTime(), TimeUnit.DAYS); |
|
|
|
redisTemplate.set(SecurityConstant.TOKEN_WORKER_PRE + appYSHeader, v, appTokenProperties.getTokenExpireTime(), TimeUnit.DAYS); |
|
|
|
} else { |
|
|
|
// JWT
|
|
|
|
try { |
|
|
|
// 解析token
|
|
|
|
Claims claims = Jwts.parser() |
|
|
|
.setSigningKey(SecurityConstant.JWT_SIGN_KEY) |
|
|
|
.parseClaimsJws(appYSHeader.replace(SecurityConstant.TOKEN_SPLIT, "")) |
|
|
|
.getBody(); |
|
|
|
// 获取用户
|
|
|
|
tokenWorker = new Gson().fromJson(claims.getSubject(), TokenWorker.class); |
|
|
|
|
|
|
|
} catch (ExpiredJwtException e) { |
|
|
|
ResponseUtil.out(response, ResponseUtil.resultMap(false, 401, "登录已失效,请重新登录")); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error(e.toString()); |
|
|
|
ResponseUtil.out(response, ResponseUtil.resultMap(false, 500, "解析token错误")); |
|
|
|
} |
|
|
|
} |
|
|
|
if (tokenWorker != null && StrUtil.isNotBlank(tokenWorker.getWorkerId())) { |
|
|
|
return new UsernamePasswordAuthenticationToken(tokenWorker, null, null); |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|