sysusercontrolelr 修改防止 低权限 该高查看数据的 数据

pull/92/head
duandazhi 4 years ago
parent 4e0301a7b2
commit d8a899f0e9

@ -5,6 +5,10 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import com.ruoyi.common.core.exception.CustomException;
import com.ruoyi.common.security.service.TokenService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.DeleteMapping;
@ -56,6 +60,9 @@ public class SysUserController extends BaseController
@Autowired @Autowired
private ISysPermissionService permissionService; private ISysPermissionService permissionService;
@Autowired
private TokenService tokenService;
/** /**
* *
*/ */
@ -146,6 +153,15 @@ public class SysUserController extends BaseController
@GetMapping(value = { "/", "/{userId}" }) @GetMapping(value = { "/", "/{userId}" })
public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId) public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId)
{ {
if (userId == null) {
return AjaxResult.error("userId不能为空");
}
// 用户信息可以被爆破 dazer
//安全漏洞测试fix增加防止越权的操作不法分子可能通过修改 userid 抓取、修改、删除、重置 任意用户敏感信息 (1 getInfo)
if (!this.checkUserIdAllowed(userId)) {
return AjaxResult.error("请勿非法操作你无权操作该用户userId = " + userId );
}
AjaxResult ajax = AjaxResult.success(); AjaxResult ajax = AjaxResult.success();
List<SysRole> roles = roleService.selectRoleAll(); List<SysRole> roles = roleService.selectRoleAll();
ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList())); ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
@ -205,6 +221,15 @@ public class SysUserController extends BaseController
{ {
return AjaxResult.error("修改用户'" + user.getUserName() + "'失败,邮箱账号已存在"); return AjaxResult.error("修改用户'" + user.getUserName() + "'失败,邮箱账号已存在");
} }
// 用户信息可以被爆破 dazer
if (user.getUserId() == null) {
return AjaxResult.error("userId不能为空");
}
if (!this.checkUserIdAllowed(user.getUserId())) {
return AjaxResult.error("请勿非法操作你无权操作该用户userId = " + user.getUserId() );
}
user.setUpdateBy(SecurityUtils.getUsername()); user.setUpdateBy(SecurityUtils.getUsername());
return toAjax(userService.updateUser(user)); return toAjax(userService.updateUser(user));
} }
@ -217,6 +242,18 @@ public class SysUserController extends BaseController
@DeleteMapping("/{userIds}") @DeleteMapping("/{userIds}")
public AjaxResult remove(@PathVariable Long[] userIds) public AjaxResult remove(@PathVariable Long[] userIds)
{ {
if (ArrayUtils.contains(userIds, SecurityUtils.getUserId())) {
return AjaxResult.error("当前用户不能删除");
}
// 用户信息可以被爆破 dazer
for (int i = 0; i < userIds.length; i++) {
Long userId = userIds[i];
if (userId != null) {
if (!this.checkUserIdAllowed(userId)) {
return AjaxResult.error("请勿非法操作你无权操作该用户userId = " + userId );
}
}
}
return toAjax(userService.deleteUserByIds(userIds)); return toAjax(userService.deleteUserByIds(userIds));
} }
@ -229,9 +266,21 @@ public class SysUserController extends BaseController
public AjaxResult resetPwd(@RequestBody SysUser user) public AjaxResult resetPwd(@RequestBody SysUser user)
{ {
userService.checkUserAllowed(user); userService.checkUserAllowed(user);
user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
user.setUpdateBy(SecurityUtils.getUsername()); // 用户信息可以被爆破 dazer
return toAjax(userService.resetPwd(user)); if (user.getUserId() == null) {
return AjaxResult.error("userId不能为空");
}
if (!this.checkUserIdAllowed(user.getUserId())) {
return AjaxResult.error("请勿非法操作你无权操作该用户userId = " + user.getUserId() );
}
//修改密码接口,只进行密码修改;防止 通过 修改密码 接口 把用户其他信息进行了修改
SysUser newSyuser = new SysUser();
newSyuser.setUserId(user.getUserId());
newSyuser.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
newSyuser.setUpdateBy(tokenService.getLoginUser().getUsername());
return toAjax(userService.resetPwd(newSyuser));
} }
/** /**
@ -273,4 +322,21 @@ public class SysUserController extends BaseController
userService.insertUserAuth(userId, roleIds); userService.insertUserAuth(userId, roleIds);
return success(); return success();
} }
/**
* @author dazer
* 1userIduserid
* 2roleidroleid
*
* @param userId userId
*/
private boolean checkUserIdAllowed(Long userId) {
if (userId == null) {
throw new CustomException("checkUserIdAllowed中【userId】不能为空");
}
SysUser query = new SysUser();
query.setUserId(userId);
List<SysUser> sysUsers = userService.selectUserList(query);
return sysUsers.stream().map(SysUser::getUserId).collect(Collectors.toSet()).contains(userId);
}
} }

Loading…
Cancel
Save