diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysIndexController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysIndexController.java index 5420cb503..f0d072245 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysIndexController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysIndexController.java @@ -27,7 +27,7 @@ public class SysIndexController extends BaseController public String index(ModelMap mmap) { // 取身份信息 - SysUser user = getUser(); + SysUser user = getSysUser(); // 根据用户id取出菜单 List menus = menuService.selectMenusByUser(user); mmap.put("menus", menus); diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysProfileController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysProfileController.java index 810e35d08..f0031743b 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysProfileController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysProfileController.java @@ -17,7 +17,7 @@ import com.ruoyi.common.annotation.Log; import com.ruoyi.common.base.AjaxResult; import com.ruoyi.common.config.Global; import com.ruoyi.common.enums.BusinessType; -import com.ruoyi.framework.shiro.service.PasswordService; +import com.ruoyi.framework.shiro.service.SysPasswordService; import com.ruoyi.framework.util.FileUploadUtils; import com.ruoyi.framework.util.ShiroUtils; import com.ruoyi.system.domain.SysUser; @@ -42,7 +42,7 @@ public class SysProfileController extends BaseController private ISysUserService userService; @Autowired - private PasswordService passwordService; + private SysPasswordService passwordService; @Autowired private ISysDictDataService dictDataService; @@ -53,7 +53,7 @@ public class SysProfileController extends BaseController @GetMapping() public String profile(ModelMap mmap) { - SysUser user = getUser(); + SysUser user = getSysUser(); user.setSex(dictDataService.selectDictLabel("sys_user_sex", user.getSex())); mmap.put("user", user); mmap.put("roleGroup", userService.selectUserRoleGroup(user.getUserId())); @@ -65,7 +65,7 @@ public class SysProfileController extends BaseController @ResponseBody public boolean checkPassword(String password) { - SysUser user = getUser(); + SysUser user = getSysUser(); String encrypt = new Md5Hash(user.getLoginName() + password + user.getSalt()).toHex().toString(); if (user.getPassword().equals(encrypt)) { @@ -91,7 +91,7 @@ public class SysProfileController extends BaseController int rows = userService.resetUserPwd(user); if (rows > 0) { - setUser(userService.selectUserById(user.getUserId())); + setSysUser(userService.selectUserById(user.getUserId())); return success(); } return error(); @@ -127,7 +127,7 @@ public class SysProfileController extends BaseController { if (userService.updateUserInfo(user) > 0) { - setUser(userService.selectUserById(user.getUserId())); + setSysUser(userService.selectUserById(user.getUserId())); return success(); } return error(); @@ -149,7 +149,7 @@ public class SysProfileController extends BaseController user.setAvatar(avatar); if (userService.updateUserInfo(user) > 0) { - setUser(userService.selectUserById(user.getUserId())); + setSysUser(userService.selectUserById(user.getUserId())); return success(); } } diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java index 938bb7c08..c00f7439b 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java @@ -16,7 +16,7 @@ import com.ruoyi.common.base.AjaxResult; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.common.utils.ExcelUtil; import com.ruoyi.common.utils.StringUtils; -import com.ruoyi.framework.shiro.service.PasswordService; +import com.ruoyi.framework.shiro.service.SysPasswordService; import com.ruoyi.framework.util.ShiroUtils; import com.ruoyi.framework.web.page.TableDataInfo; import com.ruoyi.system.domain.SysUser; @@ -46,7 +46,7 @@ public class SysUserController extends BaseController private ISysPostService postService; @Autowired - private PasswordService passwordService; + private SysPasswordService passwordService; @RequiresPermissions("system:user:view") @GetMapping() diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/shiro/realm/UserRealm.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/shiro/realm/UserRealm.java index 445158fb2..418ce9f96 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/shiro/realm/UserRealm.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/shiro/realm/UserRealm.java @@ -19,7 +19,7 @@ import org.apache.shiro.subject.PrincipalCollection; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import com.ruoyi.framework.shiro.service.LoginService; +import com.ruoyi.framework.shiro.service.SysLoginService; import com.ruoyi.framework.util.ShiroUtils; import com.ruoyi.framework.web.exception.user.CaptchaException; import com.ruoyi.framework.web.exception.user.RoleBlockedException; @@ -47,7 +47,7 @@ public class UserRealm extends AuthorizingRealm private ISysRoleService roleService; @Autowired - private LoginService loginService; + private SysLoginService loginService; /** * 授权 @@ -127,7 +127,8 @@ public class UserRealm extends AuthorizingRealm log.info("对用户[" + username + "]进行登录验证..验证未通过{}", e.getMessage()); throw new AuthenticationException(e.getMessage(), e); } - SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, password, getName()); + ShiroUtils.getSession().setAttribute("sysUser", user); + SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user.getLoginName(), password, getName()); return info; } diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/shiro/service/LoginService.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/shiro/service/SysLoginService.java similarity index 95% rename from ruoyi-framework/src/main/java/com/ruoyi/framework/shiro/service/LoginService.java rename to ruoyi-framework/src/main/java/com/ruoyi/framework/shiro/service/SysLoginService.java index 5b48a7deb..e8f677a81 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/shiro/service/LoginService.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/shiro/service/SysLoginService.java @@ -27,10 +27,10 @@ import com.ruoyi.system.service.ISysUserService; * @author ruoyi */ @Component -public class LoginService +public class SysLoginService { @Autowired - private PasswordService passwordService; + private SysPasswordService passwordService; @Autowired private ISysUserService userService; diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/shiro/service/PasswordService.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/shiro/service/SysPasswordService.java similarity index 90% rename from ruoyi-framework/src/main/java/com/ruoyi/framework/shiro/service/PasswordService.java rename to ruoyi-framework/src/main/java/com/ruoyi/framework/shiro/service/SysPasswordService.java index 9e0653866..7a5c6ee3c 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/shiro/service/PasswordService.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/shiro/service/SysPasswordService.java @@ -22,7 +22,7 @@ import com.ruoyi.system.domain.SysUser; * @author ruoyi */ @Component -public class PasswordService +public class SysPasswordService { @Autowired private CacheManager cacheManager; @@ -84,7 +84,7 @@ public class PasswordService public static void main(String[] args) { - System.out.println(new PasswordService().encryptPassword("admin", "admin123", "111111")); - System.out.println(new PasswordService().encryptPassword("ry", "admin123", "222222")); + System.out.println(new SysPasswordService().encryptPassword("admin", "admin123", "111111")); + System.out.println(new SysPasswordService().encryptPassword("ry", "admin123", "222222")); } } diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/util/ShiroUtils.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/util/ShiroUtils.java index fd0695863..59cc1579f 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/util/ShiroUtils.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/util/ShiroUtils.java @@ -4,8 +4,6 @@ import org.apache.shiro.SecurityUtils; import org.apache.shiro.crypto.SecureRandomNumberGenerator; import org.apache.shiro.mgt.RealmSecurityManager; import org.apache.shiro.session.Session; -import org.apache.shiro.subject.PrincipalCollection; -import org.apache.shiro.subject.SimplePrincipalCollection; import org.apache.shiro.subject.Subject; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.bean.BeanUtils; @@ -37,7 +35,7 @@ public class ShiroUtils public static SysUser getUser() { SysUser user = null; - Object obj = getSubjct().getPrincipal(); + Object obj = getSession().getAttribute("sysUser"); if (StringUtils.isNotNull(obj)) { user = new SysUser(); @@ -46,14 +44,9 @@ public class ShiroUtils return user; } - public static void setUser(SysUser user) + public static void setSysUser(SysUser user) { - Subject subject = getSubjct(); - PrincipalCollection principalCollection = subject.getPrincipals(); - String realmName = principalCollection.getRealmNames().iterator().next(); - PrincipalCollection newPrincipalCollection = new SimplePrincipalCollection(user, realmName); - // 重新加载Principal - subject.runAs(newPrincipalCollection); + ShiroUtils.getSession().setAttribute("sysUser", user); } public static void clearCachedAuthorizationInfo() diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/base/BaseController.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/base/BaseController.java index 09dc83430..da2d1ded4 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/base/BaseController.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/base/BaseController.java @@ -1,5 +1,10 @@ package com.ruoyi.framework.web.base; +import java.beans.PropertyEditorSupport; +import java.util.Date; +import java.util.List; +import org.springframework.web.bind.WebDataBinder; +import org.springframework.web.bind.annotation.InitBinder; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.ruoyi.common.base.AjaxResult; @@ -10,11 +15,6 @@ import com.ruoyi.framework.web.page.PageDomain; import com.ruoyi.framework.web.page.TableDataInfo; import com.ruoyi.framework.web.page.TableSupport; import com.ruoyi.system.domain.SysUser; -import org.springframework.web.bind.WebDataBinder; -import org.springframework.web.bind.annotation.InitBinder; -import java.beans.PropertyEditorSupport; -import java.util.Date; -import java.util.List; /** * web层通用数据处理 @@ -127,23 +127,23 @@ public class BaseController return StringUtils.format("redirect:{}", url); } - public SysUser getUser() + public SysUser getSysUser() { - return ShiroUtils.getUser(); + return (SysUser) ShiroUtils.getSession().getAttribute("sysUser"); } - public void setUser(SysUser user) + public void setSysUser(SysUser user) { - ShiroUtils.setUser(user); + ShiroUtils.setSysUser(user); } public Long getUserId() { - return getUser().getUserId(); + return getSysUser().getUserId(); } public String getLoginName() { - return getUser().getLoginName(); + return getSysUser().getLoginName(); } }