首页>代码>springboot2 + mybatis-plus3 +shiro1.4 +layui细颗粒度网站后台用户权限管理系统>/comadmin/src/main/java/com/xiaoshu/admin/controller/LonginController.java
package com.xiaoshu.admin.controller; import com.google.code.kaptcha.impl.DefaultKaptcha; import com.xiaoshu.admin.entity.vo.ShowMenuVo; import com.xiaoshu.admin.service.MenuService; import com.xiaoshu.admin.service.UserService; import com.xiaoshu.common.annotation.SysLog; import com.xiaoshu.common.config.MySysUser; import com.xiaoshu.common.exception.UserTypeAccountException; import com.xiaoshu.common.realm.AuthRealm; import com.xiaoshu.common.util.Constants; import com.xiaoshu.common.util.ResponseEntity; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.IncorrectCredentialsException; import org.apache.shiro.authc.LockedAccountException; import org.apache.shiro.authc.UnknownAccountException; import org.apache.shiro.authc.UsernamePasswordToken; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.GetMapping; import org.apache.shiro.subject.Subject; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.awt.image.BufferedImage; import java.io.IOException; import java.time.LocalDate; import java.util.*; @Controller public class LonginController { private final static Logger LOGGER = LoggerFactory.getLogger(LonginController.class); public final static String LOGIN_TYPE = "loginType"; @Autowired @Qualifier("captchaProducer") DefaultKaptcha captchaProducer; @Autowired UserService userService; @Autowired MenuService menuService; public enum LoginTypeEnum { PAGE,ADMIN; } @GetMapping(value = "") public String welcome() { return "redirect:admin"; } @GetMapping(value = {"admin","admin/index"}) public String adminIndex(RedirectAttributes attributes, ModelMap map) { Subject s = SecurityUtils.getSubject(); attributes.addFlashAttribute(LOGIN_TYPE, LoginTypeEnum.ADMIN); if(s.isAuthenticated()) { return "redirect:index"; } return "redirect:toLogin"; } @GetMapping(value = "toLogin") public String adminToLogin(HttpSession session, @ModelAttribute(LOGIN_TYPE) String loginType) { if(StringUtils.isBlank(loginType)) { LoginTypeEnum attribute = (LoginTypeEnum) session.getAttribute(LOGIN_TYPE); loginType = attribute == null ? loginType : attribute.name(); } if(LoginTypeEnum.ADMIN.name().equals(loginType)) { session.setAttribute(LOGIN_TYPE,LoginTypeEnum.ADMIN); return "admin/login"; }else { session.setAttribute(LOGIN_TYPE,LoginTypeEnum.PAGE); return "login"; } } @GetMapping(value = "index") public String index(HttpSession session, @ModelAttribute(LOGIN_TYPE) String loginType) { if(StringUtils.isBlank(loginType)) { LoginTypeEnum attribute = (LoginTypeEnum) session.getAttribute(LOGIN_TYPE); loginType = attribute == null ? loginType : attribute.name(); } if(LoginTypeEnum.ADMIN.name().equals(loginType)) { AuthRealm.ShiroUser principal = (AuthRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal(); session.setAttribute("icon",StringUtils.isBlank(principal.getIcon()) ? "/static/admin/img/face.jpg" : principal.getIcon()); return "admin/index"; }else { return "index"; } } @GetMapping("/getCaptcha") public void getCaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException { //设置页面不缓存 response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); String verifyCode = captchaProducer.createText(); //将验证码放到HttpSession里面 request.getSession().setAttribute(Constants.VALIDATE_CODE, verifyCode); LOGGER.info("本次生成的验证码为[" + verifyCode + "],已存放到HttpSession中"); //设置输出的内容的类型为JPEG图像 response.setContentType("image/jpeg"); BufferedImage bufferedImage = captchaProducer.createImage(verifyCode); //写给浏览器 ImageIO.write(bufferedImage, "JPEG", response.getOutputStream()); } @PostMapping("admin/login") @SysLog("用户登录") @ResponseBody public ResponseEntity adminLogin(HttpServletRequest request) { String username = request.getParameter("username"); String password = request.getParameter("password"); String rememberMe = request.getParameter("rememberMe"); String code = request.getParameter("code"); if(StringUtils.isBlank(username) || StringUtils.isBlank(password)){ return ResponseEntity.failure("用户名或者密码不能为空"); }else if(StringUtils.isBlank(code)){ return ResponseEntity.failure("验证码不能为空"); } HttpSession session = request.getSession(); if(session == null){ return ResponseEntity.failure("session超时"); } String trueCode = (String)session.getAttribute(Constants.VALIDATE_CODE); if(StringUtils.isBlank(trueCode)){ return ResponseEntity.failure("验证码超时"); } if(StringUtils.isBlank(code) || !trueCode.toLowerCase().equals(code.toLowerCase())){ return ResponseEntity.failure("验证码错误"); }else { /*当前用户*/ String errorMsg = null; Subject user = SecurityUtils.getSubject(); UsernamePasswordToken token = new UsernamePasswordToken(username,password,Boolean.valueOf(rememberMe)); try { user.login(token); LOGGER.debug(username+"用户"+LocalDate.now().toString()+":======》登陆系统!"); }catch (IncorrectCredentialsException e) { errorMsg = "用户名密码错误!"; }catch (UnknownAccountException e) { errorMsg = "账户不存在!"; }catch (LockedAccountException e) { errorMsg = "账户已被锁定!"; }catch (UserTypeAccountException e) { errorMsg = "账户不是管理用户!"; } if(StringUtils.isBlank(errorMsg)) { ResponseEntity responseEntity = new ResponseEntity(); responseEntity.setSuccess(Boolean.TRUE); responseEntity.setAny("url","index"); return responseEntity; }else { return ResponseEntity.failure(errorMsg); } } } @GetMapping("admin/main") public String main(ModelMap map){ return "admin/main"; } /*** * 获得用户所拥有的菜单列表 * @return */ @GetMapping("/admin/user/getUserMenu") @ResponseBody public List<ShowMenuVo> getUserMenu(){ String userId = MySysUser.id(); List<ShowMenuVo> list = menuService.getShowMenuByUser(userId); return list; } @GetMapping("systemLogout") @SysLog("退出系统") public String logOut(){ SecurityUtils.getSubject().logout(); return "redirect:admin"; } }

微信网友_7134912998903808 LV15
2024年9月13日
llllllK LV5
2024年5月13日
lipiao161 LV16
2024年5月4日
Iterman LV2
2023年3月9日
168345395 LV3
2022年10月24日
jiaerkai LV10
2022年10月12日
wy4525 LV6
2022年8月12日
刘亦菲9527 LV15
2022年7月19日
15578157792 LV7
2022年6月28日
chnhope LV4
2022年5月14日

Solowen
4月16日
暂无贡献等级
dane168 LV8
2月17日
微信网友_7134912998903808 LV15
2024年9月12日
llllllK LV5
2024年5月13日
xiaofei1219 LV1
2023年11月23日
wnnmmb LV2
2023年10月18日
1048571230 LV2
2023年9月20日
微信网友_6247573567180800 LV2
2023年8月31日
tyyeng LV18
2023年8月1日
hhvivi LV2
2023年7月2日