1:使用场景:Mock*网证件信息校验
2:Jetty嵌入式Server启动方式:由于Jetty9.x(需jdk7.x以上)以后Server启动方式有略微差异,所以分开说明:
2.1 Jetty9.x嵌入式Http-Server启动:
public void doRun() { Server server = new Server(); ServerConnector connector = new ServerConnector(server); connector.setPort(); connector.setIdleTimeout(); server.setConnectors(new Connector[]{connector}); server.setHandler(JettyServerHandlerFactory.getHandle()); try { server.start(); server.join(); logger.info("jetty server start!"); } catch (Exception e) { e.printStackTrace(); } }
2.2 Jetty9.x嵌入式Https-Server启动:
public void doRunHttps(int port,String path){ Server server = new Server(); HttpConfiguration https_config = new HttpConfiguration(); https_config.setSecureScheme("https"); https_config.setSecurePort(port); https_config.setOutputBufferSize(32768); https_config.addCustomizer(new SecureRequestCustomizer()); SslContextFactory sslContextFactory = new SslContextFactory(); sslContextFactory.setKeyStorePath(path); sslContextFactory.setKeyStorePassword("OBF:18jj18jj18jj18jj18jj18jj");//111111:18jj18jj18jj18jj18jj18jj sslContextFactory.setKeyManagerPassword("OBF:18jj18jj18jj18jj18jj18jj");//123456:19iy19j019j219j419j619j8 ServerConnector httpsConnector = new ServerConnector(server, new SslConnectionFactory(sslContextFactory,"http/1.1"), new HttpConnectionFactory(https_config)); httpsConnector.setPort(8443); httpsConnector.setIdleTimeout(500000); server.addConnector(httpsConnector); server.setHandler(JettyServerHandlerFactory.getWebHandle()); try { server.start(); server.join(); } catch (Exception e) { e.printStackTrace(); } }
2.3 Jetty8.x嵌入式Https-Server启动:
public void doRunHttps(int port,String path){ // create a jetty server and setup the SSL context Server server = new Server(); SslContextFactory sslContextFactory = new SslContextFactory(path); sslContextFactory.setKeyStorePassword("111111"); sslContextFactory.setTrustStore(path); sslContextFactory.setTrustStorePassword("111111"); sslContextFactory.setNeedClientAuth(false); // create a https connector SslSocketConnector connector = new SslSocketConnector(sslContextFactory); connector.setPort(port); // register the connector server.setConnectors(new Connector[] { connector }); server.setHandler(JettyServerHandlerFactory.getWebHandle()); try { server.start(); server.join(); } catch (Exception e) { e.printStackTrace(); } }
3:Xfir+Spring+Maven配置启动
3.1 Maven配置文件说明
<!--jetty配置--> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> <version>${jetty-version}</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-servlet</artifactId> <version>${jetty-version}</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-webapp</artifactId> <version>${jetty-version}</version> </dependency> <!--gzip:jetty https 压缩传输--> <dependency> <groupId>net.sourceforge.pjl-comp-filter</groupId> <artifactId>pjl-comp-filter</artifactId> <version>1.7</version> </dependency> <!--xfire 依赖--> <dependency> <groupId>org.codehaus.xfire</groupId> <artifactId>xfire-all</artifactId> <version>1.2.6</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency>
3.2 spring配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 引入XFire预配置信息 --> <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" /> <!--定义访问的url--> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <map> <entry key="/nciic.ws"> <ref bean="serviceInfoService" /> </entry> </map> </property> </bean> <!-- 使用XFire导出器 --> <bean id="baseWebService" class="org.codehaus.xfire.spring.remoting.XFireExporter" lazy-init="false" abstract="true"> <!-- 引用xfire.xml中定义的工厂 --> <property name="serviceFactory" ref="xfire.serviceFactory" /> <!-- 引用xfire.xml中的xfire实例 --> <property name="xfire" ref="xfire" /> </bean> <bean id="serviceInfoService" parent="baseWebService"> <!-- 业务服务bean --> <property name="serviceBean" ref="serviceInfo" /> <!-- 业务服务bean的窄接口类 --> <property name="serviceClass" value="com.wangyin.xfire.ServiceInfo" /> </bean> <bean id="serviceInfo" class="com.wangyin.xfire.impl.ServiceInfoImpl"></bean> </beans>
3.3:对应的java类:
public interface ServiceInfo { // 核查方法 public String nciicCheck(String inLicense, String inConditions); // 取得条件文件模板 public String nciicGetCondition(String inLicense) throws Exception; } @Component("serviceInfo") public class ServiceInfoImpl implements ServiceInfo { private final static Logger logger = LoggerFactory.getLogger(ServiceInfoImpl.class); @Override public String nciicCheck(String inLicense, String inConditions) { logger.info("do nciic check..."); //TODO return ""; } @Override public String nciicGetCondition(String inLicense) throws Exception { return ""; } }
4:补充jetty相关配置和说明:
4.1:KeyStore 获取说明:
第一步,命令行下使用jdk的keytool工具生成keystore
keytool -keystore keystore -alias jetty -genkey -keyalg RSA
第二步,生成证书
keytool -export -alias jetty -file jetty.crt -keystore keystore
第三步,生成OBA文件,这里的yoursslpassword是第一步填写的密码
java -cp jetty-all-9.0.5.v20130815.jar org.eclipse.jetty.util.security.Password yoursslpassword
4.2:JettyServer工厂类
public class JettyServerHandlerFactory { public static Handler getHandle() { IPAccessHandler ipAccessHandler = getFireWallHandler(); ipAccessHandler.setHandler(getServletHandler()); HandlerCollection handlers = new HandlerCollection(); handlers.setHandlers(new Handler[]{ipAccessHandler, new DefaultHandler()}); return handlers; } public static Handler getWebHandle(){ WebAppContext webApp = new WebAppContext(); webApp.setContextPath("/mockcd"); webApp.setResourceBase("WebRoot"); webApp.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "true"); webApp.setInitParameter("org.eclipse.jetty.servlet.Default.useFileMappedBuffer", "true"); HandlerCollection handlers = new HandlerCollection(); handlers.setHandlers(new Handler[]{webApp,getHandle(), new DefaultHandler()}); return handlers; } private static IPAccessHandler getFireWallHandler() { IPAccessHandler ipHandler = new IPAccessHandler(); return ipHandler; } private static Handler getServletHandler() { ServletContextHandler root = new ServletContextHandler( ServletContextHandler.SESSIONS); root.setSessionHandler(new SessionHandler()); root.setContextPath("/"); root.setResourceBase("META-INF/xfire/services.xml"); ServletHolder holder = new ServletHolder(new XFireConfigurableServlet()); root.addServlet(holder, "/servlet/XFireServlet"); root.addServlet(holder, "/services/*"); root.addFilter(CompressingFilter.class,"/*",null); return root; } }