|
@@ -3,6 +3,7 @@ package com.koobietech.eas.security.filter;
|
|
import com.koobietech.eas.common.exception.EasException;
|
|
import com.koobietech.eas.common.exception.EasException;
|
|
import com.koobietech.eas.common.pojo.JwtUserPojo;
|
|
import com.koobietech.eas.common.pojo.JwtUserPojo;
|
|
import com.koobietech.eas.common.utils.JwtManager;
|
|
import com.koobietech.eas.common.utils.JwtManager;
|
|
|
|
+import com.koobietech.eas.common.utils.ResponseManager;
|
|
import com.koobietech.eas.dao.pojo.UserDetailPojo;
|
|
import com.koobietech.eas.dao.pojo.UserDetailPojo;
|
|
import com.koobietech.eas.service.EasSysUserLoginRedisService;
|
|
import com.koobietech.eas.service.EasSysUserLoginRedisService;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
@@ -41,52 +42,47 @@ public class EasSecurityFilter extends OncePerRequestFilter {
|
|
|
|
|
|
//从请求里面拿到token
|
|
//从请求里面拿到token
|
|
String token = request.getHeader("Authorization");
|
|
String token = request.getHeader("Authorization");
|
|
- //判断token是否存在
|
|
|
|
- if (StringUtils.hasText(token)) {
|
|
|
|
- //解析token成JwtUserDto
|
|
|
|
- JwtUserPojo jwtUserPojo = null;
|
|
|
|
- try {
|
|
|
|
- //过滤器 允许 Token 不正确, 后面Security 会拦截处理
|
|
|
|
- jwtUserPojo = jwtManager.decodeJwt(token);
|
|
|
|
- } catch ( EasException e) {
|
|
|
|
- logger.debug(e.getMessage());
|
|
|
|
- }
|
|
|
|
- if ( Objects.nonNull(jwtUserPojo) ) {
|
|
|
|
- //判断token是否有效
|
|
|
|
- UserDetailPojo userDetailPojo = null;
|
|
|
|
- try {
|
|
|
|
- userDetailPojo = loginRedisService.checkToken(jwtUserPojo);
|
|
|
|
- } catch ( EasException e) {
|
|
|
|
- logger.debug(e.getMessage());
|
|
|
|
- }
|
|
|
|
|
|
+ try {
|
|
|
|
+ //判断token是否存在
|
|
|
|
+ if (StringUtils.hasText(token)) {
|
|
|
|
+ //解析token成JwtUserDto
|
|
|
|
+ JwtUserPojo jwtUserPojo = jwtManager.decodeJwt(token);
|
|
|
|
+
|
|
|
|
+ if (Objects.nonNull(jwtUserPojo)) {
|
|
|
|
+ //判断token是否有效
|
|
|
|
+ UserDetailPojo userDetailPojo = loginRedisService.checkToken(jwtUserPojo);
|
|
|
|
|
|
- // 如果获取到了有效的用户对象
|
|
|
|
- if (Objects.nonNull(userDetailPojo)) {
|
|
|
|
- // 获取当前的 SecurityContext 对象,用于保存当前用户的安全上下文信息
|
|
|
|
- SecurityContext context = SecurityContextHolder.getContext();
|
|
|
|
|
|
+ // 如果获取到了有效的用户对象
|
|
|
|
+ if (Objects.nonNull(userDetailPojo)) {
|
|
|
|
+ // 获取当前的 SecurityContext 对象,用于保存当前用户的安全上下文信息
|
|
|
|
+ SecurityContext context = SecurityContextHolder.getContext();
|
|
|
|
|
|
- // 获取用户的权限列表
|
|
|
|
- List<String> permissionPojo = userDetailPojo.getPermissionPojos();
|
|
|
|
|
|
+ // 获取用户的权限列表
|
|
|
|
+ List<String> permissionPojo = userDetailPojo.getPermissionPojos();
|
|
|
|
|
|
- // 创建一个 ArrayList 集合,用于存储用户权限对应的 SimpleGrantedAuthority 权限对象
|
|
|
|
- ArrayList<SimpleGrantedAuthority> objects = new ArrayList<>();
|
|
|
|
|
|
+ // 创建一个 ArrayList 集合,用于存储用户权限对应的 SimpleGrantedAuthority 权限对象
|
|
|
|
+ ArrayList<SimpleGrantedAuthority> objects = new ArrayList<>();
|
|
|
|
|
|
- // 遍历用户的权限列表
|
|
|
|
- if ( Objects.nonNull(permissionPojo) ) {
|
|
|
|
- for (String userPermission : permissionPojo) {
|
|
|
|
- // 创建一个 SimpleGrantedAuthority 权限对象,并添加到集合中
|
|
|
|
- SimpleGrantedAuthority authority = new SimpleGrantedAuthority(userPermission);
|
|
|
|
- objects.add(authority);
|
|
|
|
|
|
+ // 遍历用户的权限列表
|
|
|
|
+ if (Objects.nonNull(permissionPojo)) {
|
|
|
|
+ for (String userPermission : permissionPojo) {
|
|
|
|
+ // 创建一个 SimpleGrantedAuthority 权限对象,并添加到集合中
|
|
|
|
+ SimpleGrantedAuthority authority = new SimpleGrantedAuthority(userPermission);
|
|
|
|
+ objects.add(authority);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
|
|
- // 使用用户的用户名、空凭证参数和权限对象集合创建一个 UsernamePasswordAuthenticationToken 身份验证令牌
|
|
|
|
- UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userDetailPojo, null, objects);
|
|
|
|
|
|
+ // 使用用户的用户名、空凭证参数和权限对象集合创建一个 UsernamePasswordAuthenticationToken 身份验证令牌
|
|
|
|
+ UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userDetailPojo, null, objects);
|
|
|
|
|
|
- // 将身份验证令牌设置到当前的 SecurityContext 中
|
|
|
|
- context.setAuthentication(authenticationToken);
|
|
|
|
|
|
+ // 将身份验证令牌设置到当前的 SecurityContext 中
|
|
|
|
+ context.setAuthentication(authenticationToken);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ } catch (EasException e) {
|
|
|
|
+ ResponseManager.printResponse(response, e.getMessage(), e.getCode());
|
|
|
|
+ return;
|
|
}
|
|
}
|
|
filterChain.doFilter(request, response);
|
|
filterChain.doFilter(request, response);
|
|
}
|
|
}
|