首页>代码>介绍一款国内java免费开源CMS系统框架--师说CMS>/CMS/src/main/java/com/shishuo/cms/action/AuthAction.java
/*
 *	Copyright © 2013 Changsha Shishuo Network Technology Co., Ltd. All rights reserved.
 *	长沙市师说网络科技有限公司 版权所有
 *	http://www.shishuo.com
 */

package com.shishuo.cms.action;

import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.validator.routines.EmailValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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 com.google.code.kaptcha.impl.DefaultKaptcha;
import com.shishuo.cms.constant.SystemConstant;
import com.shishuo.cms.entity.vo.JsonVo;
import com.shishuo.cms.service.AdminService;
import com.shishuo.cms.util.HttpUtils;

/**
 * @author Herbert
 * 
 */

@Controller
@RequestMapping("/auth")
public class AuthAction extends BaseAction {

	/**
	 * Kaptcha 验证码
	 */
	@Autowired
	private DefaultKaptcha captchaProducer;

	@Autowired
	private AdminService adminService;

	@RequestMapping(value = "admin/login.htm", method = RequestMethod.GET)
	public String adminLogin(HttpServletRequest request, ModelMap modelMap) {
		return "system/login";
	}

	@RequestMapping(value = "admin/logout.htm", method = RequestMethod.GET)
	public String adminLogout(HttpServletRequest request, ModelMap modelMap) {
		request.getSession().removeAttribute(SystemConstant.SESSION_ADMIN);
		return "redirect:" + HttpUtils.getBasePath(request);
	}

	@ResponseBody
	@RequestMapping(value = "admin/login.json", method = RequestMethod.POST)
	public JsonVo<String> adminLogin(
			@RequestParam(value = "email") String email,
			@RequestParam(value = "password") String password,
			@RequestParam(value = "captcha") String captcha,
			HttpServletRequest request, ModelMap modelMap) {
		JsonVo<String> json = new JsonVo<String>();

		try {
			String kaptcha = (String) request.getSession().getAttribute(
					com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
			if (!EmailValidator.getInstance().isValid(email)) {
				json.getErrors().put("email", "邮件格式错误");
			}
			if (StringUtils.isBlank(password)) {
				json.getErrors().put("password", "密码不能为空");
			} else if (password.length() < 6 && password.length() > 30) {
				json.getErrors().put("password", "密码最少6个字符,最多30个字符");
			}
			// 校验验证码
			if (StringUtils.isNotBlank(kaptcha)
					&& kaptcha.equalsIgnoreCase(captcha)) {

			} else {
				json.getErrors().put("captcha", "验证码错误");
			}
			this.validate(json);

			adminService.adminLogin(email, password, request);

		} catch (Exception e) {
			// 异常,重置验证码
			request.getSession().removeAttribute(
					com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
			json.setResult(false);
			json.getErrors().put("password", "邮箱或密码错误");
			json.setMsg("change_captcha");
		}
		return json;
	}

	/**
	 * 生成验证码
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "captcha.htm", method = RequestMethod.GET)
	public void captcha(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		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 = captchaProducer.createText();
		request.getSession().setAttribute(
				com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY, capText);
		BufferedImage bi = captchaProducer.createImage(capText);
		ServletOutputStream out = response.getOutputStream();
		ImageIO.write(bi, "jpg", out);
		try {
			out.flush();
		} finally {
			out.close();
		}
	}
}
最近下载更多
wjh007  LV5 2024年12月17日
辛利伟  LV7 2023年3月2日
qiang123  LV8 2022年7月8日
wanglinddad  LV55 2022年4月27日
wzp1314  LV4 2020年11月10日
oushao  LV10 2020年10月13日
hahaenenene  LV8 2020年9月2日
wcz1234  LV4 2020年3月8日
sweetyy  LV8 2019年12月24日
wangdongtai  LV31 2019年10月7日
最近浏览更多
fujinjava  LV2 3月3日
wjh007  LV5 2024年12月17日
微信网友_7212830140796928 2024年10月16日
暂无贡献等级
pengkui  LV14 2024年8月16日
是数据库  LV3 2024年5月7日
jhyan127 2024年2月18日
暂无贡献等级
PSSDZH  LV3 2024年1月25日
locklock  LV2 2023年12月22日
ninanlin  LV2 2023年10月31日
zdmxjxj  LV11 2023年9月1日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友