package com.tianbo.controller; import com.tianbo.util.json.ResponseModel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.*; import org.apache.shiro.authz.UnauthenticatedException; import org.apache.shiro.subject.Subject; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Transactional; 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.ResponseBody; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; /** * Created by mrz on 2017/8/28. */ @Controller public class MainController { protected final Logger logger = LoggerFactory.getLogger(getClass()); @RequestMapping("/login") public String login(){ return "login"; } @RequestMapping("/main/info") public String index(){ return "main/info"; } @RequestMapping("/main") public String main(){ return "main"; } @Transactional @RequestMapping(value = "/doLogin", method = RequestMethod.POST) @ResponseBody public ResponseModel dologin(String username, String password, String captcha){ ResponseModel md = new ResponseModel(200,"",null); String msg = ""; UsernamePasswordToken token = new UsernamePasswordToken(username,password); token.setRememberMe(true); Subject subject = SecurityUtils.getSubject(); logger.info("token="+token); try { subject.login(token); if (subject.isAuthenticated()){ subject.getSession().setAttribute("username",username); return md; } }catch (IncorrectCredentialsException e) { msg = "登录密码错误. Password for account " + token.getPrincipal() + " was incorrect."; md.setStatus(500); System.out.println(msg); } catch (ExcessiveAttemptsException e) { msg = "登录失败次数过多"; md.setStatus(500); System.out.println(msg); } catch (LockedAccountException e) { msg = "帐号已被锁定. The account for username " + token.getPrincipal() + " was locked."; md.setStatus(500); System.out.println(msg); } catch (DisabledAccountException e) { msg = "帐号已被禁用. The account for username " + token.getPrincipal() + " was disabled."; md.setStatus(500); System.out.println(msg); } catch (ExpiredCredentialsException e) { msg = "帐号已过期. the account for username " + token.getPrincipal() + " was expired."; md.setStatus(500); System.out.println(msg); } catch (UnknownAccountException e) { msg = "帐号不存在. There is no user with username of " + token.getPrincipal(); md.setStatus(500); System.out.println(msg); } catch (UnauthenticatedException e) { msg = "您没有得到相应的授权!" + e.getMessage(); md.setStatus(500); System.out.println(msg); } md.setMsg(msg); return md; } }