//查询用户以及用户可以添加的角色
@RequestMapping("/findUserByIdAndAllRole.do")
public ModelAndView findUserByIdAndAllRole(@RequestParam(name = "id", required = true) String userid) throws Exception {
ModelAndView mv = new ModelAndView();
//1.根据用户id查询用户
UserInfo userInfo = userService.findById(userid);
//2.根据用户id查询可以添加的角色
List otherRoles = userService.findOtherRoles(userid);
mv.addObject("user", userInfo);
mv.addObject("roleList", otherRoles);
mv.setViewName("user-role-add");
return mv;
}
List findOtherRoles(String userId) throws Exception;
@Override
public List findOtherRoles(String userId) {
return userDao.findOtherRoles(userId);
}
@Select("select * from role where id not in (select roleId from users_role where userId=#{userId})")
List findOtherRoles(String userId);
user-role-add.jsp
在/系统管理/用户管理/中,点击“添加角色”,会跳转到user-role-add.jsp,显示出可以添加的角色,进行勾选选择添加后,后续去修改user_role表
一个参数为userid,另一个参数为role数组
//给用户添加角色
@RequestMapping("/addRoleToUser.do")
public String addRoleToUser(@RequestParam(name = "userId", required = true) String userId, @RequestParam(name = "ids", required = true) String[] roleIds) {
userService.addRoleToUser(userId, roleIds);
return "redirect:findAll.do";
}
void addRoleToUser(String userId, String[] roleIds);
UserServiceImpl
@Override
public void addRoleToUser(String userId, String[] roleIds) {
// FOR循环进行添加
for(String roleId:roleIds){
userDao.addRoleToUser(userId,roleId);
}
}
@Insert("insert into users_role(userId,roleId) values(#{userId},#{roleId})")
void addRoleToUser(@Param("userId") String userId, @Param("roleId") String roleId);
注意添加完后,进行的是/user/findAll.do
//根据roleId查询role,并查询出可以添加的权限
@RequestMapping("/findRoleByIdAndAllPermission.do")
public ModelAndView findRoleByIdAndAllPermission(@RequestParam(name = "id", required = true) String roleId) throws Exception {
ModelAndView mv = new ModelAndView();
//根据roleId查询role
Role role = roleService.findById(roleId);
//根据roleId查询可以添加的权限
List otherPermissions = roleService.findOtherPermissions(roleId);
mv.addObject("role", role);
mv.addObject("permissionList", otherPermissions);
mv.setViewName("role-permission-add");
return mv;
}
Role findById(String roleId) throws Exception;
List findOtherPermissions(String roleId) throws Exception;
@Select("select * from role where id=#{roleId}")
@Results({
@Result(id = true,property = "id",column = "id"),
@Result(property = "roleName",column = "roleName"),
@Result(property = "roleDesc",column = "roleDesc"),
@Result(property = "permissions",column = "id",javaType = List.class,many = @Many(select = "com.itheima.ssm.dao.IPermissionDao.findPermissionByRoleId"))
})
Role findById(String roleId);
@Select("select * from permission where id not in (select permissionId from role_permission where roleId=#{roleId})")
List findOtherPermissions(String roleId);
-1. spring-srcurity.xml中开启
开启注解后,该注解提供三个
@RolesAllowed表示访问对应方法时所应该具有的角色
示例: @RolesAllowed({"USER", "ADMIN"})
该方法只要具有"USER", "ADMIN"任意一种权限就可以访问。这里可以省略前缀ROLE_,实际的权限可能是ROLE_ADMIN
@PermitAll表示允许所有的角色进行访问,也就是说不进行权限控制
@DenyAll是和PermitAll相反的,表示无论什么角色都不能访问
2 . 在指定方法上加上@RolesAllowed并在value指定权限
在jsp页面中我们可以使用spring security提供的权限标签来进行权限控制