cowboy添加验证码
参考的http://beebole.com/blog/erlang/how-to-implement-captcha-in-erlang-web-application/,移到cowboy,修改了下;废话不多说,直接贴代码
注意,需要cowboy版本1.0.1
需要imagemagick
sudo apt-get install imagemagick
mac
brew install imagemagick
mac下如果使用convert命令找不到字体,则需要安装下这个
brew install ghostscript
创建工程
rebar-creator create-app testCaptcha
testCaptcha_app
-module(testCaptcha_app).
-behaviour(application).
-export([start/, stop/]).
-define(C_ACCEPTORS, ).
start(_StartType, _StartArgs) ->
simple_captcha_ets:init(),
application:start(crypto),
application:start(cowlib),
application:start(ranch),
application:start(cowboy),
Routes = route_helper:get_routes(),
Dispatch = cowboy_router:compile(Routes),
Port = ,
TransOpts = [{port, Port}],
ProtoOpts = [{env, [{dispatch, Dispatch}]}],
cowboy:start_http(http, ?C_ACCEPTORS, TransOpts, ProtoOpts).
stop(_State) ->
simple_captcha_ets:destroy(),
ok.
route_helper
-module(route_helper). -export([get_routes/]). get_routes() ->
[
{'_', [
{"/captcha", captcha_handler, []},
{"/captcha_check", captcha_check_handler, []}
]}
].
simple_captcha
-module(simple_captcha). -export([create/,check/]). create() ->
CryptKey = mochihex:to_hex(crypto:rand_bytes()),
Code = generate_rand(), FileName = lists:flatmap(fun(Item) -> integer_to_list(Item) end, tuple_to_list(now())),
File = io_lib:format("/tmp/~s.png",[FileName]), Cmd = io_lib:format("convert -background 'none' -fill '#222222' -size 175 -gravity Center -wave 5x100 -swirl 50 -font DejaVu-Serif-Book -pointsize 28 label:~s -draw 'Bezier 10,40 50,35 100,35 150,35 200,50 250,35 300,35' ~s", [Code, File]),
os:cmd(Cmd), {ok, BinPng} = file:read_file(File),
file:delete(File), simple_captcha_ets:insert(Code,CryptKey), {erlang:list_to_bitstring(CryptKey),BinPng}. check(CryptKeyBitString,CodeBitString) ->
CryptKey = erlang:bitstring_to_list(CryptKeyBitString),
Code = erlang:bitstring_to_list(CodeBitString),
CryptKeyFromEts = simple_captcha_ets:find(Code), case string:equal(CryptKeyFromEts,CryptKey) of
true ->
simple_captcha_ets:remove(code),
true;
_ ->
false
end. %private
generate_rand(Length) ->
Now = now(),
random:seed(element(, Now), element(, Now), element(, Now)),
lists:foldl(fun(_I, Acc) -> [do_rand() | Acc] end, [], lists:seq(, Length)). do_rand(R) when R > , R < ; R > , R < ; R > ->
R; do_rand(_R) ->
do_rand( + random:uniform()).
simple_captcha_ets
-module(simple_captcha_ets). -export([init/,insert/,find/,remove/,destroy/]). init()->
TableName = ets:new(simple_captcha, [public,named_table]),
{ok, TableName}. insert(Key,Value) ->
ets:insert(simple_captcha, {Key, Value}),
ok. find(Key) ->
Result = (
try ets:lookup_element(simple_captcha, Key, ) of
Value ->
Value
catch
_:_ ->
""
end
),
Result. remove(Key) ->
try ets:delete(simple_captcha, Key)
catch
_:_ ->
ok
end. destroy()->
try ets:delete(simple_captcha)
catch
_:_ ->
ok
end.
mochihex
自己下载
captcha_handler
-module(captcha_handler). -export([init/]).
-export([handle/]).
-export([terminate/]). init(_Transport, Req, []) ->
{ok, Req, undefined}. handle(Req, State) ->
%CryptKey用于验证的时候用,需本地保存,CapCode为用户提交的数据
%simple_captcha:check(CryptKey, CapCode)
{CryptKey,BinPng} = simple_captcha:create(), Req2 = cowboy_req:set_resp_cookie(<<"cap">>, CryptKey, [{path, <<"/">>}], Req),
{ok, Req3} = cowboy_req:reply(, [{<<"content-type">>, <<"image/png">>}],BinPng, Req2),
{ok, Req3, State}. terminate(_Reason, _Req, _State) ->
ok.
captcha_check_handler
-module(captcha_check_handler). -export([init/]).
-export([handle/]).
-export([terminate/]). init(_Transport, Req, []) ->
{ok, Req, undefined}. handle(Req, State) ->
{CryptKey,_} = cowboy_req:cookie(<<"cap">>, Req,<<"">>),
{ok, PostVals, Req2} = cowboy_req:body_qs(Req),
CaptchaCode = proplists:get_value(<<"captchaCode">>, PostVals), case simple_captcha:check(CryptKey, CaptchaCode) of
true ->
{ok, Req3} = cowboy_req:reply(, [{<<"content-type">>, <<"text/html">>}],<<"ok">>, Req2),
{ok, Req3, State};
_ ->
{ok, Req3} = cowboy_req:reply(, [{<<"content-type">>, <<"text/html">>}],<<"error">>, Req2),
{ok, Req3, State}
end. terminate(_Reason, _Req, _State) ->
ok.
rebar.config
% -*- erlang -*-
{erl_opts, [debug_info]}.
{deps, [
{cowboy,".*", {git, "https://github.com/ninenines/cowboy", {tag,"1.0.1"}}}
]}.
{cover_enabled, true}. {eunit_opts, [verbose, {report,{eunit_surefire,[{dir,"."}]}}]}.
{sub_dirs, ["apps/testCaptcha", "rel"]}.
run.sh
#!/bin/sh
rebar clean;
rebar compile;
erl -pa apps/*/ebin deps/*/ebin -eval "application:start(testCaptcha)."
captcha_test.html
<img src="http://127.0.0.1:8080/captcha" width="" height=""> <form action="http://127.0.0.1:8080/captcha_check" method="post">
<p>captcha<input type="text" name="captchaCode" /></p>
<input type="submit" value="Submit" />
</form>
收工
cowboy添加验证码的更多相关文章
- asp.net添加验证码
1.新建一个aspx页面生成验证码图像 using System; using System.Data; using System.Configuration; using System.Collec ...
- PHPCMS v9 自定义表单添加验证码验证
1. 在 \phpcms\templates\default\formguide\show.html 中添加验证码显示 <input type="text" id=" ...
- Angular企业级开发(9)-前后端分离之后添加验证码
1.背景介绍 团队开发的项目,前端基于Bootstrap+AngularJS,后端Spring MVC以RESTful接口给前端调用.开发和部署都是前后端分离.项目简单部署图如下,因为后台同时采用微服 ...
- PHPCMS v9 自定义表单添加验证码
1. 在 \phpcms\templates\default\formguide\show.html 中添加验证码显示 <input type="text" id=&quo ...
- cas4.2.4 登添加验证码
看了很多添加验证码的博文,唯独没有4.24的 重点看第3条,其余的和别人博文大致相同 1.首先在cas工程的web.xml增加验证码功能的支持 <!-- 验证码功能 --> &l ...
- [phpcms v9]自定义表单添加验证码验证功能
修改 \phpcms\templates\default\formguide\show.html 中添加验证码显示 <input type="text" id=" ...
- 【转】PHPCMS v9 自定义表单添加验证码验证
1. 在 \phpcms\templates\default\formguide\show.html 中添加验证码显示 <input type="text" id=&quo ...
- C# DateTime的11种构造函数 [Abp 源码分析]十五、自动审计记录 .Net 登陆的时候添加验证码 使用Topshelf开发Windows服务、记录日志 日常杂记——C#验证码 c#_生成图片式验证码 C# 利用SharpZipLib生成压缩包 Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库
C# DateTime的11种构造函数 别的也不多说没直接贴代码 using System; using System.Collections.Generic; using System.Glob ...
- cas添加验证码
cas添加验证码,折腾了好久,终于整理好了,很大部分都是借鉴http://binghejinjun.iteye.com/blog/1255293这个的.但是他的有一个很不好的地方就是不能提升验证码错误 ...
随机推荐
- nvm命令行操作命令
1,nvm nvm list 是查找本电脑上所有的node版本 - nvm list 查看已经安装的版本 - nvm list installed 查看已经安装的版本 - nvm list avail ...
- Duilib嵌入CEF禁止浏览器响应拖拽事件
转载:http://blog.csdn.net/liuyan20092009/article/details/53819473 转载:https://blog.csdn.net/u012778714( ...
- jz2440-uboot-201204版本移植【学习笔记】【原创】
平台:jz2440 作者:庄泽彬(欢迎转载,请注明作者) 说明:韦东山二期视频学习笔记 交叉编译工具:arm-linux-gcc (GCC)4.3.2 PC环境:ubuntu18.04 一.uboot ...
- NOIP 2018 兔纸旅游记
今年是第一次参加tg呢... Day0 早上出发去中旅坐大巴,走有 lz 特色的OI比赛道路. 车上谈笑风生,看 jw 的 GDOI 的小本本. 到动车站取票入站,看 lmh 和 zn 的爱恨情 ...
- [原][译][osgearth]Model Source Drivers模型驱动源(OE官方文档翻译)
ModelSource 是一个能生成OpenSceneGraph节点的驱动(driver) osgEarth使用ModelSources显示矢量特征(feature)数据和加载和显示外部3d模型 Fe ...
- hive row_number等窗口分析函数
一.排序&去重分析 row_number() over(partititon by col1 order by col2) as rn 结果:1,2,3,4 rank() over(parti ...
- Django配置让其他电脑访问网站(包括:修改IP和端口)
http://blog.sina.com.cn/s/blog_9c5364110101fyk7.html
- 10个有趣的Javascript和CSS库
Tailwind CSS Tailwind是用于构建自定义用户界面的实用CSS框架. 每个Tailwind小应用都有多种尺寸,这使得创建响应式界面变得非常简单. 您可以自定义颜色,边框尺寸,字体,阴影 ...
- Xmind8 (update8)破解教程
环境:Win7 一.下载xmind: Xmind版本:xmind-8-update8-windows.exe 百度云盘地址: https://pan.baidu.com/s/1ccd18E1hOY1 ...
- bzoj1179: [Apio2009]Atm scc缩点+dag上dp
先把强连通缩点,然后变成了dag,dp求终点是酒吧的最长路即可, /************************************************************** Pro ...