用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月 忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

登陆controller:LoginController

2017-03-07 作者:知行合一心 举报

[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;
    }
}


分享到:
更多

网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。