Bladeren bron

只有一个超级管理员角色

wuweihao 5 jaren geleden
bovenliggende
commit
09c906bd0c

+ 4 - 4
xiaoan-common/src/main/java/com/xiaoan/common/util/PasswordUtils.java

@@ -168,15 +168,15 @@ public class PasswordUtils {
 
     public static void main(String[] args) {
         // 13138102395 13112311178 Aa11111111
-        String userName = "13112311178";
-        String password = "Aa11111111";
+        String userName = "owen";
+        String password = "12345678";
 
         try {
             byte[] salt = PasswordUtils.getStaticSalt();
             String ciphertext = PasswordUtils.encrypt(userName, password, salt);
             System.out.println("用户密文密码: " + ciphertext);
-//            String plaintext = PasswordUtils.decrypt(ciphertext, userName, salt);
-            String plaintext = PasswordUtils.decrypt("970e6cd78216ca8c", "owen", salt);
+            String plaintext = PasswordUtils.decrypt(ciphertext, userName, salt);
+//            String plaintext = PasswordUtils.decrypt("970e6cd78216ca8c", "owen", salt);
             System.out.println("用户明文密码:" + plaintext);
         } catch (Exception e) {
             e.printStackTrace();

+ 2 - 1
xiaoan-domain/src/main/java/com/xiaoan/domain/backend/ResourceEntity.java

@@ -47,7 +47,8 @@ public class ResourceEntity extends BaseModel implements Serializable {
     private Long parentId;
 
     /** 封装权限字段,不需要映射到数据库*/
-    private boolean authority;
+    @Transient
+    private Boolean authority ;
 
 
 }

+ 0 - 2
xiaoan-domain/src/main/java/com/xiaoan/domain/dto/request/RoleRequest.java

@@ -1,9 +1,7 @@
 package com.xiaoan.domain.dto.request;
 
-import com.xiaoan.common.model.PageDto;
 import lombok.Data;
 
-import javax.validation.constraints.NotNull;
 import java.util.Set;
 
 /**

+ 2 - 2
xiaoan-domain/src/main/java/com/xiaoan/domain/dto/request/UserRequest.java

@@ -20,8 +20,8 @@ public class UserRequest{
     /**
      * 用户代号
      */
-    @Pattern(regexp = "^[0-9]*$")
-    @NotNull
+//    @Pattern(regexp = "^[0-9]*$")
+//    @NotNull
     private String userNum;
 
     private int sex; // 0:男  1:女

+ 1 - 1
xiaoan-service/src/main/java/com/xiaoan/service/backend/dto/ResourceTree.java

@@ -45,7 +45,7 @@ public class ResourceTree {
     private int level;
 
     // 默认都没有权限
-    private boolean authority = false;
+    private boolean authority ;
 
 
 }

+ 32 - 4
xiaoan-service/src/main/java/com/xiaoan/service/backend/impl/ResourceServiceImpl.java

@@ -163,24 +163,51 @@ public class ResourceServiceImpl extends BaseServiceImpl<ResourceEntity, Long> i
      *
      * 会把所有权限都会列出来的,通过Authority判断是否有权限
      */
+//    @Override
+//    public List<ResourceTree> getResourcesAllByRolePermission(Long roleId) {
+//        // 资源Mapper
+//        HashMap<Long, ResourceEntity> allResourceMapper = this.getAllResourceMapper();
+//
+//        // 设置用户权限
+//        List<ResourceEntity> resourcePer = findResourceByRoleId(roleId);
+//        for (ResourceEntity i: resourcePer) {
+//            Long id = i.getId();
+//            ResourceEntity resourceEntity = allResourceMapper.get(id);
+//            resourceEntity.setAuthority(true);
+//            allResourceMapper.put(id, resourceEntity);
+//        }
+//
+//        // 获取用户菜单
+//        List<ResourceEntity> permission = new ArrayList<>();
+//
+//        for (Map.Entry<Long, ResourceEntity> entry : allResourceMapper.entrySet()) {
+//            permission.add(entry.getValue());
+//        }
+//
+//        ResourceTreeUtil tree = new ResourceTreeUtil(permission);
+//        return tree.buildTree();
+//
+//    }
+
+
     @Override
     public List<ResourceTree> getResourcesAllByRolePermission(Long roleId) {
         // 资源Mapper
-        HashMap<Long, ResourceEntity> allResourceMapper = getAllResourceMapper();
+        HashMap<Long, ResourceEntity> resourceAllMapper = this.getAllResourceMapper();
 
         // 设置用户权限
         List<ResourceEntity> resourcePer = findResourceByRoleId(roleId);
         for (ResourceEntity i: resourcePer) {
             Long id = i.getId();
-            ResourceEntity resourceEntity = allResourceMapper.get(id);
+            ResourceEntity resourceEntity = resourceAllMapper.get(id);
             resourceEntity.setAuthority(true);
-            allResourceMapper.put(id, resourceEntity);
+            resourceAllMapper.put(id, resourceEntity);
         }
 
         // 获取用户菜单
         List<ResourceEntity> permission = new ArrayList<>();
 
-        for (Map.Entry<Long, ResourceEntity> entry : allResourceMapper.entrySet()) {
+        for (Map.Entry<Long, ResourceEntity> entry : resourceAllMapper.entrySet()) {
             permission.add(entry.getValue());
         }
 
@@ -212,6 +239,7 @@ public class ResourceServiceImpl extends BaseServiceImpl<ResourceEntity, Long> i
         // 资源Mapper
         HashMap<Long, ResourceEntity> resourceMapper = new HashMap<>();
         for (ResourceEntity i : resourceEntityAll) {
+            i.setAuthority(false);
             resourceMapper.put(i.getId(), i);
         }
         return resourceMapper;

+ 1 - 1
xiaoan-service/src/main/java/com/xiaoan/service/backend/util/ResourceTreeUtil.java

@@ -23,7 +23,7 @@ public class ResourceTreeUtil {
             treeGrid.setResourceKey(n.getResourceKey());
             treeGrid.setResourceType(n.getResourceType());
 
-            treeGrid.setAuthority(n.isAuthority());
+            treeGrid.setAuthority(n.getAuthority());
             if (n.getParentId() != null) {
                 treeGrid.setParentId(n.getParentId());
             }

+ 1 - 1
xiaoan-web/src/main/java/com/xiaoan/web/backend/ExceptionController.java

@@ -20,7 +20,7 @@ import javax.servlet.http.HttpServletRequest;
  * 这里只可以捕获controller层的异常。
  */
 @Log4j2
-@RestControllerAdvice
+//@RestControllerAdvice
 public class ExceptionController {
 
     // 捕捉shiro的异常

+ 5 - 6
xiaoan-web/src/main/java/com/xiaoan/web/backend/IndexController.java

@@ -131,7 +131,6 @@ public class IndexController {
             return new ResultJson(MsgCode.e_ADMIN_4002, MsgCode.msg_ADMIN_4002);
         }
 
-        log.info("33333333");
         // 检查账号是否启用
         if (userEntity.getStatus() != 0) {
             log.error("账号已停用: {}", userEntity.getUserName());
@@ -139,11 +138,11 @@ public class IndexController {
         }
 
         List<ResourceTree> resourcesTreeByUserPermission = resourceService.getResourcesAllByUserPermission(userEntity);
-        log.info("获取权限 success");
+//        log.info("获取权限 success");
 
         // 获取用户角色
         List<String> roles = userService.findRoleByUserId(userEntity.getId());
-        log.info("获取角色 success");
+//        log.info("获取角色 success");
 
 
         // 创建新token
@@ -160,12 +159,12 @@ public class IndexController {
         result.put("token", token);
         result.put("permission", resourcesTreeByUserPermission);
         result.put("role", roles);
-        log.info("token create");
+//        log.info("token create");
 
 
         // 更新到 redis, 有效期24h, 旧token无效
         redisTemplate.opsForValue().set(userEntity.getUserName(), token, Long.parseLong("23"), TimeUnit.HOURS);
-        log.info("set redis success");
+//        log.info("set redis success");
 
         // 更新登录次数
         Integer loginCount = userEntity.getViewCount();
@@ -174,7 +173,7 @@ public class IndexController {
         userEntity.setUpdateTime(new Date());
 
         userService.update(userEntity);
-        log.info("update success");
+//        log.info("update success");
 
         return new ResultJson(MsgCode.SUCCESS_CODE, result);
 

+ 2 - 0
xiaoan-web/src/main/java/com/xiaoan/web/backend/ResourceController.java

@@ -15,6 +15,7 @@ import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Page;
 import org.springframework.web.bind.annotation.*;
+import springfox.documentation.annotations.ApiIgnore;
 
 import java.util.Date;
 import java.util.List;
@@ -25,6 +26,7 @@ import java.util.List;
  *
  * 这一块的数据添加应该给开发者用,而不是给管理人员用
  */
+@ApiIgnore
 @Api(tags = "后台资源管理", value = "ResourceController")
 @RestController
 @RequestMapping("api/manage/resource")

+ 27 - 3
xiaoan-web/src/main/java/com/xiaoan/web/backend/RoleController.java

@@ -2,6 +2,7 @@ package com.xiaoan.web.backend;
 
 import com.github.pagehelper.PageInfo;
 import com.xiaoan.common.constant.MsgCode;
+import com.xiaoan.common.exception.BaseRuntimeException;
 import com.xiaoan.common.model.PageDto;
 import com.xiaoan.common.util.ResultJson;
 import com.xiaoan.dao.backend.RoleRepository;
@@ -32,6 +33,7 @@ import javax.validation.Valid;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
+import java.util.Set;
 
 
 /**
@@ -53,6 +55,10 @@ public class RoleController extends BaseController {
     private ResourceService resourceService;
 
 //    @RequiresPermissions(logical = Logical.AND, value = {"view", "edit"})
+
+    /**
+     * 超级管理员角色只有一个
+     */
     @RequiresPermissions("admin:role:list")
     @WebControllerLog(description = "角色权限-查询列表")
     @ApiOperation("分页获取角色列表/搜索")
@@ -61,15 +67,20 @@ public class RoleController extends BaseController {
         Condition condition = new Condition(RoleEntity.class);
         // 参数名是属性
         condition.and().andLike("roleName", "%" + param.getSearchKey()+ "%");
+        condition.and().andEqualTo("roleKey", "normal");
         PageInfo<RoleEntity> pageInfo = roleService.findAll(condition, param.getPageNum(), param.getPageSize());
         return new ResultJson(MsgCode.SUCCESS_CODE, pageInfo);
     }
 
-
+    /**
+     * 超级管理员角色只有一个
+     */
     @ApiOperation("获取角色")
     @GetMapping("find")
     public ResultJson find(){
-        List<RoleEntity> result = roleService.findAll(new Condition(RoleEntity.class), "sort desc");
+        Condition condition = new Condition(RoleEntity.class);
+        condition.and().andEqualTo("roleKey", "normal");
+        List<RoleEntity> result = roleService.findAll(condition, "sort desc");
         return new ResultJson(MsgCode.SUCCESS_CODE, result);
     }
 
@@ -90,7 +101,6 @@ public class RoleController extends BaseController {
 
         HashMap<Object, Object> resultMap = new HashMap<>();
 
-//        List<ResourceTree> permission = resourceService.getResourcesTreeByRolePermission(id);
         List<ResourceTree> permission = resourceService.getResourcesAllByRolePermission(id);
 
         resultMap.put("role", roleEntity);
@@ -174,6 +184,10 @@ public class RoleController extends BaseController {
     @ApiOperation("删除角色")
     @GetMapping("delete/{id}")
     public ResultJson delete(@PathVariable Long id){
+        RoleEntity roleEntity = roleService.findById(id);
+        if ("admin".equals(roleEntity.getRoleKey())) {
+            throw new BaseRuntimeException(MsgCode.e_ADMIN_4101, "管理员账户不能删除");
+        }
         roleService.deleteById(id);
         return new ResultJson(MsgCode.SUCCESS_CODE, MsgCode.msg_SUCCESS);
     }
@@ -183,6 +197,16 @@ public class RoleController extends BaseController {
     @ApiOperation("多角色删除")
     @GetMapping("delete/all/{ids}")
     public ResultJson delete(@PathVariable String ids){
+
+        // 管理员账户不能删除
+        String[] split = ids.split(",");
+        for (String s : split) {
+            RoleEntity roleEntity = roleService.findById(Long.valueOf(s));
+            if ("admin".equals(roleEntity.getRoleKey())) {
+                throw new BaseRuntimeException(MsgCode.e_ADMIN_4101, "管理员账户不能删除");
+            }
+        }
+
         roleService.deleteByIds(ids);
         return new ResultJson(MsgCode.SUCCESS_CODE, MsgCode.msg_SUCCESS);
     }

+ 5 - 3
xiaoan-web/src/main/java/com/xiaoan/web/backend/UserController.java

@@ -28,6 +28,8 @@ import java.util.*;
 
 /**
  * Created by owen on 2020/2/18 0018 12:17
+ *
+ * 超级管理员账户,需要手动添加
  */
 @Log4j2
 @Api(tags = "后台用户管理", value = "UserController")
@@ -90,7 +92,7 @@ public class UserController extends BaseController {
             entity.setUserNum(byMaxNum.toString());
 
 
-            entity.setPassword(PasswordUtils.encrypt(param.getUserName(), "123456", PasswordUtils.getStaticSalt()));
+            entity.setPassword(PasswordUtils.encrypt(param.getUserName(), "12345678", PasswordUtils.getStaticSalt()));
 
             n = userService.save(entity);
         } else {
@@ -184,7 +186,7 @@ public class UserController extends BaseController {
     @GetMapping("resetPass/{id}")
     public ResultJson resetPass(@PathVariable Long id){
         UserEntity userEntity = userService.findById(id);
-        userEntity.setPassword(PasswordUtils.encrypt(userEntity.getUserName(), "123456", PasswordUtils.getStaticSalt()));
+        userEntity.setPassword(PasswordUtils.encrypt(userEntity.getUserName(), "12345678", PasswordUtils.getStaticSalt()));
         userService.update(userEntity);
         return new ResultJson(MsgCode.SUCCESS_CODE, MsgCode.msg_SUCCESS);
     }
@@ -198,7 +200,7 @@ public class UserController extends BaseController {
         if (split.length > 0) {
             for (String s : split) {
                 UserEntity userEntity = userService.findById(Long.valueOf(s));
-                userEntity.setPassword(PasswordUtils.encrypt(userEntity.getUserName(), "123456", PasswordUtils.getStaticSalt()));
+                userEntity.setPassword(PasswordUtils.encrypt(userEntity.getUserName(), "12345678", PasswordUtils.getStaticSalt()));
                 userService.update(userEntity);
             }
         }