LoginController.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
  3. */
  4. package com.thinkgem.jeesite.modules.sys.web;
  5. import java.util.Map;
  6. import javax.annotation.Resource;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import javax.servlet.http.HttpSession;
  10. import com.caimei.modules.user.dao.MessageCenterDao;
  11. import com.caimei.redis.RedisService;
  12. import org.apache.shiro.authz.UnauthorizedException;
  13. import org.apache.shiro.authz.annotation.RequiresPermissions;
  14. import org.apache.shiro.web.util.WebUtils;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Controller;
  17. import org.springframework.ui.Model;
  18. import org.springframework.web.bind.annotation.PathVariable;
  19. import org.springframework.web.bind.annotation.RequestMapping;
  20. import org.springframework.web.bind.annotation.RequestMethod;
  21. import com.google.common.collect.Maps;
  22. import com.thinkgem.jeesite.common.config.Global;
  23. import com.thinkgem.jeesite.common.security.shiro.session.SessionDAO;
  24. import com.thinkgem.jeesite.common.servlet.ValidateCodeServlet;
  25. import com.thinkgem.jeesite.common.utils.CacheUtils;
  26. import com.thinkgem.jeesite.common.utils.CookieUtils;
  27. import com.thinkgem.jeesite.common.utils.IdGen;
  28. import com.thinkgem.jeesite.common.utils.StringUtils;
  29. import com.thinkgem.jeesite.common.web.BaseController;
  30. import com.thinkgem.jeesite.modules.sys.security.FormAuthenticationFilter;
  31. import com.thinkgem.jeesite.modules.sys.security.SystemAuthorizingRealm.Principal;
  32. import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
  33. /**
  34. * 登录Controller
  35. * @author ThinkGem
  36. * @version 2013-5-31
  37. */
  38. @Controller
  39. public class LoginController extends BaseController{
  40. @Resource
  41. private RedisService redisService;
  42. @Autowired
  43. private SessionDAO sessionDAO;
  44. @Autowired
  45. MessageCenterDao messageCenterDao;
  46. /**
  47. * 管理登录
  48. */
  49. @RequestMapping(value = "${adminPath}/login", method = RequestMethod.GET)
  50. public String login(HttpServletRequest request, HttpServletResponse response, Model model) {
  51. Principal principal = UserUtils.getPrincipal();
  52. // // 默认页签模式
  53. // String tabmode = CookieUtils.getCookie(request, "tabmode");
  54. // if (tabmode == null){
  55. // CookieUtils.setCookie(response, "tabmode", "1");
  56. // }
  57. if (logger.isDebugEnabled()){
  58. logger.debug("login, active session size: {}", sessionDAO.getActiveSessions(false).size());
  59. }
  60. // 如果已登录,再次访问主页,则退出原账号。
  61. if (Global.TRUE.equals(Global.getConfig("notAllowRefreshIndex"))){
  62. CookieUtils.setCookie(response, "LOGINED", "false");
  63. }
  64. // 如果已经登录,则跳转到管理首页
  65. if(principal != null && !principal.isMobileLogin()){
  66. return "redirect:" + adminPath;
  67. }
  68. // String view;
  69. // view = "/WEB-INF/views/modules/sys/sysLogin.jsp";
  70. // view = "classpath:";
  71. // view += "jar:file:/D:/GitHub/jeesite/src/main/webapp/WEB-INF/lib/jeesite.jar!";
  72. // view += "/"+getClass().getName().replaceAll("\\.", "/").replace(getClass().getSimpleName(), "")+"view/sysLogin";
  73. // view += ".jsp";
  74. return "modules/sys/sysLogin";
  75. }
  76. /**
  77. * 登录失败,真正登录的POST请求由Filter完成
  78. */
  79. @RequestMapping(value = "${adminPath}/login", method = RequestMethod.POST)
  80. public String loginFail(HttpServletRequest request, HttpServletResponse response, Model model) {
  81. Principal principal = UserUtils.getPrincipal();
  82. // 如果已经登录,则跳转到管理首页
  83. if(principal != null){
  84. return "redirect:" + adminPath;
  85. }
  86. String username = WebUtils.getCleanParam(request, FormAuthenticationFilter.DEFAULT_USERNAME_PARAM);
  87. boolean rememberMe = WebUtils.isTrue(request, FormAuthenticationFilter.DEFAULT_REMEMBER_ME_PARAM);
  88. boolean mobile = WebUtils.isTrue(request, FormAuthenticationFilter.DEFAULT_MOBILE_PARAM);
  89. String exception = (String)request.getAttribute(FormAuthenticationFilter.DEFAULT_ERROR_KEY_ATTRIBUTE_NAME);
  90. String message = (String)request.getAttribute(FormAuthenticationFilter.DEFAULT_MESSAGE_PARAM);
  91. if (StringUtils.isBlank(message) || StringUtils.equals(message, "null")){
  92. message = "用户或密码错误, 请重试.";
  93. }
  94. model.addAttribute(FormAuthenticationFilter.DEFAULT_USERNAME_PARAM, username);
  95. model.addAttribute(FormAuthenticationFilter.DEFAULT_REMEMBER_ME_PARAM, rememberMe);
  96. model.addAttribute(FormAuthenticationFilter.DEFAULT_MOBILE_PARAM, mobile);
  97. model.addAttribute(FormAuthenticationFilter.DEFAULT_ERROR_KEY_ATTRIBUTE_NAME, exception);
  98. model.addAttribute(FormAuthenticationFilter.DEFAULT_MESSAGE_PARAM, message);
  99. if (logger.isDebugEnabled()){
  100. logger.debug("login fail, active session size: {}, message: {}, exception: {}",
  101. sessionDAO.getActiveSessions(false).size(), message, exception);
  102. }
  103. // 非授权异常,登录失败,验证码加1。
  104. if (!UnauthorizedException.class.getName().equals(exception)){
  105. model.addAttribute("isValidateCodeLogin", isValidateCodeLogin(username, true, false));
  106. }
  107. // 验证失败清空验证码
  108. request.getSession().setAttribute(ValidateCodeServlet.VALIDATE_CODE, IdGen.uuid());
  109. // 如果是手机登录,则返回JSON字符串
  110. if (mobile){
  111. return renderString(response, model);
  112. }
  113. return "modules/sys/sysLogin";
  114. }
  115. /**
  116. * 登录成功,进入管理首页
  117. */
  118. @RequiresPermissions("user")
  119. @RequestMapping(value = "${adminPath}")
  120. public String index(HttpServletRequest request, HttpServletResponse response,Model model) {
  121. redisService.set("onLineFlag","online",36000l);//设置10小时有效(提供老admin系统免登入)
  122. Principal principal = UserUtils.getPrincipal();
  123. Integer count=messageCenterDao.count();
  124. model.addAttribute("count",count);
  125. // 登录成功后,验证码计算器清零
  126. isValidateCodeLogin(principal.getLoginName(), false, true);
  127. if (logger.isDebugEnabled()){
  128. logger.debug("show index, active session size: {}", sessionDAO.getActiveSessions(false).size());
  129. }
  130. // 如果已登录,再次访问主页,则退出原账号。
  131. if (Global.TRUE.equals(Global.getConfig("notAllowRefreshIndex"))){
  132. String logined = CookieUtils.getCookie(request, "LOGINED");
  133. if (StringUtils.isBlank(logined) || "false".equals(logined)){
  134. CookieUtils.setCookie(response, "LOGINED", "true");
  135. }else if (StringUtils.equals(logined, "true")){
  136. UserUtils.getSubject().logout();
  137. return "redirect:" + adminPath + "/login";
  138. }
  139. }
  140. // 如果是手机登录,则返回JSON字符串
  141. if (principal.isMobileLogin()){
  142. if (request.getParameter("login") != null){
  143. return renderString(response, principal);
  144. }
  145. if (request.getParameter("index") != null){
  146. return "modules/sys/sysIndex";
  147. }
  148. return "redirect:" + adminPath + "/login";
  149. }
  150. // // 登录成功后,获取上次登录的当前站点ID
  151. // UserUtils.putCache("siteId", StringUtils.toLong(CookieUtils.getCookie(request, "siteId")));
  152. // System.out.println("==========================a");
  153. // try {
  154. // byte[] bytes = com.thinkgem.jeesite.common.utils.FileUtils.readFileToByteArray(
  155. // com.thinkgem.jeesite.common.utils.FileUtils.getFile("c:\\sxt.dmp"));
  156. // UserUtils.getSession().setAttribute("kkk", bytes);
  157. // UserUtils.getSession().setAttribute("kkk2", bytes);
  158. // } catch (Exception e) {
  159. // e.printStackTrace();
  160. // }
  161. //// for (int i=0; i<1000000; i++){
  162. //// //UserUtils.getSession().setAttribute("a", "a");
  163. //// request.getSession().setAttribute("aaa", "aa");
  164. //// }
  165. // System.out.println("==========================b");
  166. return "modules/sys/sysIndex";
  167. }
  168. /**
  169. * 获取主题方案
  170. */
  171. @RequestMapping(value = "/theme/{theme}")
  172. public String getThemeInCookie(@PathVariable String theme, HttpServletRequest request, HttpServletResponse response){
  173. if (StringUtils.isNotBlank(theme)){
  174. CookieUtils.setCookie(response, "theme", theme);
  175. }else{
  176. theme = CookieUtils.getCookie(request, "theme");
  177. }
  178. return "redirect:"+request.getParameter("url");
  179. }
  180. /**
  181. * 是否是验证码登录
  182. * @param useruame 用户名
  183. * @param isFail 计数加1
  184. * @param clean 计数清零
  185. * @return
  186. */
  187. @SuppressWarnings("unchecked")
  188. public static boolean isValidateCodeLogin(String useruame, boolean isFail, boolean clean){
  189. Map<String, Integer> loginFailMap = (Map<String, Integer>)CacheUtils.get("loginFailMap");
  190. if (loginFailMap==null){
  191. loginFailMap = Maps.newHashMap();
  192. CacheUtils.put("loginFailMap", loginFailMap);
  193. }
  194. Integer loginFailNum = loginFailMap.get(useruame);
  195. if (loginFailNum==null){
  196. loginFailNum = 0;
  197. }
  198. if (isFail){
  199. loginFailNum++;
  200. loginFailMap.put(useruame, loginFailNum);
  201. }
  202. if (clean){
  203. loginFailMap.remove(useruame);
  204. }
  205. return loginFailNum >= 3;
  206. }
  207. }