首页>代码>SpringBoot整合Apache Shiro极简入门实例>/springboot-shiro-demo/src/main/java/com/simon/springbootshirodemo/controller/HellloController.java
package com.simon.springbootshirodemo.controller; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.IncorrectCredentialsException; import org.apache.shiro.authc.UnknownAccountException; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.subject.Subject; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; /** * @author Simon */ @Controller public class HellloController { @RequestMapping({"/", "/index"}) public String toIndex(Model model) { model.addAttribute("msg", "Hello Shiro!"); return "index"; } @RequestMapping("/user/add") public String add() { return "/user/add"; } @RequestMapping("/user/update") public String update() { return "/user/update"; } @RequestMapping("/toLogin") public String toLogin() { return "login"; } @RequestMapping("/login") public String login(String username, String password, Model model) { Subject subject = SecurityUtils.getSubject(); UsernamePasswordToken token = new UsernamePasswordToken(username, password); try { subject.login(token); return "index"; } catch (UnknownAccountException e) { model.addAttribute("msg", "用户名不存在!"); return "login"; } catch (IncorrectCredentialsException e) { model.addAttribute("msg", "密码错误!"); return "login"; } } @RequestMapping("/noauth") @ResponseBody public String unauthorized() { return "未授权无法访问此页面"; } @RequestMapping("/logout") public String logout() { Subject subject = SecurityUtils.getSubject(); System.out.println(subject.getSession().getAttribute("loginUser")); subject.logout(); System.out.println(subject.getSession().getAttribute("loginUser")); System.out.println("执行了退出"); return "login"; } }

微信网友_6103037516468224
2024年3月1日
暂无贡献等级
Seaskye LV14
2023年11月4日
Rommel LV27
2022年11月27日
最代码-宋家辉 LV61
2022年11月22日
最代码官方 LV168
2022年11月20日

dapeng0011 LV15
2024年7月8日
HSQSWNW LV1
2024年6月26日
17380184110
2024年6月19日
暂无贡献等级
TY0165 LV20
2024年6月17日
chenranr LV10
2024年6月15日
ma406805131 LV19
2024年6月2日
chirsbey2
2024年4月18日
暂无贡献等级
yhe107 LV3
2024年4月17日
tqsDemo LV5
2024年3月15日
微信网友_6103037516468224
2024年3月1日
暂无贡献等级