`
andyjackson
  • 浏览: 57681 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

restlet学习心得—— Guard&Filter

阅读更多

我感觉既然是学习知识没有必要首先一开始就将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
}
 

 

 


分享到:
评论
2 楼 andyjackson 2010-05-12  
junjieshow 写道
在web.xml里配置<url-pattern>/*</url-pattern>,这里拦截了所有的请求,
包括.gif,.css,.js文件,在这里要考虑到使用Filter,处理.gif,.css,.js的请求
public class CommonFilter extends Filter {   
    private static String[] unprotectedResources = {"scripts", "styles","images", "html"};

protected int beforeHandle(Request request, Response response) {   
    String path = request.getResourceRef().getPath();   
    if (isProtectedResource(path)) {   
         String ip = request.getClientInfo().getAddress();   
	 try {   
	//check whether the ip is allowed   
	if (ip is invalid) {   
	  response.setStatusStatus.CONNECTOR_ERROR_CONNECTION);    
	}   
	}catch (Exception ex) {
	    ex.printStackTrace();
		          }   
		        }   
		    }      
 private boolean isProtectedResource(String path) {   
        for (int i = 0; i < unprotectedResources.length; i++) {   
            if (path.indexOf(unprotectedResources[i]) == 1) {   
                return false;    
            }   
        }   
        return true;   
    }   

具体应该怎么写,另外在在不加入spring的情况下,怎么使用该Filter?

首先,你要区分这个filter并不是servlet的filter  filter其实也是一个restlet类,像我上面Guard那样,就可以拦截该application下的所有resources,如果你的项目交由一个application来处理,那么该filter就过滤所有的resources,其中filter有beforehand、afterhand等处理,这种设计有点像spring的拦截器,是一种面向切面的思想
1 楼 junjieshow 2010-05-12  
在web.xml里配置<url-pattern>/*</url-pattern>,这里拦截了所有的请求,
包括.gif,.css,.js文件,在这里要考虑到使用Filter,处理.gif,.css,.js的请求
public class CommonFilter extends Filter {   
    private static String[] unprotectedResources = {"scripts", "styles","images", "html"};

protected int beforeHandle(Request request, Response response) {   
    String path = request.getResourceRef().getPath();   
    if (isProtectedResource(path)) {   
         String ip = request.getClientInfo().getAddress();   
	 try {   
	//check whether the ip is allowed   
	if (ip is invalid) {   
	  response.setStatusStatus.CONNECTOR_ERROR_CONNECTION);    
	}   
	}catch (Exception ex) {
	    ex.printStackTrace();
		          }   
		        }   
		    }      
 private boolean isProtectedResource(String path) {   
        for (int i = 0; i < unprotectedResources.length; i++) {   
            if (path.indexOf(unprotectedResources[i]) == 1) {   
                return false;    
            }   
        }   
        return true;   
    }   

具体应该怎么写,另外在在不加入spring的情况下,怎么使用该Filter?

相关推荐

Global site tag (gtag.js) - Google Analytics