您的位置 首页 java

java项目简单实现API接口访问控制开关

项目架构

springmvc+spring+rpc+mybatis+mysql

需求:

针对某些特殊时期停止某些接口访问(比如:今天不允许用户修改个人资料)

实现:

1、创建策略表system_strategy

2、创建常量类Constant.java

3、创建注解CheckSystemStrategy.java

4、创建切面类SystemStrategyAspect.java

5、CheckSystemStrategy.java注解方式切入接口

代码:

system_strategy表

system_strategy表结构

system_strategy表数据

Constant.java

 public class Constant {
    // 允许操作
    public static int STATUS_ALLOW = 0;
    // 不允许操作
    public static int STATUS_NOT_ALLOW = 1;
    // 个人资料头像
    public static final String MEMBER_AVATAR = "member_avatar";
    // 个人资料基本信息
    public static final String MEMBER_BASIC = "member_basic";
    // 个人资料教育背景
    public static final String MEMBER_EDU = "member_edu";
    // 个人资料联系方式
    public static final String MEMBER_CONTACT = "member_contact";
}  

CheckSystemStrategy.java

 @Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CheckSystemStrategy {
    // 操作
    String action() default "";
    // 是否检查当前操作
    boolean check() default true;
}
  

SystemStrategyAspect.java

 @Order(0)
@Aspect
@Component
public class SystemStrategyAspect {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private SystemStrategyManager systemStrategyManager;
    @Pointcut("@annotation(com.*.aop.annoation.CheckSystemStrategy)")
    public void checkSystemStrategyAspect() {
    }
    @Around(value = "checkSystemStrategyAspect() && @annotation(checkSystemStrategy)")
    public Object around(ProceedingJoinPoint proceedingJoinPoint, CheckSystemStrategy checkSystemStrategy) {
        boolean check = checkSystemStrategy.check();
        String action = checkSystemStrategy.action();
        try {
            if (!check || StringUtils.isBlank(action)) {
                return proceedingJoinPoint.proceed();
            }
            // 查询数据库当前操作是否允许
            SystemStrategy systemStrategy = systemStrategyManager.queryByAction(action);
            if (systemStrategy != null && systemStrategy.getStatusId() == Constant.STATUS_NOT_ALLOW) {
                Result result = new Result();
              result.setCode(systemStrategy.getCode());
                result.setMessage(systemStrategy.getMessage());
                return result;
            }
            return proceedingJoinPoint.proceed();
        } catch (Throwable throwable) {
            logger.error("systemStrategyAspect around error", throwable);
        }
        return null;
    }

}  

service

 /**
 * 更新个人资料头像
 */@Override
@CheckSystemStrategy(action = SystemStrategyModelConstant.MEMBER_AVATAR)
public Result modifyAvatar() {
}

/**
 * 更新个人资料基本信息
 */@Override
@CheckSystemStrategy(action = SystemStrategyModelConstant.MEMBER_BASIC)
public Result modifyHomeSkin() {
}
...  

总结:

1、使用spring aop注解方式降低代码侵入性,无需在各接口业务逻辑中判断。

2、后期如需增加更多接口判断,需要在数据库中和常量类中同步添加对应action。

第一次写文章,分享日常工作用到的小技巧,写的乱码七糟,希望你能学废。

文章来源:智云一二三科技

文章标题:java项目简单实现API接口访问控制开关

文章地址:https://www.zhihuclub.com/174794.shtml

关于作者: 智云科技

热门文章

网站地图