package com.qianwen.smartman.modules.auth.endpoint; import com.github.xiaoymin.knife4j.annotations.ApiSort; import com.wf.captcha.SpecCaptcha; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import java.io.IOException; import java.time.Duration; import java.util.UUID; import org.smartboot.license.client.LicenseException; import com.qianwen.smartman.common.cache.CacheNames; import com.qianwen.smartman.common.constant.CommonConstant; import com.qianwen.smartman.common.constant.ExtCacheConstant; import com.qianwen.smartman.common.constant.FmsConstant; import com.qianwen.smartman.common.utils.LicenseUtil; import com.qianwen.smartman.common.utils.MessageUtils; import com.qianwen.core.cache.utils.CacheUtil; import com.qianwen.core.jwt.JwtUtil; import com.qianwen.core.jwt.props.JwtProperties; import com.qianwen.core.log.annotation.ApiLog; import com.qianwen.core.redis.cache.BladeRedis; import com.qianwen.core.secure.BladeUser; import com.qianwen.core.secure.utils.AuthUtil; import com.qianwen.core.tenant.annotation.NonDS; import com.qianwen.core.tool.api.R; import com.qianwen.core.tool.support.Kv; import com.qianwen.core.tool.utils.Func; import com.qianwen.core.tool.utils.WebUtil; import com.qianwen.smartman.modules.auth.granter.PasswordTokenGranter; import com.qianwen.smartman.modules.auth.granter.RefreshTokenGranter; import com.qianwen.smartman.modules.auth.provider.ITokenGranter; import com.qianwen.smartman.modules.auth.provider.TokenGranterBuilder; import com.qianwen.smartman.modules.auth.provider.TokenParameter; import com.qianwen.smartman.modules.auth.utils.TokenUtil; import com.qianwen.smartman.modules.sync.constant.QyWechatConstant; import com.qianwen.smartman.modules.system.entity.UserInfo; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; @RequestMapping({"blade-auth"}) @Api(value = "用户授权认证", tags = {"授权接口"}) @RestController @NonDS @ApiSort(1) /* loaded from: blade-api.jar:BOOT-INF/classes/org/springblade/modules/auth/endpoint/BladeTokenEndPoint.class */ public class BladeTokenEndPoint { private final BladeRedis bladeRedis; private final JwtProperties jwtProperties; public BladeTokenEndPoint(final BladeRedis bladeRedis, final JwtProperties jwtProperties) { this.bladeRedis = bladeRedis; this.jwtProperties = jwtProperties; } @PostMapping({"/oauth/token"}) @ApiLog("登录用户验证") @ApiOperation(value = "获取认证令牌", notes = "传入租户ID:tenantId,账号:account,密码:password") public Kv token(@RequestParam @ApiParam(value = "租户ID", required = true) String tenantId, @RequestParam(required = false) @ApiParam(value = "账号", required = true) String username, @RequestParam(required = false) @ApiParam(value = "密码", required = true) String password) { Kv authInfo = Kv.create(); String grantType = WebUtil.getRequest().getParameter("grant_type"); String refreshToken = WebUtil.getRequest().getParameter(RefreshTokenGranter.GRANT_TYPE); String userType = Func.toStr(WebUtil.getRequest().getHeader(TokenUtil.USER_TYPE_HEADER_KEY), TokenUtil.DEFAULT_USER_TYPE); TokenParameter tokenParameter = new TokenParameter(); tokenParameter.getArgs().set("tenantId", tenantId).set("username", username).set(PasswordTokenGranter.GRANT_TYPE, password).set("grantType", grantType).set("refreshToken", refreshToken).set("userType", userType); ITokenGranter granter = TokenGranterBuilder.getGranter(grantType); UserInfo userInfo = granter.grant(tokenParameter); if (null == userInfo || null == userInfo.getUser()) { return authInfo.set("error_code", 400).set("error_description", MessageUtils.message(TokenUtil.USER_NOT_FOUND, new Object[0])); } int errorCode = 200; String message = ""; if (1 != userInfo.getUser().getStatus().intValue()) { errorCode = 400; message = MessageUtils.message(TokenUtil.USER_BLOCKED, new Object[0]); } if (200 != errorCode) { return authInfo.set("error_code", Integer.valueOf(errorCode)).set("error_description", message); } return TokenUtil.createAuthInfo(userInfo); } @GetMapping({"/oauth/logout"}) @ApiLog("退出登录") @ApiOperation("退出登录") public Kv logout() { BladeUser user = AuthUtil.getUser(); if (user != null && this.jwtProperties.getState().booleanValue()) { String token = JwtUtil.getToken(WebUtil.getRequest().getHeader("Blade-Auth")); JwtUtil.removeAccessToken(user.getTenantId(), String.valueOf(user.getUserId()), token); } return Kv.create().set(QyWechatConstant.CALLBACK_RESULT, FmsConstant.AUTOMATIC).set(CommonConstant.ALARM_MSG, QyWechatConstant.CALLBACK_RESULT); } @GetMapping({"/oauth/captcha"}) @ApiOperation("获取验证码") public Kv captcha() { SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5); String verCode = specCaptcha.text().toLowerCase(); String key = UUID.randomUUID().toString(); this.bladeRedis.setEx(CacheNames.CAPTCHA_KEY + key, verCode, Duration.ofMinutes(30L)); return Kv.create().set("key", key).set("image", specCaptcha.toBase64()); } @PostMapping({"/oauth/license-upload"}) @ApiOperation("过期上传系统密钥文件") public R licenseUpload(@RequestParam MultipartFile file) throws IOException { String encrypt = new String(file.getBytes()); try { LicenseUtil.setEncryptCode("000000", encrypt); return R.success(""); } catch (LicenseException e) { return R.fail(MessageUtils.message("system.license.expired", new Object[0])); } } @GetMapping({"/oauth/clear-cache"}) @ApiLog("清除缓存") @ApiOperation("清除缓存") public Kv clearCache() { CacheUtil.clear("blade:user", ExtCacheConstant.TENANT_MODE); CacheUtil.clear("blade:dict", ExtCacheConstant.TENANT_MODE); CacheUtil.clear("blade:sys", ExtCacheConstant.TENANT_MODE); CacheUtil.clear("blade:param", ExtCacheConstant.TENANT_MODE); CacheUtil.clear("blade:resource", ExtCacheConstant.TENANT_MODE); CacheUtil.clear("blade:menu", ExtCacheConstant.TENANT_MODE); return Kv.create().set(QyWechatConstant.CALLBACK_RESULT, FmsConstant.AUTOMATIC).set(CommonConstant.ALARM_MSG, QyWechatConstant.CALLBACK_RESULT); } }