这篇主要是让我们学习对于一些静态页面如何更加简单有效的去访问他们。比如访问一些docs的html文件等。然后就是需要加上简单的权限认证,确保不是所有人都可以去访问的。BASIC的认证时restlet.jar支持的。所以不需要额外的扩展包。这个也是最简单的认证方式。
具体代码如下:
把我们需要访问的那些文件放到c盘的restlet/docs/api下就可以了。它会去自动识别index页面进行访问。
具体代码如下:
public class DirGuardStartRun {
/**
* web browser input:http://localhost:8182/docs
* and then it need a HTTP_BASIC verifier
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// URI of the root directory.
final String ROOT_URI = "file:///c:/restlet/docs/api/";
// Create a component
Component component = new Component();
component.getServers().add(Protocol.HTTP, 8182);
component.getClients().add(Protocol.FILE);
// Create an application
Application application = new Application() {
@Override
public Restlet createInboundRoot() {
Router router = new Router(getContext());
// Create a simple password verifier
MapVerifier verifier = new MapVerifier();
verifier.getLocalSecrets().put("scott", "tiger".toCharArray());
verifier.getLocalSecrets().put("test", "test".toCharArray());
// Create a Guard
ChallengeAuthenticator guard = new ChallengeAuthenticator(getContext(), ChallengeScheme.HTTP_BASIC, "Tutorial");
guard.setVerifier(verifier);
Directory directory = new Directory(getContext(), ROOT_URI);
directory.setListingAllowed(true);
guard.setNext(directory);
router.attach("/docs", guard);
return router;
}
};
// Attach the application to the component and start it
component.getDefaultHost().attach(application);
component.start();
}
}
把我们需要访问的那些文件放到c盘的restlet/docs/api下就可以了。它会去自动识别index页面进行访问。