playframework 拦截器

来源:互联网 发布:linux中的vi指令 编辑:程序博客网 时间:2024/05/16 17:54

前几天没搞清楚play拦截器正确用法被架构师狠狠地鄙视了一把,妈的,早上起来就把文档狠狠地温习了一遍,加上自己的想法,记录之。

(详见play官方文档)


我觉得play的拦截器有点AOP的思想,相当于struts的filter,play的拦截器使用注解方式实现的。

源码定义了这么几种注解:@before、@after、@catch、@finally 可谓是三百六十度无死角的拦截了。


标注了@Before的方法会在该类的所有方法之前执行,举例:

public class Admin extends Controller {     @Before    static void checkAuthentification() {        if(session.get("user") == null) login();    }     public static void index() {        List<User> users = User.findAll();        render(users);    }    …}

当请求进入Admin时会先检查这个类中是否有@before注解,有就优先执行。

当然还有高级点的特性,如果您不想拦截某些方法,您可以使用unless表明,这样如果用户发来login的请求就不会执行@Before了,如下:

public class Admin extends Controller {     @Before(unless="login")    static void checkAuthentification() {        if(session.get("user") == null) login();    }     public static void index() {        List<User> users = User.findAll();        render(users);    }     …}
同样,您也可以使用only属性,指定需要拦截的请求:

public class Admin extends Controller {     @Before(only={"login","logout"})    static void doSomething() {          …      }    …}


@After注解的道理是一样的,不过是执行了请求方法之后执行

public class Admin extends Controller {     @After    static void log() {        Logger.info("Action executed ...");    }     public static void index() {        List<User> users = User.findAll();        render(users);    }     …}


下面说说@Catch注解,这个很好用,当程序抛出异常时候可以被标注了@Catch的方法抓住,我们可以在里面做一些必要的操作,比如rollback等。

public class Admin extends Controller {    @Catch(IllegalStateException.class)    public static void logIllegalState(Throwable throwable) {        Logger.error("Illegal state %s…", throwable);    }        public static void index() {        List<User> users = User.findAll();        if (users.size() == 0) {            throw new IllegalStateException("Invalid database - 0 users");        }        render(users);    }}

对了,其中的prioroty属性可以设置执行顺序的优先级,priority=1是最先执行的。

public class Admin extends Controller {     @Catch(value = Throwable.class, priority = 1)    public static void logThrowable(Throwable throwable) {        // Custom error logging…        Logger.error("EXCEPTION %s", throwable);    }     @Catch(value = IllegalStateException.class, priority = 2)    public static void logIllegalState(Throwable throwable) {        Logger.error("Illegal state %s…", throwable);    }     public static void index() {        List<User> users = User.findAll();        if(users.size() == 0) {            throw new IllegalStateException("Invalid database - 0 users");        }        render(users);    }}

@Finally注解的方法和java的finally一样,不管有没有执行成功都会进入该方法,我们可以在里面打印日志:

public class Admin extends Controller {     @Finally    static void log(Throwable e) {        if( e == null ){            Logger.info("action call was successful");        } else{            Logger.info("action call failed", e);        }    }     public static void index() {        List<User> users = User.findAll();        render(users);    }    …}


上述所有的注解作用域都是类级别的,如果您想对另外的类也起作用,请用@With标签:

public class Secure extends Controller {        @Before    static void checkAuthenticated() {        if(!session.containsKey("user")) {            unAuthorized();        }    }}    @With(Secure.class)public class Admin extends Controller {        …}

实现的原理应该是这样:socket请求发过来的时候,play 的路由会去匹配进入哪个Controller,然后看这个Controller里面有哪些系统标签,然后按规定好的执行顺序依次执行,这就是所谓的拦截了。(有时间把源码分析也一起发出来)

知道原理,我们就可以写自己的拦截器了,比如权限管理:在每个需要权限的方法上加上自定义的权限注解如:@MyAnatation(priority=1),用户发起请求——@Before拦截——从登录token拿到用户信息——查看该用户所有权限ID——对比自定义注解的priority——priority在权限ID中则允许访问,不包含返回无此权限信息。


 @Before(priority = 1)    public static void doAuth(){        Annotation annotation =  request.invokedMethod.getAnnotation(MyAnotation.class);        if(annotation !=null){            //鉴权开始            QicFunction function =  (QicFunction)annotation;            //todo       }   }

有了这种思想,我们可以做任何自定义的拦截行为,谁也拦不住。


最后,要提醒自己,路漫漫其修远兮,吾将上下而求索。加油啊!




0 1