| | |
| | | import com.zy.asrs.wcs.common.constant.Constants; |
| | | import com.zy.asrs.wcs.sys.entity.Menu; |
| | | import com.zy.asrs.wcs.sys.entity.User; |
| | | import com.zy.asrs.wcs.sys.entity.UserLogin; |
| | | import com.zy.asrs.wcs.sys.service.UserLoginService; |
| | | import com.zy.asrs.wcs.sys.service.UserService; |
| | | import com.zy.asrs.wcs.utils.HttpUtils; |
| | |
| | | @Component |
| | | public class JwtAuthenticationFilter extends OncePerRequestFilter { |
| | | |
| | | public static final ArrayList<String> WHITE_KEY = new ArrayList<String>(){ |
| | | public static final ArrayList<String> WHITE_LIST = new ArrayList<String>(){ |
| | | private static final long serialVersionUID = 1L; |
| | | { |
| | | add("xltys1995"); |
| | |
| | | if (!Cools.isEmpty(access_token)) { |
| | | try { |
| | | User user; |
| | | if (WHITE_KEY.contains(access_token)) { |
| | | user = userService.getByUsername("openapi", 2); |
| | | // 白名单 |
| | | if (WHITE_LIST.contains(access_token)) { |
| | | user = userService.getByUsername("root", 1L); |
| | | if (user == null) { |
| | | throw new UsernameNotFoundException("Username not found"); |
| | | } |
| | |
| | | // 解析token |
| | | Claims claims = JwtUtil.parseToken(access_token, configProperties.getTokenKey()); |
| | | JwtSubject jwtSubject = JwtUtil.getJwtSubject(claims); |
| | | user = userService.getByUsername(jwtSubject.getUsername(), jwtSubject.getTenantId()); |
| | | user = userService.getByUsername(jwtSubject.getUsername(), jwtSubject.getHostId()); |
| | | if (user == null) { |
| | | throw new UsernameNotFoundException("Username not found"); |
| | | } |
| | |
| | | String token = JwtUtil.buildToken(jwtSubject, configProperties.getTokenExpireTime(), |
| | | configProperties.getTokenKey()); |
| | | response.addHeader(Constants.TOKEN_HEADER_NAME, token); |
| | | userLoginService.saveAsync(user.getUsername(), LoginRecord.TYPE_REFRESH, null, |
| | | user.getTenantId(), request); |
| | | userLoginService.saveAsync(user.getId(), UserLogin.TYPE_REFRESH, null, user.getHostId(), request); |
| | | } |
| | | } |
| | | } catch (ExpiredJwtException e) { |
| | | e.printStackTrace(); |
| | | HttpUtils.responseError(response, Constants.TOKEN_EXPIRED_CODE, Constants.TOKEN_EXPIRED_MSG, |
| | | e.getMessage()); |
| | | return; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | HttpUtils.responseError(response, Constants.BAD_CREDENTIALS_CODE, Constants.BAD_CREDENTIALS_MSG, |
| | | e.toString()); |
| | | return; |