当前位置: 移动技术网 > IT编程>开发语言>Java > Java之Spring AOP 实现用户权限验证

Java之Spring AOP 实现用户权限验证

2019年07月22日  | 移动技术网IT编程  | 我要评论

每个项目都会有权限管理系统

无论你是一个简单的企业站,还是一个复杂到爆的平台级项目,都会涉及到用户登录、权限管理这些必不可少的业务逻辑。有人说,企业站需要什么权限管理阿?那行吧,你那可能叫静态页面,就算这样,但你肯定也会有后台管理及登录功能。

每个项目中都会有这些几乎一样的业务逻辑,我们能不能把他们做成通用的系统呢?

aop 实现用户权限验证

aop 在实际项目中运用的场景主要有权限管理(authority management)、事务管理(transaction management)、安全管理(security)、日志管理(logging)和调试管理(debugging)等。

所以,权限验证正好我们可以使用 aop 来直接实现。具体你项目中权限怎么管理,管理的粒度是什么级别这些完全取决于项目需要,这里完全不做任何的讨论。

先说思路:利用自定义注解及拦截器来在你需要的时候,进行你需要的一些权限认证。这里依然涉及到的有enum(枚举)annotation(自定义注解)及拦截器相关知识,废话不多说,直接开写代码。

开始撸一下代码

**一、建立authoritytype.java枚举类

public enum authoritytype {

  // 登录和权限都验证 默认
  validate,

  // 不验证
  novalidate,

  // 不验证权限
  noauthority;
}

这个枚举类的作用,依然是使自定义注解用起来爽到还想要。

二、新建authority.java自定义注解类

import java.lang.annotation.documented;
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)
@documented
public @interface authority { 
  // 默认验证
  authoritytype value() default authoritytype.validate;

} 

三、再建一个authorityannotationinterceptor.java

/**
 * 权限认证拦截器
 *
 */
public class authorityannotationinterceptor extends handlerinterceptoradapter {
  @override
  public boolean prehandle(httpservletrequest request, httpservletresponse response, object handler)
    throws exception {

  if (handler instanceof handlermethod) {
    handlermethod hm = (handlermethod) handler;

    class<?> clazz = hm.getbeantype();
    method m = hm.getmethod();
    try {
      if (clazz != null && m != null) {
        boolean isclzannotation = clazz.isannotationpresent(authority.class);
        boolean ismethondannotation = m.isannotationpresent(authority.class);
        authority authority = null;
        // 如果方法和类声明中同时存在这个注解,那么方法中的会覆盖类中的设定。
        if (ismethondannotation) {
          authority = m.getannotation(authority.class);
        } else if (isclzannotation) {
          authority = clazz.getannotation(authority.class);
        }
        int code = -1;
        string msg = "";
        if (authority != null) {
          if (authoritytype.novalidate == authority.value()) {
            // 标记为不验证,放行
            return true;
          } else if (authoritytype.noauthority == authority.value()) {
            // 不验证权限,验证是否登录
            // todo:
            return true;
          } else {
            // 验证登录及权限
            // todo:

            code = 1;
            msg = "验证成功!";
            return true;
          }
        }

        // //跳转
        // string url = "";
        // response.getwriter().write("<script>top.location.href='"
        // + url + "'</script>");
        // return false;

        // 未通过验证,返回提示json
        map<string, object> responsemap = new hashmap<string, object>();
        responsemap.put("code", code);
        responsemap.put("msg", msg);
        responsemap.put("params", "");
        responsemap.put("rows", "");
        string json = new gson().tojson(responsemap);
        response.setcharacterencoding("utf-8");
        response.setcontenttype("application/json; charset=utf-8");
        response.getwriter().write(json);
        return false;
      }
    } catch (exception e) {
    }
  }
  return false;
  }  
}

这个类的目的就是在打过authority标签的方法及类上,进行权限认证。我这里分了三种类型:全部验证、只验证登录、不验证用来满足我们的业务需求。

这里的返回值可以是 json 串,也可以是跳转到相应的页面,来实现你想要的效果。

四、配置拦截器

<mvc:interceptors>
  <!-- 权限认证拦截器 -->
  <mvc:interceptor>
    <mvc:mapping path="/**"/>
    <bean class="cn.mayongfa.interceptor.authorityannotationinterceptor"></bean>
  </mvc:interceptor>
</mvc:interceptors>

/webcontent/web-inf/springmvc-servlet.xml文件下的<mvc:interceptors>节点配置就行,这里可以配置具体要拦截的 url。

到这里就完成了权限验证的工作了,如何使用呢?

使用就非常简单

因为我们的拦截器配置,然后我们在自定义注解的默认是验证,所以,我们只需要在类名及方法名上打标签就可以。

当然,你完全是可以在拦截器中设置默认就验证所有请求的,接着设置不验证的请求。

文章的具体的案例地址:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网