{"id":2026,"date":"2016-05-18T10:41:15","date_gmt":"2016-05-18T02:41:15","guid":{"rendered":"http:\/\/boweihe.me\/?p=2026"},"modified":"2016-05-18T10:41:15","modified_gmt":"2016-05-18T02:41:15","slug":"%e5%9c%a8springmvc-4-%e4%b8%ad%e7%94%a8%e6%b3%a8%e9%87%8aannotation%e6%96%b9%e5%bc%8f%e4%bd%bf%e7%94%a8-google-kaptcha-captcha","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2026","title":{"rendered":"\u5728SpringMVC 4 \u4e2d\u7528\u6ce8\u91ca(Annotation)\u65b9\u5f0f\u4f7f\u7528 Google Kaptcha (Captcha)"},"content":{"rendered":"<blockquote><p>\u539f\u521b\u5185\u5bb9\uff0c\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u6765\u81eahttp:\/\/boweihe.me\/?p=2026<\/p><\/blockquote>\n<p>\u8fd1\u671f\u5728\u52aa\u529b\u5b66\u4e60SpringMVC\uff0c\u56e0\u4e3a\u4e4b\u524d\u5bf9JSP\u4e5f\u662f\u4e00\u77e5\u534a\u89e3\u7684\uff0c\u5e72\u8106\u62ff\u4e86\u672c\u300aSpring in Action\u300b(4th edition)\u5543\uff0c\u53d1\u73b0\u4f7f\u7528\u6ce8\u91ca\u7684\u65b9\u5f0f\u6bd4\u7528xml\u6765\u7684\u6709\u610f\u601d\u4e00\u4e9b\u3002\u7f51\u7ad9\u4e2d\u8981\u7528\u5230\u9a8c\u8bc1\u7801\uff0c\u76ee\u524d\u80fd\u627e\u5230\u7684\u6587\u6863\u90fd\u662f\u7528xml\u914d\u7f6e\u7684\uff0c\u611f\u89c9\u6709\u70b9\u4e0d\u723d\uff0c\u51b3\u5b9a\u5b66\u6211\u515a\u201c\u6478\u7740\u77f3\u5934\u8fc7\u6cb3\u201d\u4e00\u6b21\uff0c\u5e0c\u671b\u4e0d\u8981naive\u4e86..<\/p>\n<h3>Maven\u5e93<\/h3>\n<p>Google\u81ea\u5bb6\u7684\u5e93\u4f30\u8ba1\u662f\u6ca1\u4e86\uff0c\u5927\u6982\u662f\u65f6\u95f4\u592a\u4e45\u8fdc\u4e86\u5427\u3002\u6211\u7528\u7684Maven\u5e93\u662f\u8fd9\u4e2a<\/p>\n<pre class=\"lang:default decode:true\">&lt;dependency&gt;\n            &lt;groupId&gt;com.github.penggle&lt;\/groupId&gt;\n            &lt;artifactId&gt;kaptcha&lt;\/artifactId&gt;\n            &lt;version&gt;2.3.2&lt;\/version&gt;\n&lt;\/dependency&gt;<\/pre>\n<h3>Bean\u914d\u7f6e\u6587\u4ef6<\/h3>\n<p>\u9700\u8981\u628a\u539f\u6709\u7684applicationContext.xml\u7528\u6ce8\u91ca\u7684\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5176\u5b9e\u5c31\u662f\u8ba9Spring\u627e\u5230\u4e2a\u53ef\u4ee5\u7528\u7684Bean\u5e76\u52a0\u8f7d\u76f8\u5173\u914d\u7f6e\u3002<br \/>\n&nbsp;<br \/>\n\u6211\u6784\u9020\u4e86\u4e00\u4e2a\u7c7b\u6765\u641e\u5b9a\u8fd9\u4e2a\u4e8b\u60c5\uff0c\u91cc\u9762\u7684\u914d\u7f6e\u53c2\u6570\u4f1a\u4eceapplication.properties\u6587\u4ef6\u4e2d\u8bfb\u53d6\uff0c\u6211\u4ec5\u4ec5\u5b9e\u73b0\u4e86\u6211\u9700\u8981\u7684\u51e0\u4e2a\u53c2\u6570\uff0c\u53cd\u6b63\u5982\u679c\u8981\u52a0\u7684\u8bdd\u5c31\u5728kaptchaProperties\u65b9\u6cd5\u91cc\u9762\u5199\u5c31\u53ef\u4ee5\uff0c\u7136\u540eAutowire\u4e00\u4e2aEnvironment\u7528\u6765\u8bfb\u53d6\u6587\u4ef6\u914d\u7f6e\u53c2\u6570\u3002\u4e3b\u8981\u662f<span class=\"lang:java decode:true crayon-inline \">@Configuration<\/span>\u00a0\u6ce8\u91ca\uff0c\u8fd9\u4e2a\u662f\u544a\u8bc9Spring\u6211\u662f\u4e2a\u914d\u7f6e\u7c7b\uff0c\u8fd9\u4e2a\u8fd8\u662f\u4eceHibernate\u914d\u7f6e\u4e2d\u5b66\u8fc7\u6765\u7684\uff0c\u54c8\u54c8\u3002<br \/>\n\u58f0\u660e\u51fa\u6765\u7684\u8fd9\u4e2a<span class=\"lang:java decode:true crayon-inline \">@Bean(name = &#8220;captchaProducer&#8221;)<\/span>\u00a0\u5c31\u662f\u540e\u9762Controller\u91cc\u9762\u8981\u7528\u7684\u4e86<\/p>\n<pre class=\"lang:java decode:true\" title=\"KaptchaConfig.java\">package edu.inlab.config;\nimport com.google.code.kaptcha.impl.DefaultKaptcha;\nimport com.google.code.kaptcha.util.Config;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\nimport org.springframework.context.annotation.PropertySource;\nimport org.springframework.core.env.Environment;\nimport java.util.Properties;\n\/**\n * Created by inlab-dell on 2016\/5\/17.\n *\/\n@Configuration\n@PropertySource(value = {\"classpath:application.properties\"})\npublic class KaptchaConfig {\n    @Autowired\n    private Environment environment;\n    private DefaultKaptcha kaptcha;\n    @Bean(name = \"captchaProducer\")\n    public DefaultKaptcha getKaptchaProducer(){\n        if(null == kaptcha) {\n            kaptcha = new DefaultKaptcha();\n            kaptcha.setConfig(getKaptchaConfig());\n        }\n        return kaptcha;\n    }\n    @Bean\n    public Config getKaptchaConfig(){\n        return new Config(kaptchaProperties());\n    }\n    private Properties kaptchaProperties(){\n        Properties properties = new Properties();\n        properties.put(\"kaptcha.image.width\",\n                environment.getRequiredProperty(\"kaptcha.image.width\"));\n        properties.put(\"kaptcha.image.height\",\n                environment.getRequiredProperty(\"kaptcha.image.height\"));\n        properties.put(\"kaptcha.textproducer.char.string\",\n                environment.getRequiredProperty(\"kaptcha.textproducer.char.string\"));\n        properties.put(\"kaptcha.textproducer.char.length\",\n                environment.getRequiredProperty(\"kaptcha.textproducer.char.length\"));\n        return properties;\n    }\n}\n<\/pre>\n<p>\u5bf9\u5e94\u7684application.properties\uff0c\u5177\u4f53\u542b\u4e49\u53ef\u4ee5\u770b\u53c2\u8003\u6587\u732e\u91cc\uff0c\u89e3\u91ca\u7684\u5f88\u6e05\u695a\u4e86\u3002<\/p>\n<pre class=\"lang:default decode:true\">kaptcha.image.width = 200\nkaptcha.image.height = 50\nkaptcha.textproducer.char.string = ABCDEFGHKLMNPQRSTWXY3456789\nkaptcha.textproducer.char.length = 6<\/pre>\n<h3>CaptchaController \u63a7\u5236\u5668<\/h3>\n<p>\u9879\u76ee\u5728\u5b9e\u4e60\u524d\u6015\u662f\u8d76\u4e0d\u5b8c\u6765\u4e86\uff0c\u6211\u8fd8\u662f\u5c11\u82b1\u70b9\u65f6\u95f4\u5199\u535a\u5ba2\u5427\u3002<br \/>\n\u63a7\u5236\u5668\u7684\u5b9e\u73b0Wiki\u4e0a\u90fd\u6709\uff0c\u4e3b\u8981\u7684\u662f\u8981@Autowire\u4e4b\u524d\u6211\u4eec\u505a\u597d\u7684\u90a3\u4e2aBean\uff0c\u5927\u6982\u662f\u8fd9\u6837\u3002\u5176\u5b9e\u5c31\u662f\u4e2a\u57fa\u4e8eset\u65b9\u6cd5\u7684\u6ce8\u5165\u561b~<\/p>\n<pre class=\"lang:java decode:true\">@Controller\n@RequestMapping(\"\/captcha\")\npublic class CaptchaController {\n    private Producer captchaProducer;\n    @Autowired\n    public void setCaptchaProducer(Producer captchaProducer) {\n        this.captchaProducer = captchaProducer;\n    }\n    @RequestMapping(method = RequestMethod.GET)\n    public ModelAndView handleRequest(HttpServletRequest request,\n                  HttpServletResponse response) throws Exception{\n        \/\/ ...\n    }\n}\n<\/pre>\n<p>&nbsp;<\/p>\n<h3>\u53c2\u8003\u6587\u732e\uff1a<\/h3>\n<ol>\n<li><a href=\"http:\/\/my.oschina.net\/u\/1450300\/blog\/486377?fromerr=JzOuKUXD\" target=\"_blank\" rel=\"noopener noreferrer\">\u5728springmvc\u9879\u76ee\u4e2d\u4f7f\u7528kaptcha\u751f\u6210\u9a8c\u8bc1\u7801<\/a><\/li>\n<li><a href=\"http:\/\/blog.csdn.net\/gao36951\/article\/details\/48261003\" target=\"_blank\" rel=\"noopener noreferrer\">\u7b80\u5355Maven\u7684Web\u9879\u76ee\u4e4b\u9a8c\u8bc1\u7801(Kaptcha\u7bc7)<\/a><\/li>\n<li><a href=\"http:\/\/blog.csdn.net\/gao36951\/article\/details\/48261003\" target=\"_blank\" rel=\"noopener noreferrer\">Spring mvc\u6846\u67b6\u4e0b\u4f7f\u7528kaptcha\u751f\u6210\u9a8c\u8bc1\u7801<\/a><\/li>\n<li><a href=\"https:\/\/code.google.com\/archive\/p\/kaptcha\/wikis\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/code.google.com\/archive\/p\/kaptcha\/wikis<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u521b\u5185\u5bb9\uff0c\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u6765\u81eahttp:\/\/boweihe.me\/?p=2026 \u8fd1\u671f\u5728\u52aa\u529b\u5b66\u4e60SpringMVC\uff0c\u56e0\u4e3a\u4e4b\u524d\u5bf9JSP\u4e5f\u662f\u4e00\u77e5\u534a\u89e3\u7684\uff0c\u5e72\u8106\u62ff\u4e86\u672c\u300aSpring in Action\u300b(4th edition)\u5543\uff0c\u53d1\u73b0\u4f7f\u7528\u6ce8\u91ca\u7684\u65b9\u5f0f\u6bd4\u7528xml\u6765\u7684\u6709\u610f\u601d\u4e00\u4e9b\u3002\u7f51\u7ad9\u4e2d\u8981\u7528\u5230\u9a8c\u8bc1\u7801\uff0c\u76ee\u524d\u80fd\u627e\u5230\u7684\u6587\u6863\u90fd\u662f\u7528xml\u914d\u7f6e\u7684\uff0c\u611f\u89c9\u6709\u70b9\u4e0d\u723d\uff0c\u51b3\u5b9a\u5b66\u6211\u515a\u201c\u6478\u7740\u77f3\u5934\u8fc7\u6cb3\u201d\u4e00\u6b21\uff0c\u5e0c\u671b\u4e0d\u8981naive\u4e86.. Maven\u5e93 Google\u81ea\u5bb6\u7684\u5e93\u4f30\u8ba1\u662f\u6ca1\u4e86\uff0c\u5927\u6982\u662f\u65f6\u95f4\u592a\u4e45\u8fdc\u4e86\u5427\u3002\u6211\u7528\u7684Maven\u5e93\u662f\u8fd9\u4e2a &lt;dependency&gt; &lt;groupId&gt;com.github.penggle&lt;\/groupId&gt; &lt;artifactId&gt;kaptcha&lt;\/artifactId&gt; &lt;version&gt;2.3.2&lt;\/version&gt; &lt;\/dependency&gt; Bean\u914d\u7f6e\u6587\u4ef6 \u9700\u8981\u628a\u539f\u6709\u7684applicationContext.xml\u7528\u6ce8\u91ca\u7684\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5176\u5b9e\u5c31\u662f\u8ba9Spring\u627e\u5230\u4e2a\u53ef\u4ee5\u7528\u7684Bean\u5e76\u52a0\u8f7d\u76f8\u5173\u914d\u7f6e\u3002 &nbsp; \u6211\u6784\u9020\u4e86\u4e00\u4e2a\u7c7b\u6765\u641e\u5b9a\u8fd9\u4e2a\u4e8b\u60c5\uff0c\u91cc\u9762\u7684\u914d\u7f6e\u53c2\u6570\u4f1a\u4eceapplication.properties\u6587\u4ef6\u4e2d\u8bfb\u53d6\uff0c\u6211\u4ec5\u4ec5\u5b9e\u73b0\u4e86\u6211\u9700\u8981\u7684\u51e0\u4e2a\u53c2\u6570\uff0c\u53cd\u6b63\u5982\u679c\u8981\u52a0\u7684\u8bdd\u5c31\u5728kaptchaProperties\u65b9\u6cd5\u91cc\u9762\u5199\u5c31\u53ef\u4ee5\uff0c\u7136\u540eAutowire\u4e00\u4e2aEnvironment\u7528\u6765\u8bfb\u53d6\u6587\u4ef6\u914d\u7f6e\u53c2\u6570\u3002\u4e3b\u8981\u662f@Configuration\u00a0\u6ce8\u91ca\uff0c\u8fd9\u4e2a\u662f\u544a\u8bc9Spring\u6211\u662f\u4e2a\u914d\u7f6e\u7c7b\uff0c\u8fd9\u4e2a\u8fd8\u662f\u4eceHibernate\u914d\u7f6e\u4e2d\u5b66\u8fc7\u6765\u7684\uff0c\u54c8\u54c8\u3002 \u58f0\u660e\u51fa\u6765\u7684\u8fd9\u4e2a@Bean(name = &#8220;captchaProducer&#8221;)\u00a0\u5c31\u662f\u540e\u9762Controller\u91cc\u9762\u8981\u7528\u7684\u4e86 package edu.inlab.config; import com.google.code.kaptcha.impl.DefaultKaptcha; import com.google.code.kaptcha.util.Config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; import java.util.Properties; \/** * Created by inlab-dell on 2016\/5\/17. *\/ @Configuration @PropertySource(value = {&#8220;classpath:application.properties&#8221;}) public class KaptchaConfig { @Autowired private [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,8],"tags":[60,99,101],"class_list":["post-2026","post","type-post","status-publish","format-standard","hentry","category-study","category-technical","tag-java","tag-spring","tag-springmvc"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2026","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2026"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2026\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}