自定義注解:com.example.hrh.module.sys.aop.annonation.PermControl
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PermControl {
/**
* 權(quán)限枚舉
* @return
*/
PermType[] value();
/**
* 判定邏輯 OR & AND
* @return
*/
Logical logical() default Logical.AND;
}
自定義注解:com.example.hrh.module.sys.configs.shiro.PermType
public enum PermType {
ADMIN("admin", "超級(jí)管理員"),
SUB_ADMIN("sub_admin", "管理員"),
JDBC_FLAG("jdbc-flag", "JDBC管控");
PermType(String flag, String value) {
this.flag = flag;
this.value = value;
}
public static List<String> keySet() {
List<String> flagSet = new ArrayList<>();
PermType[] types = values();
for (PermType type : types) {
flagSet.add(type.getFlag());
}
return flagSet;
}
private String flag;
private String value;
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
AOP 控制器: com.example.hrh.module.sys.aop.ShiroAspect
@Aspect
@Component
public class ShiroAspect {
/**
* 角色控制服務(wù)
*/
@Autowired
private RoleService roleService;
@Pointcut("@annotation(com.example.hrh.module.sys.aop.annonation.PermControl)")
public void pointCut() {
}
/**
* 前置條件,已經(jīng)登錄
* @param joinPoint
*/
@Before("pointCut()")
@RequiresAuthentication
public void before(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
PermControl permControl = signature.getMethod().getAnnotation(PermControl.class);
boolean jdbcFlag = false;
PermType[] permTypes = permControl.value();
List<String> keySet = new ArrayList<>(permControl.value().length);
for (PermType type : permTypes) {
keySet.add(type.getFlag());
// jdbc權(quán)限控制
if(type.equals(PermType.JDBC_FLAG)){
jdbcFlag = true;
}
}
/**
* 進(jìn)行多種復(fù)雜情況的權(quán)限控制。
*/
if(jdbcFlag){
// TODO JDBC 權(quán)限控制
return;
}
if (ShiroUtils.hasRoles(keySet, permControl.logical().equals(Logical.AND) ? true : false)) {
Log4jUtils.getInstance(getClass()).info("具有權(quán)限:" + keySet);
} else {
throw new PermException("權(quán)限不足!");
}
}
}
自定義權(quán)限異常 com.example.hrh.module.sys.exceptions.PermException
public class PermException extends UnauthorizedException {
public PermException(String message){
super(message);
}
}
SpringBoot 統(tǒng)一權(quán)限異常處理
com.example.hrh.module.sys.handle.ShiroExceptionHandle
@RestControllerAdvice
public class ShiroExceptionHandle {
@ExceptionHandler(UnauthenticatedException.class)
@ResponseStatus(value = HttpStatus.UNAUTHORIZED)
public Object unauthenticatedException(UnauthenticatedException exception) {
System.out.println("message:" + exception.getMessage());
System.out.println("權(quán)限異常:沒(méi)有認(rèn)證");
return "沒(méi)有權(quán)限。。。。。";
}
@ExceptionHandler(UnauthorizedException.class)
@ResponseStatus(value = HttpStatus.UNAUTHORIZED)
public Object unauthorizedException(UnauthorizedException exception) {
System.out.println("message:" + exception.getMessage());
System.out.println("權(quán)限異常:沒(méi)有授權(quán)登錄");
return "沒(méi)有授權(quán)登錄。。。。。";
}
}