mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-10-22 03:27:23 +08:00
新增注解式鉴权功能
This commit is contained in:
@@ -2,9 +2,12 @@
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<!-- 基础信息 -->
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-dev</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<version>1.0.2</version>
|
||||
|
||||
<!-- SpringBoot -->
|
||||
<parent>
|
||||
|
@@ -0,0 +1,72 @@
|
||||
package cn.dev33.satoken.annotation;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
|
||||
/**
|
||||
* 注解式鉴权 - 拦截器
|
||||
*/
|
||||
public class SaCheckInterceptor implements HandlerInterceptor {
|
||||
|
||||
// 每次请求之前触发
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
|
||||
throws Exception {
|
||||
|
||||
// 获取处理method
|
||||
if (handler instanceof HandlerMethod == false) {
|
||||
return true;
|
||||
}
|
||||
HandlerMethod method = (HandlerMethod ) handler;
|
||||
|
||||
// 验证登录
|
||||
if(method.hasMethodAnnotation(SaCheckLogin.class) || method.getBeanType().isAnnotationPresent(SaCheckLogin.class)) {
|
||||
StpUtil.getLoginId();
|
||||
}
|
||||
|
||||
// 获取权限注解
|
||||
SaCheckPermission scp = method.getMethodAnnotation(SaCheckPermission.class);
|
||||
if(scp == null) {
|
||||
scp = method.getBeanType().getAnnotation(SaCheckPermission.class);
|
||||
}
|
||||
if(scp == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 开始验证权限
|
||||
Object[] codeArray = concatABC(scp.value(), scp.valueInt(), scp.valueLong());
|
||||
if(scp.isAnd()) {
|
||||
StpUtil.checkPermissionAnd(codeArray); // 必须全部都有
|
||||
} else {
|
||||
StpUtil.checkPermissionOr(codeArray); // 有一个就行了
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// 合并三个数组
|
||||
private Object[] concatABC(String[] a, int[] b, long[] c) {
|
||||
// 循环赋值
|
||||
Object[] d = new Object[a.length + b.length + c.length];
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
d[i] = a[i];
|
||||
}
|
||||
for (int i = 0; i < b.length; i++) {
|
||||
d[a.length + i] = b[i];
|
||||
}
|
||||
for (int i = 0; i < c.length; i++) {
|
||||
d[a.length + b.length + i] = c[i];
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
package cn.dev33.satoken.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 标注一个路由方法,当前会话必须已登录才能通过
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.METHOD, ElementType.TYPE })
|
||||
public @interface SaCheckLogin {
|
||||
|
||||
}
|
@@ -0,0 +1,39 @@
|
||||
package cn.dev33.satoken.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 标注一个路由方法,当前会话必须具有指定权限才可以通过
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.METHOD,ElementType.TYPE})
|
||||
public @interface SaCheckPermission {
|
||||
|
||||
/**
|
||||
* 权限码数组 ,String类型
|
||||
* @return .
|
||||
*/
|
||||
String [] value() default {};
|
||||
|
||||
/**
|
||||
* 权限码数组 ,int类型
|
||||
* @return .
|
||||
*/
|
||||
int [] valueInt() default {};
|
||||
|
||||
/**
|
||||
* 权限码数组 ,long类型
|
||||
* @return .
|
||||
*/
|
||||
long [] valueLong() default {};
|
||||
|
||||
/**
|
||||
* 是否属于and型验证 ,true=必须全部具有,false=只要具有一个就可以通过
|
||||
* @return .
|
||||
*/
|
||||
boolean isAnd() default true;
|
||||
|
||||
}
|
Reference in New Issue
Block a user