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.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import com.lyms.cm.entity.sys.SysUsers; import com.lyms.cm.entity.sys.TreeNode; import com.lyms.cm.enums.PermissionEnum; import com.lyms.cm.service.sys.SysPermissionsService; import com.lyms.constants.Constants; import com.lyms.shiro.ShiroWebUtils; import com.lyms.util.StrUtils; import com.lyms.web.controller.BaseController; @Controller public class LoginController extends BaseController { private static final String VIEW_LOGIN = "/login/login"; @Autowired private SysPermissionsService sysPermissionsService; @RequestMapping("/") public String index(Model model) { if(!ShiroWebUtils.isLogin()){ return redirectTo(VIEW_LOGIN); } SysUsers user = ShiroWebUtils.getCurrentUser(); List menus = sysPermissionsService.getPermissionMenuTreeByPuri(PermissionEnum.CENTER_MANAGER_ROOT.getCode(),user); model.addAttribute("menus", menus); return "/index"; } @RequestMapping("/home") public String home() { return "/home"; } /** *
  • @Description:导航到登录(GET) *
  • @param model *
  • @return *
  • 创建人:方承 *
  • 创建时间:2016年11月27日 *
  • 修改人: *
  • 修改时间: */ @RequestMapping(value = "/login", method = RequestMethod.GET) public String login(Model model){ model.addAttribute(Constants.CAPTCHA_TOKEN, StrUtils.uuid()); return VIEW_LOGIN; } /** *
  • @Description:登录验证方法,详细查看ShiroRealm.class *
  • @param req *
  • @param model *
  • @return *
  • 创建人:方承 *
  • 创建时间:2016年11月25日 *
  • 修改人: *
  • 修改时间: */ @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; } }