|
@@ -6,6 +6,8 @@ import io.swagger.annotations.ApiOperation;
|
|
|
import io.swagger.annotations.ApiParam;
|
|
|
import net.chenlin.dp.common.constant.RestApiConstant;
|
|
|
import net.chenlin.dp.common.entity.R;
|
|
|
+import net.chenlin.dp.common.utils.CommonUtils;
|
|
|
+import net.chenlin.dp.common.utils.JwtUtils;
|
|
|
import net.chenlin.dp.common.utils.MD5Utils;
|
|
|
import net.chenlin.dp.common.utils.TokenUtils;
|
|
|
import net.chenlin.dp.modules.sys.controller.AbstractController;
|
|
@@ -30,13 +32,16 @@ public class RestAuthController extends AbstractController {
|
|
|
@Autowired
|
|
|
private SysUserService sysUserService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private JwtUtils jwtUtils;
|
|
|
+
|
|
|
/**
|
|
|
* 登录授权校验
|
|
|
* @return
|
|
|
*/
|
|
|
@ApiOperation(value = "登录")
|
|
|
@ApiImplicitParam(name = "token", value = "授权码")
|
|
|
- @RequestMapping(value = RestApiConstant.AUTH_REQUEST, method = RequestMethod.POST)
|
|
|
+ @RequestMapping(value = RestApiConstant.AUTH_REQUEST, method = {RequestMethod.GET, RequestMethod.POST})
|
|
|
public R auth(@ApiParam(name = "username", value = "用户名") @RequestParam String username,
|
|
|
@ApiParam(name = "password", value = "密码") @RequestParam String password) {
|
|
|
// 用户名为空
|
|
@@ -62,8 +67,10 @@ public class RestAuthController extends AbstractController {
|
|
|
return RestApiConstant.TokenErrorEnum.USER_DISABLE.getResp();
|
|
|
}
|
|
|
// 保存或者更新token
|
|
|
- String token = TokenUtils.generateValue();
|
|
|
- int count = sysUserService.saveOrUpdateToken(sysUserEntity.getUserId(), token);
|
|
|
+ String randomKey = TokenUtils.generateValue();
|
|
|
+ String token = jwtUtils.generateToken(sysUserEntity.getUsername(),
|
|
|
+ String.valueOf(sysUserEntity.getUserId()), randomKey);
|
|
|
+ int count = sysUserService.saveOrUpdateToken(sysUserEntity.getUserId(), randomKey);
|
|
|
if (count > 0) {
|
|
|
R success = RestApiConstant.TokenErrorEnum.TOKEN_ENABLE.getResp();
|
|
|
success.put(RestApiConstant.AUTH_TOKEN, token);
|
|
@@ -77,30 +84,48 @@ public class RestAuthController extends AbstractController {
|
|
|
* @return
|
|
|
*/
|
|
|
@ApiOperation(value = "校验token是否可用")
|
|
|
- @RequestMapping(value = RestApiConstant.AUTH_CHECK, method = RequestMethod.POST)
|
|
|
+ @RequestMapping(value = RestApiConstant.AUTH_CHECK, method = {RequestMethod.GET, RequestMethod.POST})
|
|
|
public R authStatus(@ApiParam(name = "token", value = "授权码") @RequestParam String token) {
|
|
|
+
|
|
|
// token为空
|
|
|
if (StringUtils.isBlank(token.trim())) {
|
|
|
return RestApiConstant.TokenErrorEnum.TOKEN_NOT_FOUND.getResp();
|
|
|
}
|
|
|
- SysUserTokenEntity sysUserTokenEntity = sysUserService.getUserTokenByToken(token);
|
|
|
+
|
|
|
+ // jwt过期时间校验
|
|
|
+ if (jwtUtils.isExpred(token)) {
|
|
|
+ return RestApiConstant.TokenErrorEnum.TOKEN_EXPIRED.getResp();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据md5混淆字符串查询用户token
|
|
|
+ SysUserTokenEntity sysUserTokenEntity = sysUserService.getUserTokenByToken(jwtUtils.getMd5Key(token));
|
|
|
+
|
|
|
// 无效的token:token不存在
|
|
|
if (sysUserTokenEntity == null) {
|
|
|
return RestApiConstant.TokenErrorEnum.TOKEN_INVALID.getResp();
|
|
|
}
|
|
|
+
|
|
|
+ // token中的userId和数据库中userId是否一致
|
|
|
+ if (sysUserTokenEntity.getUserId() != Long.parseLong(jwtUtils.getUserId(token))) {
|
|
|
+ return RestApiConstant.TokenErrorEnum.TOKEN_INVALID.getResp();
|
|
|
+ }
|
|
|
+
|
|
|
// 无效token:用户不存在
|
|
|
SysUserEntity sysUserEntity = sysUserService.getUserByIdForToken(sysUserTokenEntity.getUserId());
|
|
|
if (sysUserEntity == null) {
|
|
|
return RestApiConstant.TokenErrorEnum.TOKEN_INVALID.getResp();
|
|
|
}
|
|
|
- // token过期
|
|
|
+
|
|
|
+ // token过期:采用服务端时间校验
|
|
|
if (TokenUtils.isExpired(sysUserTokenEntity.getGmtExpire())) {
|
|
|
return RestApiConstant.TokenErrorEnum.TOKEN_EXPIRED.getResp();
|
|
|
}
|
|
|
+
|
|
|
// 用户是否禁用
|
|
|
if (sysUserEntity.getStatus() == 0) {
|
|
|
return RestApiConstant.TokenErrorEnum.USER_DISABLE.getResp();
|
|
|
}
|
|
|
+
|
|
|
return RestApiConstant.TokenErrorEnum.TOKEN_ENABLE.getResp();
|
|
|
}
|
|
|
|