package com.vv.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;
import com.vv.common.GenRandomCode;
import com.vv.common.RedisMgr;
@Controller
public class getImageCtl {
private static final Logger _log = LoggerFactory.getLogger("accpet");
private static int TIMEOUTSCENDS =3*60;
private static String UserFiex= "IMG_VERFIY";
@RequestMapping("/getimage.htm")
public void getimage(HttpServletRequest request,HttpServletResponse response)throws Exception
{
String code = this.showVerifyCode(request,response);
String uuid = utfStr(request.getParameter("uuid"));
_log.info("getimage get parameter uuid = " + uuid );
Jedis jedis = null;
try{
jedis =RedisMgr.getResource();
String key = UserFiex+"_"+uuid;
_log.info("getimage key = " + key );
jedis.setex(UserFiex+"_"+uuid, TIMEOUTSCENDS,code);
} catch (Throwable ce) {
RedisMgr.returnBrokenJedis(jedis);
} finally {
RedisMgr.returnResource(jedis);
}
}
@RequestMapping("/verify.htm")
@ResponseBody
public String verify(HttpServletRequest request,HttpServletResponse response)throws Exception
{
String uuid = utfStr(request.getParameter("uuid"));
String code = utfStr(request.getParameter("code"));
String back= "-1";
Jedis jedis = null;
try{
jedis =RedisMgr.getResource();
String codeBak = null;
String key = UserFiex+"_"+uuid;
_log.info("verify key = " + key );
codeBak = jedis.get(key);
if(key==null||code==null)
{
back = "-3";//参数错误
}
else if(codeBak==null)
{
back = "-2";//无效验证码 :uuid没有对应验证码,验证码失效,验证已被验证正确过
}
else if(key!=null && code != null && codeBak != null && code.equalsIgnoreCase(codeBak))
{
jedis.del(key);
back = "1";//正确
}
else {
back = "-1";//验证错误
}
} catch (Throwable ce) {
RedisMgr.returnBrokenJedis(jedis);
} finally {
RedisMgr.returnResource(jedis);
}
return back;
}
/**
*
* jsonp访问接口
验证图片
地址:/verify2.htm
参数:uuid:校验key //uuid
code:待校验字符串
jsonp:jsonpCallback //必填参数
返回:
1:正确
-1:错误
-2:无效验证码 :uuid对应验证码失效,uuid对应验证已被验证正确过,uuid没有对应验证码
-3:参数错误
超时时间为3分钟
e.g.
$.ajax({
type:"get",
url:"http://verify.im.baidu.com/verify2.htm",
data:{"uuid":uuid,"code":v},
dataType:"jsonp",
jsonp: "jsonpCallback",//必填 且必须是jsonpCallback
jsonpCallback:"trest",//自定义任意
async:false,
success:function(data){
alert(data); // -1 -2 -3 1
},
error:function(data){
alert("error");
}
});
*
*
* @param request
* @param response
* @throws IOException
*/
@RequestMapping("/verify2.htm")
public void showVerifyCodeForJsonp( HttpServletRequest request,HttpServletResponse response)throws IOException
{
response.setContentType("text/plain");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
String jsonpCallback = request.getParameter("jsonpCallback");//客户端请求参数
String uuid = utfStr(request.getParameter("uuid"));
String code = utfStr(request.getParameter("code"));
String back= "-1";
Jedis jedis = null;
try{
jedis =RedisMgr.getResource();
String codeBak = null;
String key = UserFiex+"_"+uuid;
_log.info("verify key = " + key );
codeBak = jedis.get(key);
if(key==null||code==null)
{
back = "-3";//参数错误
}
else if(codeBak==null)
{
back = "-2";//无效验证码 :uuid没有对应验证码,验证码失效,验证已被验证正确过
}
else if(key!=null && code != null && codeBak != null && code.equalsIgnoreCase(codeBak))
{
jedis.del(key);
back = "1";//正确
}
else {
back = "-1";//验证错误
}
} catch (Throwable ce) {
RedisMgr.returnBrokenJedis(jedis);
} finally {
RedisMgr.returnResource(jedis);
}
PrintWriter out = response.getWriter();
back = jsonpCallback+"("+back+")";
out.write(back);
}
private String showVerifyCode( HttpServletRequest request,HttpServletResponse response) throws IOException
{
GenRandomCode verifyCode = new GenRandomCode();
verifyCode.createCode();
String code = verifyCode.getCode();
_log.info(" verify code = " + code );
response.setContentType("image/png");
verifyCode.write(response.getOutputStream());
return code;
}
public String utfStr(String str)
{
if(str!=null)
{
try
{
URLEncoder.encode(str, "utf-8");
return str;
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
}
return null;
}
}
ajax跨域调用
最新推荐文章于 2021-08-05 22:27:46 发布