html如何做购物网站,创办公司的基本流程,小程序微商城制作教程,上海市中心是哪个区有时我们只需要CAPTCHA #xff0c;这是一个可悲的事实。 今天#xff0c;我们将学习如何与reCAPTCHA集成。 因为主题本身并不是特别有趣和高级#xff0c;所以我们将通过使用Spring Integration处理低级细节来过度设计#xff08;#xff1f;#xff09;。 Google决定使… 有时我们只需要CAPTCHA 这是一个可悲的事实。 今天我们将学习如何与reCAPTCHA集成。 因为主题本身并不是特别有趣和高级所以我们将通过使用Spring Integration处理低级细节来过度设计。 Google决定使用reCAPTCHA的决定取决于两个因素1这是一种适度良好的CAPTCHA实施方案具有体面的图像并内置了对视力障碍者的支持2外包CAPTCHA可使我们在服务器端保持无状态。 更不用说我们在图书数字化方面有所帮助。 第二个原因实际上很重要。 通常您必须在服务器端生成CAPTCHA并将预期结果存储在例如用户会话中。 当响应返回时您比较预期的并输入了CAPTCHA解决方案。 有时我们不想在服务器端存储任何状态更不用说实现验证码并不是特别有意义的任务。 因此拥有现成的和可以接受的东西真是太好了。 完整的源代码一如既往地可用我们从一个简单的Spring MVC Web应用程序开始没有任何验证码。 reCAPTCHA是免费的但需要注册因此第一步是在我们的示例项目中唱歌并生成您的公钥/私钥和填写的app.properties配置文件。 要在表单上显示reCAPTCHA并将其包括在表单中只需添加JavaScript库 div idrecaptcha /div
...
script srchttp://www.google.com/recaptcha/api/js/recaptcha_ajax.js/script 并将reCAPTCHA小部件放置在您喜欢的任何位置 Recaptcha.create(${recaptcha_public_key},recaptcha,{theme: white,lang : en}
); 官方文档非常简洁描述性强因此我不会深入探讨这一细节。 当您在form/包含此小部件时当用户提交时您将收到两个额外的字段 recaptcha_response_field和recaptcha_challenge_field 。 第一个是用户键入的实际文本第二个是每个请求生成的隐藏令牌。 reCAPTCHA服务器可能会将它用作会话密钥但是我们不在乎我们要做的就是将此字段进一步传递给reCAPTCHA服务器 。 我将使用HttpClient 4对外部服务器执行HTTP请求并在Scala中执行一些巧妙的模式匹配来解析响应 trait ReCaptchaVerifier {def validate(reCaptchaRequest: ReCaptchaSecured): Boolean}Service
class HttpClientReCaptchaVerifier Autowired()(httpClient: HttpClient,servletRequest: HttpServletRequest,Value(${recaptcha_url}) recaptchaUrl: String,Value(${recaptcha_private_key}) recaptchaPrivateKey: String) extends ReCaptchaVerifier {def validate(reCaptchaRequest: ReCaptchaSecured): Boolean {val post new HttpPost(recaptchaUrl)post.setEntity(new UrlEncodedFormEntity(List(new BasicNameValuePair(privatekey, recaptchaPrivateKey),new BasicNameValuePair(remoteip, servletRequest.getRemoteAddr),new BasicNameValuePair(challenge, reCaptchaRequest.recaptchaChallenge),new BasicNameValuePair(response, reCaptchaRequest.recaptchaResponse))))val response httpClient.execute(post)isReCaptchaSuccess(response.getEntity.getContent)}private def isReCaptchaSuccess(response: InputStream) {val responseLines Option(response) map {Source.fromInputStream(_).getLines().toList} getOrElse NilresponseLines match {case true :: _ truecase false :: incorrect-captcha-sol :: _ falsecase false :: msg :: _ throw new ReCaptchaException(msg)case resp throw new ReCaptchaException(Unrecognized response: resp.toList)}}}class ReCaptchaException(msg: String) extends RuntimeException(msg) 唯一缺少的部分是ReCaptchaSecured特性它封装了前面提到的两个reCAPTCHA字段。 为了使用reCAPTCHA保护任何Web表单我只是在扩展此模型 trait ReCaptchaSecured {BeanProperty var recaptchaChallenge BeanProperty var recaptchaResponse
}class NewComment extends ReCaptchaSecured {BeanProperty var name BeanProperty var contents
} 整个CommentsController.scala并不相关。 但是结果是 这样就可以了但是显然不是很壮观。 您如何用Spring Integration替换低级HttpClient调用 ReCaptchaVerifier接口特征保持不变因此不必更改客户端代码。 但是我们将HttpClientReCaptchaVerifier重构为两个单独的较小的相对高级的抽象类 Service
class ReCaptchaFormToHttpRequest Autowired() (servletRequest: HttpServletRequest, Value(${recaptcha_private_key}) recaptchaPrivateKey: String) {def transform(form: ReCaptchaSecured) Map(privatekey - recaptchaPrivateKey,remoteip - servletRequest.getRemoteAddr,challenge - form.recaptchaChallenge,response - form.recaptchaResponse).asJava}Service
class ReCaptchaServerResponseToResult {def transform(response: String) {val responseLines response.split(\n).toListresponseLines match {case true :: _ truecase false :: incorrect-captcha-sol :: _ falsecase false :: msg :: _ throw new ReCaptchaException(msg)case resp throw new ReCaptchaException(Unrecognized response: resp.toList)}}} 请注意我们不再需要实现ReCaptchaVerifier Spring Integration将为我们做到这一点。 我们只需要告诉我们框架应该如何使用上面提取的构建块。 我想我还没有描述Spring Integration是什么以及它是如何工作的。 简而言之它是企业集成模式的非常纯净的实现有人可能将其称为ESB。 消息流是使用XML描述的可以嵌入到标准Spring XML配置中 ?xml version1.0 encodingUTF-8?
beans:beans xmlns:beanshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlnshttp://www.springframework.org/schema/integrationxmlns:httphttp://www.springframework.org/schema/integration/httpxsi:schemaLocationhttp://www.springframework.org/schema/integrationhttp://www.springframework.org/schema/integration/spring-integration.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/integration/httphttp://www.springframework.org/schema/integration/http/spring-integration-http.xsd!-- configuration here -- /beans:beans 在我们的案例中我们将描述从HttpClientReCaptchaVerifier Java接口/ Scala特征到reCAPTCHA服务器再返回的消息流。 在必须将ReCaptchaSecured对象转换为HTTP请求并将HTTP响应转换为有意义的结果的方式上该方法从接口透明返回。 gateway idReCaptchaVerifier service-interfacecom.blogspot.nurkiewicz.recaptcha.ReCaptchaVerifier default-request-channelreCaptchaSecuredForm/channel idreCaptchaSecuredForm datatypecom.blogspot.nurkiewicz.web.ReCaptchaSecured/transformer input-channelreCaptchaSecuredForm output-channelreCaptchaGoogleServerRequest refreCaptchaFormToHttpRequest/channel idreCaptchaGoogleServerRequest datatypejava.util.Map/http:outbound-gatewayrequest-channelreCaptchaGoogleServerRequestreply-channelreCaptchaGoogleServerResponseurl${recaptcha_url}http-methodPOSTextract-request-payloadtrueexpected-response-typejava.lang.String/channel idreCaptchaGoogleServerResponse datatypejava.lang.String/transformer input-channelreCaptchaGoogleServerResponse refreCaptchaServerResponseToResult/ 尽管有大量的XML但是整个消息流还是非常简单的。 首先我们定义网关 它是Java接口和Spring Integration消息流之间的桥梁。 ReCaptchaVerifier.validate()的参数稍后变成一条消息 该消息发送到reCaptchaSecuredForm channel 。 ReCaptchaSecured对象从该通道传递到ReCaptchaFormToHttpRequest 转换器 。 转换器的用途是从ReCaptchaSecured对象到Java映射的两次转换代表一组键值对。 稍后此映射通过reCaptchaGoogleServerRequest通道传递到http:outbound-gateway 。 该组件的职责是将先前创建的地图转换为HTTP请求并将其发送到指定的地址。 响应返回时将其发送到reCaptchaGoogleServerResponse通道。 ReCaptchaServerResponseToResult转换器将采取行动将HTTP响应转换为业务结果布尔值。 最终转换器结果被路由回网关。 默认情况下所有操作都是同步发生的因此我们仍然可以使用简单的Java接口进行reCAPTCHA验证。 信不信由你这一切正常。 我们不再使用HttpClient 猜测一切都比HttpClient 4 API更好……而不是一个“巨大”的类我们有一组较小的集中的易于测试的类。 该框架处理接线和底层细节。 精彩 建筑师的梦想还是开发商的噩梦 让我通过引用以上介绍的结论来总结我们的努力在架构利益与开发有效性之间取得平衡 。 Spring Integration能够从各种异构源如JMS关系数据库甚至FTP接收数据以多种方式聚合拆分解析和过滤消息最后使用最奇特的协议进一步发送消息。 手工编写所有代码是一项非常繁琐且容易出错的任务。 另一方面有时我们只是不需要所有的幻想而弄脏我们的手例如通过执行手动HTTP请求并解析响应则更加简单易懂。 在盲目地将整个体系结构基于非常高级的抽象或基于手工编码的低级过程之前请考虑一下后果和平衡。 没有解决方案可以解决所有问题。 您发现哪个版本的reCAPTCHA集成更好 参考资料 使用…与reCAPTCHA集成... Java社区博客上的JCG合作伙伴 Tomasz Nurkiewicz的Spring Integration 。 翻译自: https://www.javacodegeeks.com/2012/05/spring-integration-with-recaptcha.html