LoginController.java 3.85 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
package com.lyms.cm.controller;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
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.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lyms.base.common.entity.role.TreeNode;
import com.lyms.base.common.entity.user.Users;
import com.lyms.base.common.enums.PermissionEnum;
import com.lyms.base.common.service.role.PermissionsService;
import com.lyms.base.common.service.user.UsersService;
import com.lyms.cm.controller.sys.SysUsersController;
import com.lyms.constants.Constants;
import com.lyms.shiro.ShiroWebUtils;
import com.lyms.util.StrUtils;
import com.lyms.web.controller.BaseController;
import com.qiniu.util.Json;

@Controller
public class LoginController extends BaseController {

private static final String VIEW_LOGIN = "/login/login";

@Autowired
private PermissionsService sysPermissionsService;
@Autowired
private UsersService usersService;

@RequestMapping("/")
public String index(Model model) {
if (!ShiroWebUtils.isLogin()) {
return redirectTo(VIEW_LOGIN);
}
Users user = ShiroWebUtils.getCurrentUser();
List<TreeNode> menus = sysPermissionsService
.getPermissionMenuTreeByPuri(PermissionEnum.CENTER_MANAGER_ROOT.getCode(), user);
model.addAttribute("menus", menus);
return "/index";
}

@RequestMapping("/home")
public String home() {
return "/home";
}

/**
* <li>@Description:导航到登录(GET)
* <li>@param model
* <li>@return
* <li>创建人:方承
* <li>创建时间:2016年11月27日
* <li>修改人:
* <li>修改时间:
*/
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(Model model) {
model.addAttribute(Constants.CAPTCHA_TOKEN, StrUtils.uuid());
return VIEW_LOGIN;
}
/**
* <li>@Description:登录验证方法,详细查看ShiroRealm.class
* <li>@param req
* <li>@param model
* <li>@return
* <li>创建人:方承
* <li>创建时间:2016年11月25日
* <li>修改人:
* <li>修改时间:
*/
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(String username, String password,
@RequestParam(value = "rememberMe", defaultValue = "0") int rememberMe, Model model) {
String view = VIEW_LOGIN;
if (StrUtils.isEmpty(username) || StrUtils.isEmpty(password)) {
model.addAttribute(Constants.CAPTCHA_TOKEN, StrUtils.uuid());
return view;
}
AuthenticationToken token = new UsernamePasswordToken(username, password);
if (rememberMe == 1) {
((UsernamePasswordToken) token).setRememberMe(true);
}
try {
// 查看ShiroRealm.class
SecurityUtils.getSubject().login(token);
return redirectTo("/");
} catch (AuthenticationException e) {
if (e instanceof UnknownAccountException) {
model.addAttribute("message", "用户不存在");
} else if (e instanceof AccountException) {
model.addAttribute("message", "用户名密码错误");
} else if (e instanceof LockedAccountException) {
model.addAttribute("message", "用户被禁用");
} else {
model.addAttribute("message", "用户认证失败");
}
}
model.addAttribute(Constants.CAPTCHA_TOKEN, StrUtils.uuid());
return view;
}

}