福州网站建设加推广,最好的网站建设价格,易营宝自助建站系统,大兵seo博客在构建系统时#xff0c;开发人员通常会忽略安全性方面。 安全一直是令人担忧的重要问题#xff0c;但是它比以前吸引了更高的关注。 就在今年#xff0c;我们发生了像Heartbleed Bug或CelebrityGate丑闻这样的案件。 这与帖子无关#xff0c;只是安全真正重要的示例#… 在构建系统时开发人员通常会忽略安全性方面。 安全一直是令人担忧的重要问题但是它比以前吸引了更高的关注。 就在今年我们发生了像Heartbleed Bug或CelebrityGate丑闻这样的案件。 这与帖子无关只是安全真正重要的示例我们应该意识到这一点。 随着REST服务的日益普及有必要以某种方式确保这些安全。 几周前我不得不将客户端与https后面的REST服务集成。 我以前从未做过这就是这篇文章的原因。 我必须承认自己不是安全专家所以如果我写任何愚蠢的文章请纠正我。 设置 对于此示例我使用了以下设置 具有SSL配置的 TomEE 或Tomcat 弹簧 Apache HTTP组件 我不会讨论有关SSL和TSL的许多详细信息因此请在此处查看其他内容。 请注意TLS是SSL演进的新名称。 有时两者之间会产生混淆人们通常会说SSL但使用的是TSL的最新版本。 记住这一点。 不要忘记按照下一页上的说明为Tomcat设置SSL SSL Configuration HOW-TO 。 服务器需要向客户端提供一组凭据证书以保护服务器与客户端之间的连接。 编码 服务 让我们创建一个简单的Spring REST服务 RestService.java Controller
RequestMapping(/)
public class RestService {RequestMapping(method RequestMethod.GET)ResponseBodypublic String get() {return Called the get Rest Service;}
} 而且我们还需要一些接线来使其工作 RestConfig.java Configuration
EnableWebMvc
ComponentScan(basePackages com.radcortez.rest.ssl)
public class RestConfig {} web.xml ?xml version1.0 encodingUTF-8?
web-appversion3.1xmlnshttp://xmlns.jcp.org/xml/ns/javaeexmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsdservletservlet-namerest/servlet-nameservlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-classinit-paramparam-namecontextClass/param-nameparam-valueorg.springframework.web.context.support.AnnotationConfigWebApplicationContext/param-value/init-paraminit-paramparam-namecontextConfigLocation/param-nameparam-valuecom.radcortez.rest.ssl/param-value/init-paramload-on-startup1/load-on-startup/servletservlet-mappingservlet-namerest/servlet-nameurl-pattern//url-pattern/servlet-mappingsecurity-constraintweb-resource-collectionweb-resource-nameRest Application/web-resource-nameurl-pattern/*/url-pattern/web-resource-collectionuser-data-constraint!-- Needed for our application to respond to https requests --transport-guaranteeCONFIDENTIAL/transport-guarantee/user-data-constraint/security-constraint
/web-app 请注意元素security-constraint user-data-constraint和transport-guaranteeCONFIDENTIAL/transport-guarantee 。 需要这些来指定应用程序需要安全连接。 选中“ 保护 Java应用程序的Web应用程序安全” 。 运行服务 只需使用您喜欢的IDE环境在TomEE服务器上部署应用程序然后访问https://localhost:8443/ 。 您应该获得以下信息您可能需要先接受服务器证书 请注意浏览器协议为https 端口为8443 假设您将默认设置保留在SSL Configuration HOW-TO中 。 客户 现在如果您尝试使用Java客户端调用此REST服务则很可能将获得以下消息和Exception或类似信息 消息 GET请求“ https// localhost8443 /”上的I / O错误sun.security.validator.ValidatorException 异常 由 以下原因 引起javax.net.ssl.SSLHandshakeExceptionsun.security.validator.ValidatorExceptionPKIX路径构建失败sun.security.provider.certpath.SunCertPathBuilderException无法找到到请求目标的有效证书路径 发生这种情况是因为正在运行的JDK没有针对您的服务器的有效证书。 您可以导入它并解决该问题但是让我们做一些更有趣的事情。 我们将以编程方式为受信任的密钥库提供服务器证书。 这在以下情况下特别有用 您正在将代码运行到多个环境中 您不必每次都手动将证书导入JDK 如果您升级JDK则必须记住有关证书的信息 由于某些奇怪的原因您无权访问JDK本身来导入证书 让我们写一些代码 RestClientConfig.java Configuration
PropertySource(classpath:config.properties)
public class RestClientConfig {Beanpublic RestOperations restOperations(ClientHttpRequestFactory clientHttpRequestFactory) throws Exception {return new RestTemplate(clientHttpRequestFactory);}Beanpublic ClientHttpRequestFactory clientHttpRequestFactory(HttpClient httpClient) {return new HttpComponentsClientHttpRequestFactory(httpClient);}Beanpublic HttpClient httpClient(Value(${keystore.file}) String file,Value(${keystore.pass}) String password) throws Exception {KeyStore trustStore KeyStore.getInstance(KeyStore.getDefaultType());FileInputStream instream new FileInputStream(new File(file));try {trustStore.load(instream, password.toCharArray());} finally {instream.close();}SSLContext sslcontext SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()).build();SSLConnectionSocketFactory sslsf new SSLConnectionSocketFactory(sslcontext, new String[]{TLSv1.2}, null,BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);return HttpClients.custom().setSSLSocketFactory(sslsf).build();}Beanpublic static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {return new PropertySourcesPlaceholderConfigurer();}
} 在这里我们使用Spring RestOperations接口该接口指定了一组基本的RESTful操作。 接下来我们使用Apache HTTP组件SSLConnectionSocketFactory 它使我们能够根据可信证书列表来验证服务器的身份。 证书从KeyStore从服务器使用的同一文件中加载 。 RestServiceClientIT.java RunWith(SpringJUnit4ClassRunner.class)
ContextConfiguration(classes RestClientConfig.class)
public class RestServiceClientIT {Autowiredprivate RestOperations rest;Testpublic void testRestRequest() throws Exception {ResponseEntity response rest.getForEntity(https://localhost:8443/, String.class);System.out.println(response response);System.out.println(response.getBody() response.getBody());}
} 一个简单的测试类。 我们还需要一个具有密钥库文件位置和密码的属性文件 config.properties keystore.file${user.home}/.keystore
keystore.passchangeit 如果您使用了所有默认值这应该可以正常工作。 运行测试 如果现在运行在Java客户端中调用REST服务的测试则应获得以下输出 响应 200 OK调用了get Rest服务{服务器 [Apache-土狼/1.1]缓存控制 [私有]到期时间 [星期四1970年1月1日周四010000 WET]内容类型 Content-Length [27]Date [Tue2014年12月23日格林尼治标准时间]] 身体 叫得到休息服务 结论 而已 现在您可以以安全的方式与客户端调用REST服务。 如果您希望将证书添加到JDK密钥库请检查此文章 。 敬请期待与Java EE JAX-RS等效的等效产品。 资源资源 您可以从我的github仓库REST SSL克隆完整的工作副本。 翻译自: https://www.javacodegeeks.com/2014/12/ssl-tls-rest-server-client-with-spring-and-tomee.html