New file |
| | |
| | | package com.ruoyi.chargingPile.filter; |
| | | |
| | | import com.ruoyi.account.api.feignClient.AppUserClient; |
| | | import com.ruoyi.account.api.model.TAppUser; |
| | | import com.ruoyi.common.core.constant.HttpStatus; |
| | | import com.ruoyi.common.core.constant.TokenConstants; |
| | | import com.ruoyi.common.core.utils.JwtUtils; |
| | | import com.ruoyi.common.core.utils.ServletUtils; |
| | | import com.ruoyi.common.core.utils.StringUtils; |
| | | import com.ruoyi.system.api.domain.SysUser; |
| | | import com.ruoyi.system.api.feignClient.SysUserClient; |
| | | import io.jsonwebtoken.Claims; |
| | | import org.apache.logging.log4j.core.config.Order; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.context.annotation.Lazy; |
| | | import org.springframework.http.server.reactive.ServerHttpRequest; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.web.server.ServerWebExchange; |
| | | import org.springframework.web.server.WebFilter; |
| | | import org.springframework.web.server.WebFilterChain; |
| | | import reactor.core.publisher.Mono; |
| | | |
| | | import javax.annotation.Resource; |
| | | |
| | | /** |
| | | * @author zhibing.pu |
| | | * @Date 2024/8/23 11:22 |
| | | */ |
| | | @Order(-200) |
| | | @Component |
| | | public class AuthFilter implements WebFilter { |
| | | private static final Logger log = LoggerFactory.getLogger(AuthFilter.class); |
| | | |
| | | @Lazy |
| | | @Resource |
| | | private AppUserClient appUserClient; |
| | | |
| | | @Lazy |
| | | @Resource |
| | | private SysUserClient sysUserClient; |
| | | |
| | | |
| | | @Override |
| | | public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { |
| | | ServerHttpRequest request = exchange.getRequest(); |
| | | String token = getToken(request); |
| | | Claims claims = JwtUtils.parseToken(token); |
| | | String userid = JwtUtils.getUserId(claims); |
| | | String userType = JwtUtils.getUserType(claims); |
| | | //管理后台用户 |
| | | if ("system".equals(userType)) { |
| | | SysUser sysUser = sysUserClient.getSysUser(Long.valueOf(userid)).getData(); |
| | | if(null == sysUser || "2".equals(sysUser.getDelFlag())){ |
| | | log.error("[账户异常处理]请求账户id:{}", userid); |
| | | return unauthorizedResponse(exchange,"无效的账户"); |
| | | } |
| | | if("1".equals(sysUser.getStatus())){ |
| | | log.error("[账户异常处理]请求账户id:{}", userid); |
| | | return unauthorizedResponse(exchange,"账户已被停用,请联系系统管理员!"); |
| | | } |
| | | } |
| | | //小程序用户 |
| | | if ("applet".equals(userType)) { |
| | | TAppUser appUser = appUserClient.getUserById(Long.valueOf(userid)).getData(); |
| | | if(null == appUser || appUser.getDelFlag() || 3 == appUser.getStatus()){ |
| | | log.error("[账户异常处理]请求账户id:{}", userid); |
| | | return unauthorizedResponse(exchange,"无效的账户"); |
| | | } |
| | | if(2 == appUser.getStatus()){ |
| | | log.error("[账户异常处理]请求账户id:{}", userid); |
| | | return unauthorizedResponse(exchange,"账户已被冻结,请联系系统管理员!"); |
| | | } |
| | | } |
| | | return chain.filter(exchange); |
| | | } |
| | | |
| | | |
| | | private Mono<Void> unauthorizedResponse(ServerWebExchange exchange, String msg) { |
| | | return ServletUtils.webFluxResponseWriter(exchange.getResponse(), msg, HttpStatus.UNAUTHORIZED); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 获取请求token |
| | | */ |
| | | private String getToken(ServerHttpRequest request) { |
| | | String token = request.getHeaders().getFirst(TokenConstants.AUTHENTICATION); |
| | | // 如果前端设置了令牌前缀,则裁剪掉前缀 |
| | | if (StringUtils.isNotEmpty(token) && token.startsWith(TokenConstants.PREFIX)) { |
| | | token = token.replaceFirst(TokenConstants.PREFIX, StringUtils.EMPTY); |
| | | } |
| | | return token; |
| | | } |
| | | } |