Play framework interceptors

In the controller, the definition of interceptor method. Interceptor will be called for all action controller class and future generations.

These methods must be static, but can not be public, and use effectively block comment.

@Before

With @Before comments execution method will be called before each controller action

public class weixinIntercept extends Controller{
    @Before(unless="login") //""中写的是方法的具体位置,例:wechat.account.wechatInformation
    static void check(){
        if(session.get("user") == null)
            login();
    }
}
/*
    可以使用unless和only,还可以使用@After,@Before,@Finally注释
*/

Controller Inheritance:

If a controller class is a subclass of the class of other controllers, it will be applied in accordance with the appropriate subclass inherits order.

Use @With comments add more interceptors

public class Security extends Controller{
    @Before
    protected static void checkAuthentic(){
        if(!session.containsKey("user"){
            unAuthen();
        }
    }
}


//另一个控制器
@With(Security.class)
public class Admin extends Controller{
    ......
}

 

Published 15 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/J_M_S_H_T/article/details/88292407