我感觉既然是学习知识没有必要首先一开始就将Spring框架结合进来,这样不利于进一步更深的理解。
今天继续看了一下restlet的相关知识,感觉有必要写一下关于Guard&Filter的相关知识。关于Guard具体是什么以及Filter和restlet中有什么区别,可以参考本站ajax的博客:http://ajaxcn.iteye.com
我现在要讲一下如何在application中配置Guard,在createRoot方法中加入一下代码创建一个Guard的对象:
MapVerifier verifier = new MapVerifier();
verifier.getSecrets().put("JZB", "password".toCharArray());
ChallengeGuard challengeGuard =
new ChallengeGuard(getContext(), ChallengeScheme.HTTP_BASIC, "JZB");
challengeGuard.getAuthenticator().setVerifier(verifier);
其中ChallengeGuard是派生于Guard的类
然后把他challengeGuard的next方法中传入正常的路由router
challengeGuard.setNext(router);
return challengeGuard;
以后,challengeGuard将会对所有的改router下的resources请求进行验证。
对于Filer来说类似
Filer myFilter = new MyFilter(getContext);
myFilter.setNext(router);
return myFilter;
然后就是要自定义一个filter
public class MyFilter extends Filter{
//write you method or overwrite its parent
}