赞
踩
首先启动一个Server:
- public Server getServer() {
- Server server = new Server(new InetSocketAddress("localhost", 8081));
- ServletContextHandler jerseyHandler = getJerseyHandler();
- setSecurity(jerseyHandler);
- server.setHandler(jerseyHandler);
-
- return server;
- }
- public ServletContextHandler getJerseyHandler() {
- ServletContextHandler handler = new ServletContextHandler(ServletContextHandler.SESSIONS);
- handler.setContextPath("/ws/v1");
- //add jersey container
- ServletHolder jerseyServletHolder = new ServletHolder(ServletContainer.class);
- jerseyServletHolder.setInitParameter("com.sun.jersey.config.property.resourceConfigClass",
- "com.sun.jersey.api.core.PackagesResourceConfig");
- jerseyServletHolder.setInitParameter("com.sun.jersey.config.property.packages", "rest/resouce");
- handler.addServlet(jerseyServletHolder, "/*");
- return handler;
- }
- private void setSecurity(ServletContextHandler handler) {
- ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler();
- handler.setSecurityHandler(securityHandler);
-
- Constraint constraint = new Constraint();
- constraint.setName("auth");
- constraint.setAuthenticate(true);
- constraint.setRoles(new String[]{"user", "admin"});
-
- ConstraintMapping mapping = new ConstraintMapping();
- mapping.setPathSpec("ws/v1/*");
- mapping.setConstraint(constraint);
-
- securityHandler.setConstraintMappings(Collections.singletonList(mapping));
- securityHandler.setAuthenticator(new FormAuthenticator());
- securityHandler.setLoginService(new HashLoginService("MyRealm","realm.properties"));
- }
jetty: MD5:164c88b302622e17050af52c89945d44,user
admin: CRYPT:adpexzg3FUZAk,server-administrator,content-administrator,admin,user
other: OBF:1xmk1w261u9r1w1c1xmq,user
plain: plain,user
user: password,user
- public void run() {
- Server server = getServer();
- try {
- server.start();
- server.join();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
下面是web页面的Jetty嵌入式代码:
- public HandlerWrapper getWebHandler() {
- WebAppContext context = new WebAppContext();
-
- context.setContextPath("/");
- context.setDescriptor("hugetable-core/src/main/webapp/WEB-INF/web.xml");
- context.setResourceBase("hugetable-core/src/main/webapp");
- context.setClassLoader(Thread.currentThread().getContextClassLoader());
- context.setParentLoaderPriority(true);
-
- return context;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。