首页>代码>springboot+apache shiro登录注册demo>/shiro/src/main/java/com/shiro/demo/controller/UserController.java
package com.shiro.demo.controller; import com.shiro.demo.pojo.User; import com.shiro.demo.service.UserService; import org.apache.shiro.SecurityUtils; 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.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import javax.annotation.Resource; import javax.servlet.http.HttpSession; /** * @CLASSNAME UserController * @DESCRIPTION 描述 * @AUTHOR chen * @DATE 2018-11-24 15:43 **/ @Controller public class UserController { @Resource private UserService userService; @RequestMapping("login") public String login1() { return "login"; } @RequestMapping("unauthorized") public String unauthorized() { return "unauthorized"; } @RequestMapping("index") public String index() { return "index"; } @RequestMapping("admin") @ResponseBody public String admin() { return "admin ok"; } @RequestMapping("edit") @ResponseBody public String edit() { return "edit ok"; } @RequestMapping("add") @ResponseBody public String add() { return "add ok"; } @ResponseBody @RequestMapping("delete") public String delete() { return "delete ok"; } @RequestMapping("loginUser") public String loginUser(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session) { UsernamePasswordToken token = new UsernamePasswordToken( username, password ); Subject subject = SecurityUtils.getSubject(); //认证逻辑 try { subject.login( token ); User user = (User) subject.getPrincipal(); session.setAttribute( "user", user ); return "index"; } catch (Exception e) { return "login"; } } @RequestMapping("logout") public String logout() { Subject subject = SecurityUtils.getSubject(); if (subject != null) { subject.logout(); } return "login"; } @ResponseBody @RequestMapping("insert") public void insert(User user) { userService.insert( user ); } @RequestMapping("regUI") public String regUI() { return "register"; } @RequestMapping("register") public ModelAndView register(User user){ ModelAndView mv = new ModelAndView( ); userService.insert( user ); mv.setViewName( "login" ); return mv; } }
最近下载更多
wangyulong111213 LV1
2023年11月15日
微信网友_6718454036303872 LV2
2023年11月2日
jiemomo LV12
2023年10月19日
打撒十大 LV1
2023年9月13日
森sdfgf LV8
2023年6月28日
彬 LV6
2022年7月3日
chenhan413 LV10
2022年5月5日
fangen0005 LV25
2022年4月19日
szl123 LV2
2022年4月9日
heweimin LV12
2022年4月9日
最近浏览更多
zrz666 LV1
4月18日
一起加油 LV4
3月5日
微信网友_6802079662936064 LV3
2023年12月31日
微信网友_6792227559149568
2023年12月24日
暂无贡献等级
3501855841
2023年12月22日
暂无贡献等级
哪里的完整版 LV7
2023年12月15日
GJQ123 LV4
2023年12月1日
fff2003 LV6
2023年11月25日
王卿毅 LV1
2023年11月18日
wangyulong111213 LV1
2023年11月15日