系统用户会话命名sysUser

pull/36/head
RuoYi 2018-11-18 18:02:20 +08:00
parent 6244e6bf18
commit d870710af1
8 changed files with 33 additions and 39 deletions

View File

@ -27,7 +27,7 @@ public class SysIndexController extends BaseController
public String index(ModelMap mmap) public String index(ModelMap mmap)
{ {
// 取身份信息 // 取身份信息
SysUser user = getUser(); SysUser user = getSysUser();
// 根据用户id取出菜单 // 根据用户id取出菜单
List<SysMenu> menus = menuService.selectMenusByUser(user); List<SysMenu> menus = menuService.selectMenusByUser(user);
mmap.put("menus", menus); mmap.put("menus", menus);

View File

@ -17,7 +17,7 @@ import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult; import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.config.Global; import com.ruoyi.common.config.Global;
import com.ruoyi.common.enums.BusinessType; 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.FileUploadUtils;
import com.ruoyi.framework.util.ShiroUtils; import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysUser; import com.ruoyi.system.domain.SysUser;
@ -42,7 +42,7 @@ public class SysProfileController extends BaseController
private ISysUserService userService; private ISysUserService userService;
@Autowired @Autowired
private PasswordService passwordService; private SysPasswordService passwordService;
@Autowired @Autowired
private ISysDictDataService dictDataService; private ISysDictDataService dictDataService;
@ -53,7 +53,7 @@ public class SysProfileController extends BaseController
@GetMapping() @GetMapping()
public String profile(ModelMap mmap) public String profile(ModelMap mmap)
{ {
SysUser user = getUser(); SysUser user = getSysUser();
user.setSex(dictDataService.selectDictLabel("sys_user_sex", user.getSex())); user.setSex(dictDataService.selectDictLabel("sys_user_sex", user.getSex()));
mmap.put("user", user); mmap.put("user", user);
mmap.put("roleGroup", userService.selectUserRoleGroup(user.getUserId())); mmap.put("roleGroup", userService.selectUserRoleGroup(user.getUserId()));
@ -65,7 +65,7 @@ public class SysProfileController extends BaseController
@ResponseBody @ResponseBody
public boolean checkPassword(String password) public boolean checkPassword(String password)
{ {
SysUser user = getUser(); SysUser user = getSysUser();
String encrypt = new Md5Hash(user.getLoginName() + password + user.getSalt()).toHex().toString(); String encrypt = new Md5Hash(user.getLoginName() + password + user.getSalt()).toHex().toString();
if (user.getPassword().equals(encrypt)) if (user.getPassword().equals(encrypt))
{ {
@ -91,7 +91,7 @@ public class SysProfileController extends BaseController
int rows = userService.resetUserPwd(user); int rows = userService.resetUserPwd(user);
if (rows > 0) if (rows > 0)
{ {
setUser(userService.selectUserById(user.getUserId())); setSysUser(userService.selectUserById(user.getUserId()));
return success(); return success();
} }
return error(); return error();
@ -127,7 +127,7 @@ public class SysProfileController extends BaseController
{ {
if (userService.updateUserInfo(user) > 0) if (userService.updateUserInfo(user) > 0)
{ {
setUser(userService.selectUserById(user.getUserId())); setSysUser(userService.selectUserById(user.getUserId()));
return success(); return success();
} }
return error(); return error();
@ -149,7 +149,7 @@ public class SysProfileController extends BaseController
user.setAvatar(avatar); user.setAvatar(avatar);
if (userService.updateUserInfo(user) > 0) if (userService.updateUserInfo(user) > 0)
{ {
setUser(userService.selectUserById(user.getUserId())); setSysUser(userService.selectUserById(user.getUserId()));
return success(); return success();
} }
} }

View File

@ -16,7 +16,7 @@ import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.enums.BusinessType; import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ExcelUtil; import com.ruoyi.common.utils.ExcelUtil;
import com.ruoyi.common.utils.StringUtils; 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.util.ShiroUtils;
import com.ruoyi.framework.web.page.TableDataInfo; import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.system.domain.SysUser; import com.ruoyi.system.domain.SysUser;
@ -46,7 +46,7 @@ public class SysUserController extends BaseController
private ISysPostService postService; private ISysPostService postService;
@Autowired @Autowired
private PasswordService passwordService; private SysPasswordService passwordService;
@RequiresPermissions("system:user:view") @RequiresPermissions("system:user:view")
@GetMapping() @GetMapping()

View File

@ -19,7 +19,7 @@ import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; 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.util.ShiroUtils;
import com.ruoyi.framework.web.exception.user.CaptchaException; import com.ruoyi.framework.web.exception.user.CaptchaException;
import com.ruoyi.framework.web.exception.user.RoleBlockedException; import com.ruoyi.framework.web.exception.user.RoleBlockedException;
@ -47,7 +47,7 @@ public class UserRealm extends AuthorizingRealm
private ISysRoleService roleService; private ISysRoleService roleService;
@Autowired @Autowired
private LoginService loginService; private SysLoginService loginService;
/** /**
* *
@ -127,7 +127,8 @@ public class UserRealm extends AuthorizingRealm
log.info("对用户[" + username + "]进行登录验证..验证未通过{}", e.getMessage()); log.info("对用户[" + username + "]进行登录验证..验证未通过{}", e.getMessage());
throw new AuthenticationException(e.getMessage(), e); 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; return info;
} }

View File

@ -27,10 +27,10 @@ import com.ruoyi.system.service.ISysUserService;
* @author ruoyi * @author ruoyi
*/ */
@Component @Component
public class LoginService public class SysLoginService
{ {
@Autowired @Autowired
private PasswordService passwordService; private SysPasswordService passwordService;
@Autowired @Autowired
private ISysUserService userService; private ISysUserService userService;

View File

@ -22,7 +22,7 @@ import com.ruoyi.system.domain.SysUser;
* @author ruoyi * @author ruoyi
*/ */
@Component @Component
public class PasswordService public class SysPasswordService
{ {
@Autowired @Autowired
private CacheManager cacheManager; private CacheManager cacheManager;
@ -84,7 +84,7 @@ public class PasswordService
public static void main(String[] args) public static void main(String[] args)
{ {
System.out.println(new PasswordService().encryptPassword("admin", "admin123", "111111")); System.out.println(new SysPasswordService().encryptPassword("admin", "admin123", "111111"));
System.out.println(new PasswordService().encryptPassword("ry", "admin123", "222222")); System.out.println(new SysPasswordService().encryptPassword("ry", "admin123", "222222"));
} }
} }

View File

@ -4,8 +4,6 @@ import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.SecureRandomNumberGenerator; import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.mgt.RealmSecurityManager; import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.session.Session; 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 org.apache.shiro.subject.Subject;
import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils; import com.ruoyi.common.utils.bean.BeanUtils;
@ -37,7 +35,7 @@ public class ShiroUtils
public static SysUser getUser() public static SysUser getUser()
{ {
SysUser user = null; SysUser user = null;
Object obj = getSubjct().getPrincipal(); Object obj = getSession().getAttribute("sysUser");
if (StringUtils.isNotNull(obj)) if (StringUtils.isNotNull(obj))
{ {
user = new SysUser(); user = new SysUser();
@ -46,14 +44,9 @@ public class ShiroUtils
return user; return user;
} }
public static void setUser(SysUser user) public static void setSysUser(SysUser user)
{ {
Subject subject = getSubjct(); ShiroUtils.getSession().setAttribute("sysUser", user);
PrincipalCollection principalCollection = subject.getPrincipals();
String realmName = principalCollection.getRealmNames().iterator().next();
PrincipalCollection newPrincipalCollection = new SimplePrincipalCollection(user, realmName);
// 重新加载Principal
subject.runAs(newPrincipalCollection);
} }
public static void clearCachedAuthorizationInfo() public static void clearCachedAuthorizationInfo()

View File

@ -1,5 +1,10 @@
package com.ruoyi.framework.web.base; 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.PageHelper;
import com.github.pagehelper.PageInfo; import com.github.pagehelper.PageInfo;
import com.ruoyi.common.base.AjaxResult; 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.TableDataInfo;
import com.ruoyi.framework.web.page.TableSupport; import com.ruoyi.framework.web.page.TableSupport;
import com.ruoyi.system.domain.SysUser; 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 * web
@ -127,23 +127,23 @@ public class BaseController
return StringUtils.format("redirect:{}", url); 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() public Long getUserId()
{ {
return getUser().getUserId(); return getSysUser().getUserId();
} }
public String getLoginName() public String getLoginName()
{ {
return getUser().getLoginName(); return getSysUser().getLoginName();
} }
} }