首页>代码>ssm整合apache shiro开发用户登录权限认证的实例demo>/ShiroDemo/src/com/gjp/controller/ShiroLoginController.java
package com.gjp.controller; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.subject.Subject; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/shiro") public class ShiroLoginController { @RequestMapping("/login") public ModelAndView login( @RequestParam("username") String username, @RequestParam("password") String password) { //定义返回对象 ModelAndView modelAndView = new ModelAndView(); //创建Subject(这个就代表当前用户) Subject currentUser = SecurityUtils.getSubject(); // 如果用户没登录,则走调用登录方法,否则直接跳转到成功页面 if (!currentUser.isAuthenticated()) { // 把用户名和密码封装为UsernamePasswordToken对象 UsernamePasswordToken token = new UsernamePasswordToken(username,password); // rememberme 如果页面上加了记住我复选框,也可以获取到,设置给token // 这里我没弄 // token.setRememberMe(true); try { // 调用login方法,登录, //此处方法会进入到ShiroRealm类中执行doGetAuthenticationInfo()方法 currentUser.login(token); } catch (AuthenticationException ae) { //如果登录失败,则统一提示账号密码错误,其实还有很多对应异常 //例如,账号不存在,密码错误,用户被锁定等等 //将错误信息存入到scope中,并跳转到当前页面,显示错误信息 modelAndView.addObject("errorMsg", "账号或密码错误!"); modelAndView.setViewName("login"); return modelAndView; } } // 跳转到相应的处理页面 // redirect:/list.jsp modelAndView.setViewName("redirect:/list.jsp"); return modelAndView; } }
最近下载更多
sweetbox LV10
2022年7月11日
543666826 LV33
2021年11月20日
adasdsda LV1
2021年9月26日
303661956 LV11
2020年7月6日
wsk588 LV26
2020年6月8日
卡卡罗特 LV8
2020年5月7日
liuwenlong LV20
2020年4月21日
2945624210 LV2
2020年3月27日
cainame LV11
2020年3月17日
yingyingya LV3
2020年3月10日
最近浏览更多
fire1234 LV2
3月5日
森sdfgf LV8
2023年6月28日
女王不该在山炮村养花 LV8
2023年4月13日
139465 LV12
2023年4月3日
Wu_大花 LV1
2023年3月26日
LITIANYU084414 LV11
2023年1月4日
qq1176648922 LV6
2022年12月28日
wz520135 LV7
2022年12月22日
邬松桥 LV11
2022年11月17日
sweetbox LV10
2022年7月11日