智慧农业后台管理
guoshilong
2022-11-08 72b971c98c46531064d74b68fc8fe8864ed0b544
src/main/java/org/springblade/modules/auth/endpoint/BladeTokenEndPoint.java
@@ -18,6 +18,7 @@
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.wf.captcha.SpecCaptcha;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
@@ -29,12 +30,15 @@
import org.springblade.core.launch.constant.AppConstant;
import org.springblade.core.launch.constant.TokenConstant;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.core.tool.utils.WebUtil;
import org.springblade.modules.auth.provider.ITokenGranter;
import org.springblade.modules.auth.provider.TokenGranterBuilder;
@@ -65,6 +69,13 @@
   private final BladeRedis bladeRedis;
   private final JwtProperties jwtProperties;
   @ApiLog("根据token获取登录用户")
   @GetMapping("/getuser/token")
   public R<Object> getUserLoginInfoByToken() {
      String token = JwtUtil.getToken(WebUtil.getRequest().getHeader(TokenConstant.HEADER));
      return R.data(bladeRedis.get(token));
   }
   @ApiLog("登录用户验证")
   @PostMapping("/oauth/token")
@@ -101,7 +112,10 @@
      if (Func.isEmpty(userInfo.getRoles())) {
         return authInfo.set("error_code", HttpServletResponse.SC_BAD_REQUEST).set("error_description", "未获得用户的角色信息");
      }
      Kv authInfo1 = TokenUtil.createAuthInfo(userInfo);
      String access_token = authInfo1.get("access_token").toString();
      // 存入redis并设置过期时间为60分钟
      bladeRedis.setEx(access_token, authInfo1, Duration.ofMinutes(60));
      return TokenUtil.createAuthInfo(userInfo);
   }
@@ -110,10 +124,12 @@
   @ApiOperation(value = "退出登录")
   public Kv logout() {
      BladeUser user = AuthUtil.getUser();
      String token = JwtUtil.getToken(WebUtil.getRequest().getHeader(TokenConstant.HEADER));
      // 清空redis保存的token
      if (user != null && jwtProperties.getState()) {
         String token = JwtUtil.getToken(WebUtil.getRequest().getHeader(TokenConstant.HEADER));
         JwtUtil.removeAccessToken(user.getTenantId(), String.valueOf(user.getUserId()), token);
      }
      bladeRedis.del(token);
      return Kv.create().set("success", "true").set("msg", "success");
   }
@@ -146,4 +162,16 @@
      CacheUtil.clear(PARAM_CACHE, Boolean.FALSE);
      return Kv.create().set("success", "true").set("msg", "success");
   }
   @GetMapping("/oauth/judgeRefreshToken")
   @ApiOperation(value = "token认证时效")
   public String judgeRefreshToken(String refreshToken) {
      Object o = bladeRedis.get(refreshToken);
      if (o == null) {
         return "token令牌已失效";
      } else {
         return "token令牌有效";
      }
   }
}