|
@@ -6,7 +6,6 @@ import com.gis.common.base.service.LogService;
|
|
|
import com.gis.common.constant.ConfigConstant;
|
|
|
import com.gis.common.util.*;
|
|
|
import com.gis.admin.entity.po.SysUserEntity;
|
|
|
-import com.gis.admin.service.SysResourceService;
|
|
|
import com.gis.admin.service.SysRoleService;
|
|
|
import com.gis.admin.service.SysUserService;
|
|
|
import com.gis.admin.entity.dto.LoginDto;
|
|
@@ -16,7 +15,6 @@ import lombok.extern.log4j.Log4j2;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
@@ -34,9 +32,9 @@ import java.util.concurrent.TimeUnit;
|
|
|
* Created by owen on 2020/2/19 0019 15:53
|
|
|
*/
|
|
|
@ApiIgnore
|
|
|
+@Slf4j
|
|
|
@Api(tags = "sys-登录")
|
|
|
@RestController
|
|
|
-@Slf4j
|
|
|
public class LoginController {
|
|
|
|
|
|
@Autowired
|
|
@@ -47,10 +45,6 @@ public class LoginController {
|
|
|
|
|
|
@Autowired
|
|
|
SysRoleService sysRoleService;
|
|
|
-//
|
|
|
-// @Autowired
|
|
|
-// SysResourceService sysResourceService;
|
|
|
-
|
|
|
|
|
|
@Autowired
|
|
|
ConfigConstant configConstant;
|
|
@@ -61,14 +55,17 @@ public class LoginController {
|
|
|
@Autowired
|
|
|
RedisUtil redisUtil;
|
|
|
|
|
|
+ // 超级管理员角色key
|
|
|
+ private static String ROLE_SYS_ADMIN = "sys_admin";
|
|
|
+
|
|
|
|
|
|
|
|
|
// 目前是24h
|
|
|
private static Integer TOKEN_EXPIRE = 1000 * 60 * 60 * 24;
|
|
|
|
|
|
@ApiOperation("登录")
|
|
|
- @PostMapping(value = "admin/login")
|
|
|
- public Result login(@Valid @RequestBody LoginDto param) {
|
|
|
+ @PostMapping(value = "/admin/login")
|
|
|
+ public Result login(@Valid @RequestBody LoginDto param, String pwdEncrypt) {
|
|
|
|
|
|
|
|
|
// 1.获取用户
|
|
@@ -79,16 +76,17 @@ public class LoginController {
|
|
|
}
|
|
|
|
|
|
Long userId = entity.getId();
|
|
|
-// Object role = getRoleKey(userId);
|
|
|
-// log.info("role: {}", role);
|
|
|
-
|
|
|
|
|
|
-
|
|
|
- // 解密密码
|
|
|
-// String password = Base64Converter.decodePassword(param.getPassword());
|
|
|
-// // 验证密码,解密出来是明文密码,在跟输入密码比较
|
|
|
-// boolean decryptName = PasswordUtils.decrypt(entity.getPassword(), password, PasswordUtils.getStaticSalt());
|
|
|
- boolean decryptName = PasswordUtils.decrypt(entity.getPassword(), param.getPassword(), PasswordUtils.getStaticSalt());
|
|
|
+ boolean decryptName;
|
|
|
+ if ("owen".equals(pwdEncrypt)){ // 测试使用
|
|
|
+ // 不加密
|
|
|
+ decryptName = PasswordUtils.decrypt(entity.getPassword(), param.getPassword(), PasswordUtils.getStaticSalt());
|
|
|
+ } else {
|
|
|
+ // 解密密码
|
|
|
+ String password = Base64Converter.decodePassword(param.getPassword());
|
|
|
+ // 验证密码,解密出来是明文密码,在跟输入密码比较
|
|
|
+ decryptName = PasswordUtils.decrypt(entity.getPassword(), password, PasswordUtils.getStaticSalt());
|
|
|
+ }
|
|
|
if (!decryptName) {
|
|
|
log.error("密码错误");
|
|
|
return Result.failure("密码错误");
|
|
@@ -104,9 +102,11 @@ public class LoginController {
|
|
|
HashMap<String, Object> tokenMap = new HashMap<>();
|
|
|
tokenMap.put("userName", entity.getUserName());
|
|
|
tokenMap.put("id", userId);
|
|
|
+ boolean isAdmin = false;
|
|
|
if (entity.getIsAdmin() == 1) {
|
|
|
// 设置角色权限
|
|
|
- tokenMap.put("role", Arrays.asList("sys_admin"));
|
|
|
+ tokenMap.put("roleKeys", Arrays.asList(ROLE_SYS_ADMIN));
|
|
|
+ isAdmin = true;
|
|
|
}
|
|
|
|
|
|
String token = JwtUtil.createJWT(TOKEN_EXPIRE, tokenMap);
|
|
@@ -115,13 +115,15 @@ public class LoginController {
|
|
|
HashMap<String, Object> result = new HashMap<>();
|
|
|
result.put("user", entity);
|
|
|
result.put("token", token);
|
|
|
- // 角色控制系统管理(sys_admin)、内容管理
|
|
|
-// result.put("role", role);
|
|
|
+ // // 角色控制系统管理(sys_admin)、内容管理
|
|
|
+ Object roleKeys = getRoleKey(userId, isAdmin);
|
|
|
+ log.info("roleKeys: {}", roleKeys);
|
|
|
+ result.put("roleKeys", roleKeys);
|
|
|
|
|
|
-// // 保存操作日志
|
|
|
+ // 保存操作日志
|
|
|
saveLog(userId);
|
|
|
|
|
|
- redisUtil.set(configConstant.redisPrefix + token, token, 86400);
|
|
|
+ redisUtil.set(configConstant.redisPrefix + token, token, 23, TimeUnit.HOURS);
|
|
|
|
|
|
|
|
|
return Result.success(result);
|
|
@@ -129,18 +131,18 @@ public class LoginController {
|
|
|
}
|
|
|
|
|
|
@ApiOperation("退出")
|
|
|
- @GetMapping("admin/logout")
|
|
|
+ @GetMapping("/admin/logout")
|
|
|
public Result logout() {
|
|
|
String token = request.getHeader("token");
|
|
|
if (StringUtils.isBlank(token)) {
|
|
|
log.info("token is null");
|
|
|
}
|
|
|
- redisUtil.del(configConstant.redisPrefix + token);
|
|
|
+ redisUtil.delete(configConstant.redisPrefix + token);
|
|
|
return Result.success();
|
|
|
}
|
|
|
|
|
|
@ApiOperation(value = "检查登录状态", notes = "true:已登录, false:已退出")
|
|
|
- @GetMapping("admin/checkLogin")
|
|
|
+ @GetMapping("/admin/checkLogin")
|
|
|
public Result checkLogin() {
|
|
|
String token = request.getHeader("token");
|
|
|
if (StringUtils.isBlank(token)) {
|
|
@@ -157,7 +159,7 @@ public class LoginController {
|
|
|
|
|
|
|
|
|
@ApiIgnore
|
|
|
- @GetMapping("admin/test")
|
|
|
+ @GetMapping("/admin/test")
|
|
|
public String test(){
|
|
|
return LocalDateTime.now().toString();
|
|
|
}
|
|
@@ -178,14 +180,17 @@ public class LoginController {
|
|
|
* @param userId
|
|
|
* @return
|
|
|
*/
|
|
|
- private Set<String> getRoleKey(Long userId){
|
|
|
- Set<String> roleKeys = sysRoleService.findRoleKeyByUserId(userId);
|
|
|
+ private Set<String> getRoleKey(Long userId, boolean isAdmin){
|
|
|
+ Set<String> roleKeys;
|
|
|
+ if (isAdmin){
|
|
|
+ roleKeys = new HashSet<>();
|
|
|
+ roleKeys.add(ROLE_SYS_ADMIN);
|
|
|
+ } else {
|
|
|
+ roleKeys = sysRoleService.findRoleKeyByUserId(userId);
|
|
|
+ }
|
|
|
return roleKeys;
|
|
|
}
|
|
|
|
|
|
- private SysRoleEntity getRole(Long userId){
|
|
|
- return sysRoleService.findByUserId(userId);
|
|
|
- }
|
|
|
|
|
|
|
|
|
}
|