在aside.jsp中的“系统管理”部分下有用户管理/角色管理/资源权限管理/访问日志,点击用户管理要能查询出所有的用户
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("/findAll.do")
public ModelAndView findAll() throws Exception {
ModelAndView mv = new ModelAndView();
List userList = userService.findAll();
//这个值的确定要和前端一致
mv.addObject("userList", userList);
mv.setViewName("user-list");
return mv;
}
List findAll() throws Exception;
UserServiceImpl
@Override
public List findAll() throws Exception {
return userDao.findAll();
}
修改用户状态的字符串表示(0代表未开启,1代表已开启)
添加完后跳转到所有用户页面
@RequestMapping("/save.do")
public String save(UserInfo userInfo) throws Exception {
userService.save(userInfo);
return "redirect:findAll.do";
}
UserServiceImpl
@Override
public void save(UserInfo userInfo) throws Exception {
//对密码进行加密处理
userInfo.setPassword(bCryptPasswordEncoder.encode(userInfo.getPassword()));
userDao.save(userInfo);
}
@Insert("insert into users(email,username,password,phoneNum,status) values(#{email},#{username},#{password},#{phoneNum},#{status})")
void save(UserInfo userInfo) throws Exception;
在spring-security,xml中声明了一个bean,同时修改ss的配置,配置加密方式
在heima_ssm_utils对应包下新建一个类BCryptPasswordEncode
package com.itheima.ssm.utils;
//导入该包,springspringframework.security提供了该类
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
//底层做了盐值加密(保证每次加密的过程都不一样,但是解密可以得到一样的结果)
public class BCryptPasswordEncoderUtils {
private static BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();
public static String encodePassword(String password){
return bCryptPasswordEncoder.encode(password);
}
public static void main(String[] args) {
String password="123";
String pwd = encodePassword(password);
//$2a$10$tJHudmJh6MRPdiL7mv0yfe0nZJbDHuhl7sSTnqNC4DauMik9ppi4K
//$2a$10$Ce8LB3jdYDZ2f6HB281zA.4eC7v6ziJdK8MMWg0Yu8ETMg5ToMpIe
System.out.print(pwd.length());
}
}
在UserServiceImple中先
@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
再加密后重新赋值
@Override
public void save(UserInfo userInfo) throws Exception {
//对密码进行加密处理
userInfo.setPassword(bCryptPasswordEncoder.encode(userInfo.getPassword()));
userDao.save(userInfo);
}
//查询指定id的用户
@RequestMapping("/findById.do")
public ModelAndView findById(String id) throws Exception{
ModelAndView mv = new ModelAndView();
UserInfo userInfo = userService.findById(id);
mv.addObject("user",userInfo);
mv.setViewName("user-show1");
return mv;
}
UserInfo findById(String id) throws Exception;
UserServiceImpl
@Override
public UserInfo findById(String id) throws Exception{
return userDao.findById(id);
}
@Select("select * from users where id=#{id}")
@Results({
@Result(id = true, property = "id", column = "id"),
@Result(property = "username", column = "username"),
@Result(property = "email", column = "email"),
@Result(property = "password", column = "password"),
@Result(property = "phoneNum", column = "phoneNum"),
@Result(property = "status", column = "status"),
@Result(property = "roles",column = "id",javaType = List.class,many = @Many(select = "com.itheima.ssm.dao.IRoleDao.findRoleByUserId"))
})
UserInfo findById(String id) throws Exception;
@Results注解用于对查询结果进行封装
之前在权限管理中已经实现了该方法,但是此处还是需要对查询结果进行一定封装
//根据用户id查询出所有对应的角色
@Select("select * from role where id in (select roleId from users_role where userId=#{userId})")
@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"))
})
@Select("select * from permission where id in (select permissionId from role_permission where roleId=#{id} )")
public List findPermissionByRoleId(String id) throws Exception;