|
|
@ -22,11 +22,25 @@ import com.tamguo.common.utils.Result;
|
|
|
|
import com.tamguo.common.utils.SystemConstant;
|
|
|
|
import com.tamguo.common.utils.SystemConstant;
|
|
|
|
import com.tamguo.utils.ShiroUtils;
|
|
|
|
import com.tamguo.utils.ShiroUtils;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 登录控制器
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @author tamguo
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
*/
|
|
|
|
@Controller
|
|
|
|
@Controller
|
|
|
|
public class LoginController {
|
|
|
|
public class LoginController {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 生成验证码图片并设置到响应头和会话中
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param response 响应对象
|
|
|
|
|
|
|
|
* @param session 会话对象
|
|
|
|
|
|
|
|
* @throws ServletException 可能抛出的 Servlet 异常
|
|
|
|
|
|
|
|
* @throws IOException 可能抛出的 IO 异常
|
|
|
|
|
|
|
|
*/
|
|
|
|
@RequestMapping("captcha.jpg")
|
|
|
|
@RequestMapping("captcha.jpg")
|
|
|
|
public void captcha(HttpServletResponse response , HttpSession session) throws ServletException, IOException {
|
|
|
|
public void captcha(HttpServletResponse response, HttpSession session) throws ServletException, IOException {
|
|
|
|
response.setHeader("Cache-Control", "no-store, no-cache");
|
|
|
|
response.setHeader("Cache-Control", "no-store, no-cache");
|
|
|
|
response.setContentType("image/jpeg");
|
|
|
|
response.setContentType("image/jpeg");
|
|
|
|
|
|
|
|
|
|
|
@ -34,20 +48,41 @@ public class LoginController {
|
|
|
|
session.setAttribute(SystemConstant.KAPTCHA_SESSION_KEY, a);
|
|
|
|
session.setAttribute(SystemConstant.KAPTCHA_SESSION_KEY, a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 处理登录页面的 GET 请求,设置模型和视图属性
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param redirectUrl 重定向 URL
|
|
|
|
|
|
|
|
* @param model 模型和视图对象
|
|
|
|
|
|
|
|
* @return 模型和视图对象
|
|
|
|
|
|
|
|
*/
|
|
|
|
@RequestMapping(value = "/login.html", method = RequestMethod.GET)
|
|
|
|
@RequestMapping(value = "/login.html", method = RequestMethod.GET)
|
|
|
|
public ModelAndView login(String redirectUrl , ModelAndView model){
|
|
|
|
public ModelAndView login(String redirectUrl, ModelAndView model) {
|
|
|
|
model.setViewName("login");
|
|
|
|
model.setViewName("login");
|
|
|
|
model.addObject("isVerifyCode" , "0");
|
|
|
|
model.addObject("isVerifyCode", "0");
|
|
|
|
model.addObject("redirectUrl", redirectUrl);
|
|
|
|
model.addObject("redirectUrl", redirectUrl);
|
|
|
|
return model;
|
|
|
|
return model;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 处理提交登录的 POST 请求,进行登录验证和处理
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param username 用户名
|
|
|
|
|
|
|
|
* @param password 密码
|
|
|
|
|
|
|
|
* @param verifyCode 验证码
|
|
|
|
|
|
|
|
* @param redirectUrl 重定向 URL
|
|
|
|
|
|
|
|
* @param model 模型和视图对象
|
|
|
|
|
|
|
|
* @param session 会话对象
|
|
|
|
|
|
|
|
* @param response 响应对象
|
|
|
|
|
|
|
|
* @return 模型和视图对象或 null
|
|
|
|
|
|
|
|
* @throws IOException 可能抛出的 IO 异常
|
|
|
|
|
|
|
|
*/
|
|
|
|
@RequestMapping(value = "/submitLogin.html", method = RequestMethod.POST)
|
|
|
|
@RequestMapping(value = "/submitLogin.html", method = RequestMethod.POST)
|
|
|
|
public ModelAndView submitLogin(String username , String password , String verifyCode , String redirectUrl , ModelAndView model , HttpSession session , HttpServletResponse response) throws IOException{
|
|
|
|
public ModelAndView submitLogin(String username, String password, String verifyCode, String redirectUrl, ModelAndView model, HttpSession session, HttpServletResponse response) throws IOException {
|
|
|
|
Result result = Result.successResult(null);
|
|
|
|
Result result = Result.successResult(null);
|
|
|
|
if(StringUtils.isEmpty(verifyCode)) {
|
|
|
|
|
|
|
|
|
|
|
|
if (StringUtils.isEmpty(verifyCode)) {
|
|
|
|
result = Result.result(202, null, "请输入验证码");
|
|
|
|
result = Result.result(202, null, "请输入验证码");
|
|
|
|
} else if(StringUtils.isNotEmpty(verifyCode)){
|
|
|
|
} else if (StringUtils.isNotEmpty(verifyCode)) {
|
|
|
|
String kaptcha = session.getAttribute(SystemConstant.KAPTCHA_SESSION_KEY).toString();
|
|
|
|
String kaptcha = session.getAttribute(SystemConstant.KAPTCHA_SESSION_KEY).toString();
|
|
|
|
if (!verifyCode.equalsIgnoreCase(kaptcha)) {
|
|
|
|
if (!verifyCode.equalsIgnoreCase(kaptcha)) {
|
|
|
|
result = Result.result(205, null, "验证码错误");
|
|
|
|
result = Result.result(205, null, "验证码错误");
|
|
|
@ -58,9 +93,9 @@ public class LoginController {
|
|
|
|
subject.login(token);
|
|
|
|
subject.login(token);
|
|
|
|
|
|
|
|
|
|
|
|
session.setAttribute("currMember", ShiroUtils.getMember());
|
|
|
|
session.setAttribute("currMember", ShiroUtils.getMember());
|
|
|
|
if(!StringUtils.isEmpty(redirectUrl)) {
|
|
|
|
if (!StringUtils.isEmpty(redirectUrl)) {
|
|
|
|
response.sendRedirect(redirectUrl);
|
|
|
|
response.sendRedirect(redirectUrl);
|
|
|
|
}else {
|
|
|
|
} else {
|
|
|
|
response.sendRedirect("index.html");
|
|
|
|
response.sendRedirect("index.html");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
@ -73,24 +108,36 @@ public class LoginController {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
model.setViewName("login");
|
|
|
|
model.setViewName("login");
|
|
|
|
model.addObject("code", result.getCode());
|
|
|
|
model.addObject("code", result.getCode());
|
|
|
|
model.addObject("msg" , result.getMessage());
|
|
|
|
model.addObject("msg", result.getMessage());
|
|
|
|
model.addObject("username", username);
|
|
|
|
model.addObject("username", username);
|
|
|
|
return model;
|
|
|
|
return model;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 处理迷你登录的 GET 请求,进行登录验证和处理
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param username 用户名
|
|
|
|
|
|
|
|
* @param password 密码
|
|
|
|
|
|
|
|
* @param captcha 验证码
|
|
|
|
|
|
|
|
* @param model 模型和视图对象
|
|
|
|
|
|
|
|
* @param session 会话对象
|
|
|
|
|
|
|
|
* @return 结果对象
|
|
|
|
|
|
|
|
*/
|
|
|
|
@RequestMapping(value = "/miniLogin.html", method = RequestMethod.GET)
|
|
|
|
@RequestMapping(value = "/miniLogin.html", method = RequestMethod.GET)
|
|
|
|
@ResponseBody
|
|
|
|
@ResponseBody
|
|
|
|
public Result miniLogin(String username , String password , String captcha, ModelAndView model , HttpSession session) {
|
|
|
|
public Result miniLogin(String username, String password, String captcha, ModelAndView model, HttpSession session) {
|
|
|
|
Result result = null;
|
|
|
|
Result result = null;
|
|
|
|
if(StringUtils.isEmpty(captcha)) {
|
|
|
|
|
|
|
|
|
|
|
|
if (StringUtils.isEmpty(captcha)) {
|
|
|
|
result = Result.result(204, null, "请输入验证码");
|
|
|
|
result = Result.result(204, null, "请输入验证码");
|
|
|
|
} else if(StringUtils.isNotEmpty(captcha)){
|
|
|
|
} else if (StringUtils.isNotEmpty(captcha)) {
|
|
|
|
String kaptcha = session.getAttribute(SystemConstant.KAPTCHA_SESSION_KEY).toString();
|
|
|
|
String kaptcha = session.getAttribute(SystemConstant.KAPTCHA_SESSION_KEY).toString();
|
|
|
|
if (!captcha.equalsIgnoreCase(kaptcha)) {
|
|
|
|
if (!captcha.equalsIgnoreCase(kaptcha)) {
|
|
|
|
result = Result.result(205, null, "验证码错误");
|
|
|
|
result = Result.result(205, null, "验证码错误");
|
|
|
|
}else {
|
|
|
|
} else {
|
|
|
|
Subject subject = ShiroUtils.getSubject();
|
|
|
|
Subject subject = ShiroUtils.getSubject();
|
|
|
|
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
|
|
|
|
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
|
|
|
|
try {
|
|
|
|
try {
|
|
|
@ -107,13 +154,18 @@ public class LoginController {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 检查是否登录
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @return 结果对象
|
|
|
|
|
|
|
|
*/
|
|
|
|
@RequestMapping(value = "/isLogin.html", method = RequestMethod.GET)
|
|
|
|
@RequestMapping(value = "/isLogin.html", method = RequestMethod.GET)
|
|
|
|
@ResponseBody
|
|
|
|
@ResponseBody
|
|
|
|
public Result isLogin() {
|
|
|
|
public Result isLogin() {
|
|
|
|
if(ShiroUtils.isLogin()) {
|
|
|
|
if (ShiroUtils.isLogin()) {
|
|
|
|
return Result.result(1, null , "已经登录");
|
|
|
|
return Result.result(1, null, "已经登录");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result.result(0, null, "未登录");
|
|
|
|
return Result.result(0, null, "未登录");
|
|
|
|
}
|
|
|
|
}
|
|
|
|