mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-12-21 10:59:45 +08:00
SaAnnotationAbstractHandler 由抽象类改为接口
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.pj.cases.test;
|
||||
|
||||
import cn.dev33.satoken.util.SaResult;
|
||||
import com.pj.satoken.custom_annotation.CheckAccount;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@@ -15,6 +16,7 @@ public class TestController {
|
||||
|
||||
// 测试 浏览器访问: http://localhost:8081/test/test
|
||||
@RequestMapping("test")
|
||||
@CheckAccount(name = "sa", pwd = "123456")
|
||||
public SaResult test() {
|
||||
System.out.println("------------进来了");
|
||||
return SaResult.ok();
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.pj.satoken.custom_annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 账号校验:在标注一个方法上时,要求前端必须提交相应的账号密码参数才能访问方法。
|
||||
*
|
||||
* @author click33
|
||||
*
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.METHOD, ElementType.TYPE})
|
||||
public @interface CheckAccount {
|
||||
|
||||
/**
|
||||
* 需要校验的账号
|
||||
*
|
||||
* @return /
|
||||
*/
|
||||
String name();
|
||||
|
||||
/**
|
||||
* 需要校验的密码
|
||||
*
|
||||
* @return /
|
||||
*/
|
||||
String pwd();
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.pj.satoken.custom_annotation;
|
||||
|
||||
import cn.dev33.satoken.util.SaTokenConsts;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 二级认证校验(User版):客户端必须完成二级认证之后,才能进入该方法,否则将被抛出异常。
|
||||
*
|
||||
* <p> 可标注在方法、类上(效果等同于标注在此类的所有方法上)。
|
||||
*
|
||||
* @author click33
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.METHOD, ElementType.TYPE })
|
||||
public @interface SaUserCheckSafe {
|
||||
|
||||
/**
|
||||
* 要校验的服务
|
||||
*
|
||||
* @return /
|
||||
*/
|
||||
String value() default SaTokenConsts.DEFAULT_SAFE_AUTH_SERVICE;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.pj.satoken.custom_annotation.handler;
|
||||
|
||||
import cn.dev33.satoken.annotation.handler.SaAnnotationAbstractHandler;
|
||||
import cn.dev33.satoken.context.SaHolder;
|
||||
import cn.dev33.satoken.exception.SaTokenException;
|
||||
import com.pj.satoken.custom_annotation.CheckAccount;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
|
||||
/**
|
||||
* 注解 CheckAccount 的处理器
|
||||
*
|
||||
* @author click33
|
||||
*
|
||||
*/
|
||||
@Component
|
||||
public class CheckAccountHandler implements SaAnnotationAbstractHandler<CheckAccount> {
|
||||
|
||||
// 指定这个处理器要处理哪个注解
|
||||
@Override
|
||||
public Class<CheckAccount> getHandlerAnnotationClass() {
|
||||
return CheckAccount.class;
|
||||
}
|
||||
|
||||
// 每次请求校验注解时,会执行的方法
|
||||
@Override
|
||||
public void checkMethod(CheckAccount at, AnnotatedElement element) {
|
||||
// 获取前端请求提交的参数
|
||||
String name = SaHolder.getRequest().getParamNotNull("name");
|
||||
String pwd = SaHolder.getRequest().getParamNotNull("pwd");
|
||||
|
||||
// 与注解中指定的值相比较
|
||||
if(name.equals(at.name()) && pwd.equals(at.pwd()) ) {
|
||||
// 校验通过,什么也不做
|
||||
} else {
|
||||
// 校验不通过,则抛出异常
|
||||
throw new SaTokenException("账号或密码错误,未通过校验");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.pj.satoken.custom_annotation.handler;
|
||||
|
||||
import cn.dev33.satoken.annotation.handler.SaAnnotationAbstractHandler;
|
||||
import cn.dev33.satoken.annotation.handler.SaCheckLoginHandler;
|
||||
import com.pj.satoken.StpUserUtil;
|
||||
import com.pj.satoken.custom_annotation.SaUserCheckLogin;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
|
||||
/**
|
||||
* 注解 SaUserCheckLogin 的处理器
|
||||
*
|
||||
* @author click33
|
||||
*/
|
||||
@Component
|
||||
public class SaUserCheckLoginHandler implements SaAnnotationAbstractHandler<SaUserCheckLogin> {
|
||||
|
||||
@Override
|
||||
public Class<SaUserCheckLogin> getHandlerAnnotationClass() {
|
||||
return SaUserCheckLogin.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkMethod(SaUserCheckLogin at, AnnotatedElement element) {
|
||||
SaCheckLoginHandler._checkMethod(StpUserUtil.TYPE);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.pj.satoken.custom_annotation.handler;
|
||||
|
||||
import cn.dev33.satoken.annotation.handler.SaAnnotationAbstractHandler;
|
||||
import cn.dev33.satoken.annotation.handler.SaCheckPermissionHandler;
|
||||
import com.pj.satoken.StpUserUtil;
|
||||
import com.pj.satoken.custom_annotation.SaUserCheckPermission;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
|
||||
/**
|
||||
* 注解 SaUserCheckPermission 的处理器
|
||||
*
|
||||
* @author click33
|
||||
*/
|
||||
@Component
|
||||
public class SaUserCheckPermissionHandler implements SaAnnotationAbstractHandler<SaUserCheckPermission> {
|
||||
|
||||
@Override
|
||||
public Class<SaUserCheckPermission> getHandlerAnnotationClass() {
|
||||
return SaUserCheckPermission.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkMethod(SaUserCheckPermission at, AnnotatedElement element) {
|
||||
SaCheckPermissionHandler._checkMethod(StpUserUtil.TYPE, at.value(), at.mode(), at.orRole());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.pj.satoken.custom_annotation.handler;
|
||||
|
||||
import cn.dev33.satoken.annotation.handler.SaAnnotationAbstractHandler;
|
||||
import cn.dev33.satoken.annotation.handler.SaCheckRoleHandler;
|
||||
import com.pj.satoken.StpUserUtil;
|
||||
import com.pj.satoken.custom_annotation.SaUserCheckRole;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
|
||||
/**
|
||||
* 注解 SaUserCheckRole 的处理器
|
||||
*
|
||||
* @author click33
|
||||
*/
|
||||
@Component
|
||||
public class SaUserCheckRoleHandler implements SaAnnotationAbstractHandler<SaUserCheckRole> {
|
||||
|
||||
@Override
|
||||
public Class<SaUserCheckRole> getHandlerAnnotationClass() {
|
||||
return SaUserCheckRole.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkMethod(SaUserCheckRole at, AnnotatedElement element) {
|
||||
SaCheckRoleHandler._checkMethod(StpUserUtil.TYPE, at.value(), at.mode());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.pj.satoken.custom_annotation.handler;
|
||||
|
||||
import cn.dev33.satoken.annotation.handler.SaAnnotationAbstractHandler;
|
||||
import cn.dev33.satoken.annotation.handler.SaCheckSafeHandler;
|
||||
import com.pj.satoken.StpUserUtil;
|
||||
import com.pj.satoken.custom_annotation.SaUserCheckSafe;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
|
||||
/**
|
||||
* 注解 SaUserCheckPermission 的处理器
|
||||
*
|
||||
* @author click33
|
||||
*/
|
||||
@Component
|
||||
public class SaUserCheckSafeHandler implements SaAnnotationAbstractHandler<SaUserCheckSafe> {
|
||||
|
||||
@Override
|
||||
public Class<SaUserCheckSafe> getHandlerAnnotationClass() {
|
||||
return SaUserCheckSafe.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkMethod(SaUserCheckSafe at, AnnotatedElement element) {
|
||||
SaCheckSafeHandler._checkMethod(StpUserUtil.TYPE, at.value());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.pj.satoken.merge_annotation;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckLogin;
|
||||
import com.pj.satoken.StpUserUtil;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 登录认证(User版):只有登录之后才能进入该方法
|
||||
* <p> 可标注在函数、类上(效果等同于标注在此类的所有方法上)
|
||||
* @author click33
|
||||
*
|
||||
*/
|
||||
@SaCheckLogin(type = StpUserUtil.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.METHOD, ElementType.TYPE})
|
||||
public @interface SaUserCheckLogin {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.pj.satoken.merge_annotation;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckPermission;
|
||||
import cn.dev33.satoken.annotation.SaMode;
|
||||
import com.pj.satoken.StpUserUtil;
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 权限认证(User版):必须具有指定权限才能进入该方法
|
||||
* <p> 可标注在函数、类上(效果等同于标注在此类的所有方法上)
|
||||
* @author click33
|
||||
*
|
||||
*/
|
||||
@SaCheckPermission(type = StpUserUtil.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.METHOD, ElementType.TYPE})
|
||||
public @interface SaUserCheckPermission {
|
||||
|
||||
/**
|
||||
* 需要校验的权限码
|
||||
* @return 需要校验的权限码
|
||||
*/
|
||||
@AliasFor(annotation = SaCheckPermission.class)
|
||||
String [] value() default {};
|
||||
|
||||
/**
|
||||
* 验证模式:AND | OR,默认AND
|
||||
* @return 验证模式
|
||||
*/
|
||||
@AliasFor(annotation = SaCheckPermission.class)
|
||||
SaMode mode() default SaMode.AND;
|
||||
|
||||
/**
|
||||
* 在权限校验不通过时的次要选择,两者只要其一校验成功即可通过校验
|
||||
*
|
||||
* <p>
|
||||
* 例1:@SaCheckPermission(value="user-add", orRole="admin"),
|
||||
* 代表本次请求只要具有 user-add权限 或 admin角色 其一即可通过校验。
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* 例2: orRole = {"admin", "manager", "staff"},具有三个角色其一即可。 <br>
|
||||
* 例3: orRole = {"admin, manager, staff"},必须三个角色同时具备。
|
||||
* </p>
|
||||
*
|
||||
* @return /
|
||||
*/
|
||||
@AliasFor(annotation = SaCheckPermission.class)
|
||||
String[] orRole() default {};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.pj.satoken.merge_annotation;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckRole;
|
||||
import cn.dev33.satoken.annotation.SaMode;
|
||||
import com.pj.satoken.StpUserUtil;
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 角色认证(User版):必须具有指定角色标识才能进入该方法
|
||||
* <p> 可标注在函数、类上(效果等同于标注在此类的所有方法上)
|
||||
* @author click33
|
||||
*
|
||||
*/
|
||||
@SaCheckRole(type = StpUserUtil.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.METHOD, ElementType.TYPE})
|
||||
public @interface SaUserCheckRole {
|
||||
|
||||
/**
|
||||
* 需要校验的角色标识
|
||||
* @return 需要校验的角色标识
|
||||
*/
|
||||
@AliasFor(annotation = SaCheckRole.class)
|
||||
String [] value() default {};
|
||||
|
||||
/**
|
||||
* 验证模式:AND | OR,默认AND
|
||||
* @return 验证模式
|
||||
*/
|
||||
@AliasFor(annotation = SaCheckRole.class)
|
||||
SaMode mode() default SaMode.AND;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.pj.satoken.merge_annotation;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckSafe;
|
||||
import cn.dev33.satoken.util.SaTokenConsts;
|
||||
import com.pj.satoken.StpUserUtil;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 二级认证校验(User版):客户端必须完成二级认证之后,才能进入该方法,否则将被抛出异常。
|
||||
*
|
||||
* <p> 可标注在方法、类上(效果等同于标注在此类的所有方法上)。
|
||||
*
|
||||
* @author click33
|
||||
*/
|
||||
@SaCheckSafe(type = StpUserUtil.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.METHOD, ElementType.TYPE })
|
||||
public @interface SaUserCheckSafe {
|
||||
|
||||
/**
|
||||
* 要校验的服务
|
||||
*
|
||||
* @return /
|
||||
*/
|
||||
String value() default SaTokenConsts.DEFAULT_SAFE_AUTH_SERVICE;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user