这一章是对后续资源管理用的思路进行讲解,用户角色权限离不开资源这个概念。我这边的资源是通过自定义注解的形式启动自动加载。
在common模块创建 annotation目录,在目录下我创建了四个自定义注解
这四个注解都是用在controller层,这样我在项目启动的时候回去读取方法是否包含这些注解然后自动生成对应的资源。
package com.swh.common.annotation;
import java.lang.annotation.*;
/**
* @ClassName ApiCode
* @Description
* @Author songWenHao
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(ApiCodes.class)
public @interface ApiCode {
String menuName();
Class<?> menuClass();
}
package com.swh.common.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @ClassName ApiCodes
* @Description
* @Author songWenHao
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiCodes {
ApiCode[] value();
}
package com.swh.common.annotation;
import java.lang.annotation.*;
/**
* @author songWenHao
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(Menus.class)
public @interface Menu {
String menuName();
Class<?> menuClass();
}
package com.swh.common.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author songWenHao
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Menus {
Menu[] value();
}
在common目录下创建menu目录,在这个目录下我创建了三个枚举类,用来定义资源的三级别,第一个级别是大菜单级别,第二个级别是菜单下的子菜单级别,第三个级别是按钮级别,用来达到权限控制的目的。
package com.swh.common.menu;
import com.swh.common.constant.MenuLevelOne;
/**
* @ClassName LevelOneMenuEnum
* @Description
* @Author songWenHao
*/
public enum LevelOneMenuEnum implements MenuLevelOne {
/**
* 一级菜单 首页
*/
MENU_HOME("001", "首页", "/home", "en-home-default", 1),
/**
* 一级菜单 系统配置
*/
MENU_SYSTEM_CONFIG("002", "系统配置", "/system", "en-system-default", 2),
;
private final String moduleCode;
private final String moduleDesc;
private final String path;
private final String icon;
private final int sort;
LevelOneMenuEnum(String moduleCode, String moduleDesc, String path, String icon, int sort) {
this.moduleCode = moduleCode;
this.moduleDesc = moduleDesc;
this.path = path;
this.icon = icon;
this.sort = sort;
}
@Override
public String getModuleCode() {
return this.moduleCode;
}
@Override
public String getModuleDesc() {
return this.moduleDesc;
}
@Override
public String getPath() {
return this.path;
}
@Override
public String getIcon() {
return this.icon;
}
@Override
public int getSort() {
return this.sort;
}
@Override
public String getDisplayFlag() {
return "Y";
}
}
package com.swh.common.menu;
import com.swh.common.constant.MenuLevelTwo;
/**
* @ClassName LevelTwoMenuEnum
* @Description
* @Author songWenHao
*/
public enum LevelTwoMenuEnum implements MenuLevelTwo {
// === 首页 ===
// ==== 系统配置 ====
MENU_ADMIN_USER_CONFIG(LevelOneMenuEnum.MENU_SYSTEM_CONFIG, "101", "用户管理", "/system/adminUserConfig", "", 101),
// ==== 系统配置 ====
MENU_ADMIN_ROLE_CONFIG(LevelOneMenuEnum.MENU_SYSTEM_CONFIG, "102", "角色管理", "/system/adminRoleConfig", "", 102),
;
private final String moduleCode;
private final String moduleDesc;
private final String funCode;
private final String funDesc;
private final String path;
private final String icon;
private final int sort;
private final String displayFlag;
LevelTwoMenuEnum(LevelOneMenuEnum level1, String funCode, String funDesc, String path, String icon, int sort) {
this.moduleCode = level1.getModuleCode();
this.moduleDesc = level1.getModuleDesc();
this.funCode = funCode;
this.funDesc = funDesc;
this.path = path;
this.icon = icon;
this.sort = sort;
this.displayFlag = "Y";
}
LevelTwoMenuEnum(LevelOneMenuEnum level1, String funCode, String funDesc, String path, String icon, int sort, String displayFlag) {
this.moduleCode = level1.getModuleCode();
this.moduleDesc = level1.getModuleDesc();
this.funCode = funCode;
this.funDesc = funDesc;
this.path = path;
this.icon = icon;
this.sort = sort;
this.displayFlag = "N";
}
@Override
public String getModuleCode() {
return this.moduleCode;
}
@Override
public String getModuleDesc() {
return this.moduleDesc;
}
@Override
public String getFunCode() {
return this.funCode;
}
@Override
public String getFunDesc() {
return this.funDesc;
}
@Override
public String getPath() {
return this.path;
}
@Override
public String getIcon() {
return this.icon;
}
@Override
public int getSort() {
return this.sort;
}
@Override
public String getDisplayFlag() {
return this.displayFlag;
}
}
package com.swh.common.menu;
import com.swh.common.constant.MenuLevelThree;
import static com.swh.common.constant.AdminUserConst.Url.*;
import static com.swh.common.menu.LevelTwoMenuEnum.MENU_ADMIN_ROLE_CONFIG;
import static com.swh.common.menu.LevelTwoMenuEnum.MENU_ADMIN_USER_CONFIG;
/**
* @ClassName LevelThreeMenuEnum
* @Description
* @Author songWenHao
*/
public enum LevelThreeMenuEnum implements MenuLevelThree {
/*
* 三级菜单 用户管理员
*/
/**
* 分页查询
*/
MENU_ADMIN_USER_LIST_PAGE(MENU_ADMIN_USER_CONFIG, "001", "用户分页查询", ADMIN_USER_LIST_PAGE),
/**
* 新增
*/
MENU_ADMIN_USER_ADD(MENU_ADMIN_USER_CONFIG, "002", "用户新增", ADMIN_USER_ADD),
/**
* 更新
*/
MENU_ADMIN_USER_UPDATE(MENU_ADMIN_USER_CONFIG, "003", "用户修改", ADMIN_USER_UPDATE),
/**
* 批量删除
*/
MENU_ADMIN_USER_BATCH_DEL(MENU_ADMIN_USER_CONFIG, "004", "用户批量删除", ADMIN_USER_BATCH_DEL),
/**
* 状态变更
*/
MENU_ADMIN_USER_CHANGE_STATE(MENU_ADMIN_USER_CONFIG, "005", "用户状态变更", ADMIN_USER_STATE_CHANGE),
/**
* 密码修改
*/
MENU_ADMIN_USER_PASSWORD_CHANGE(MENU_ADMIN_USER_CONFIG, "006", "用户密码修改", ADMIN_USER_PASSWORD_CHANGE),
/**
* 密码重置
*/
MENU_ADMIN_USER_PASSWORD_RESET(MENU_ADMIN_USER_CONFIG, "007", "用户密码重置", ADMIN_USER_PASSWORD_RESET),
/**
* 角色 分页查询
*/
MENU_ADMIN_ROLE_LIST_PAGE(MENU_ADMIN_ROLE_CONFIG, "201", "角色分页", ADMIN_ROLE_LIST_PAGE),
/**
* 角色 新增
*/
MENU_ADMIN_ROLE_ADD(MENU_ADMIN_ROLE_CONFIG, "202", "角色新增", ADMIN_ROLE_ADD),
/**
* 角色 修改
*/
MENU_ADMIN_ROLE_UPDATE(MENU_ADMIN_ROLE_CONFIG, "203", "角色修改", ADMIN_ROLE_UPDATE),
/**
* 角色 批量删除
*/
MENU_ADMIN_ROLE_BATCH_DEL(MENU_ADMIN_ROLE_CONFIG, "204", "角色批量删除", ADMIN_ROLE_BATCH_DEL),
/**
* 角色 获取所有
*/
MENU_ADMIN_ROLE_BATCH_ALL(MENU_ADMIN_ROLE_CONFIG, "205", "角色获取所有", ADMIN_ROLE_BATCH_ALL),
/**
* 资源树的获取
*/
MENU_ADMIN_RES_ALL(MENU_ADMIN_ROLE_CONFIG, "301", "资源树的获取", ADMIN_RES_GET_ALL),
/**
* 菜单树
*/
MENU_ADMIN_MENU_ALL(MENU_ADMIN_ROLE_CONFIG, "302", "菜单树的获取", ADMIN_MENU_GET_ALL),
/**
* 菜单项更新
*/
MENU_ADMIN_MENU_UPDATE(MENU_ADMIN_ROLE_CONFIG, "303", "菜单项更新", ADMIN_MENU_INFO_UPDATE),
;
private final String moduleCode;
private final String moduleDesc;
private final String funCode;
private final String funDesc;
private final String operCode;
private final String operDesc;
private final String path;
LevelThreeMenuEnum(LevelTwoMenuEnum level2, String operCode, String operDesc, String path) {
this.moduleCode = level2.getModuleCode();
this.moduleDesc = level2.getModuleDesc();
this.funCode = level2.getFunCode();
this.funDesc = level2.getFunDesc();
this.operCode = operCode;
this.operDesc = operDesc;
this.path = path;
}
@Override
public String getPath() {
return this.path;
}
@Override
public String getModuleCode() {
return this.moduleCode;
}
@Override
public String getModuleDesc() {
return this.moduleDesc;
}
@Override
public String getFunCode() {
return this.funCode;
}
@Override
public String getFunDesc() {
return this.funDesc;
}
@Override
public String getOperCode() {
return this.operCode;
}
@Override
public String getOperDesc() {
return this.operDesc;
}
}
自定义的注解和枚举类如何使用我们在后续的代码层面用的时候在具体体现出来。