[java]代码库
package com.liang.bike.controller.login;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.alibaba.fastjson.JSONObject;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.liang.bike.bean.base.UserBase;
import com.liang.bike.service.rent.IRentBikeService;
import com.liang.bike.service.user.IUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import java.awt.image.BufferedImage;
import java.io.IOException;
/**
* Created by Administrator on 2017/3/2.
*/
@Controller
@RequestMapping("/login")
public class LoginController {
@Resource
private Producer kaptchaProducer;
@Autowired
private IRentBikeService rentBikeService;
@Autowired
private IUserService userService;
/**
* 跳转到登录页面
*
* @return login.jsp
*/
@RequestMapping(value = "/loginTurn")
public String loginTurn() {
return "user/login";
}
/**
* 获取验证码图片
*
* @param request
* @param response
* @return
* @throws java.io.IOException Created 2017年1月17日 下午5:07:28
* @author ccg
*/
@RequestMapping("/getkaptchaCode")
public ModelAndView getKaptchaCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
HttpSession session = request.getSession();
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");
response.setContentType("image/jpeg");
//生成验证码文本
String capText = kaptchaProducer.createText();
session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
System.out.println("生成验证码文本====" + capText);
//利用生成的字符串构建图片
BufferedImage bi = kaptchaProducer.createImage(capText);
ServletOutputStream out = response.getOutputStream();
ImageIO.write(bi, "jpg", out);
try {
out.flush();
} finally {
out.close();
}
return null;
}
/**
* check:ajax异步校验验证码. <br/>
*
* @param request
* @return 校验结果
* @author
*/
@RequestMapping(value = "/check", method = RequestMethod.POST)
@ResponseBody
public JSONObject loginCheck(HttpServletRequest request, String kaptcha) {
JSONObject json = new JSONObject();
boolean kaptchaFlag = this.checkKaptcha(request, kaptcha);
json.put("kaptchaFlag", kaptchaFlag);
return json;
}
//校验验证码是否正确
public boolean checkKaptcha(HttpServletRequest request, String kaptcha) {
String kaptchaExpected = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
System.out.println("kaptcha = [" + kaptcha + "]");
boolean kaptchaFlag = false;
if (kaptcha != null && kaptcha.equals(kaptchaExpected)) {
kaptchaFlag = true;
}
return kaptchaFlag;
}
/**
* login:ajax异步校验登录请求. <br/>
*
* @param request
* @return 校验结果
* @author
*/
@RequestMapping(value = "/login", method = RequestMethod.POST)
@ResponseBody
public JSONObject login(/*@RequestParam(value = "file", required = false) MultipartFile file,*/ HttpServletRequest request, String userPhone, String kaptcha) {
//String fileName = file.getOriginalFilename();
String path = request.getSession().getServletContext().getRealPath("login");
//System.out.println(path + fileName);
JSONObject json = new JSONObject();
boolean kaptchaFlag = this.checkKaptcha(request, kaptcha);
json.put("kaptchaFlag", kaptchaFlag);
if (kaptchaFlag) {
UserBase userBase = new UserBase();
userBase.setUserPhone(userPhone);
UserBase user = rentBikeService.selectUserByUserPhone(userBase);
boolean userFlag = false;
if (user != null) {
userFlag = true;
request.getSession().setAttribute("user", user);
SecurityUtils.getSubject().login(new UsernamePasswordToken(user.getUserPhone(), user.getPassword()));
}
json.put("userFlag", userFlag);
}
return json;
}
/**
* 跳转到注册页面
*
* @return login.jsp
*/
@RequestMapping(value = "/registerTurn")
public String registerTurn() {
return "user/register";
}
/**
* login:ajax异步校验注册请求. <br/>
*
* @param request
* @return 校验结果
* @author
*/
@RequestMapping(value = "/register", method = RequestMethod.POST)
@ResponseBody
public JSONObject register(HttpServletRequest request, UserBase userBase, String kaptcha) {
JSONObject json = new JSONObject();
//校验验证码
Boolean kaptchaFlag = this.checkKaptcha(request, kaptcha);
json.put("kaptchaFlag", kaptchaFlag);
if (!kaptchaFlag) {
return json;
}
//校验手机号是否存在
UserBase user = rentBikeService.selectUserByUserPhone(userBase);
boolean userFlag = Boolean.FALSE;
if (user != null) {
json.put("userFlag", userFlag);
return json;
}
userFlag = Boolean.TRUE;
json.put("userFlag", userFlag);
//注册
Boolean registerFlag = Boolean.FALSE;
int i = userService.insertSelective(userBase);
if (i == 0) {
json.put("registerFlag", registerFlag);
return json;
}
registerFlag = Boolean.TRUE;
json.put("registerFlag", registerFlag);
return json;
}
}
by: 发表于:2017-12-28 14:02:24 顶(0) | 踩(0) 回复
??
回复评论