@RequestMapping("/user/") @Controller public class UserController { @Autowired private IUserService userService; @Autowired private IRoleService roleService; @Resource private ProcessEngineConfiguration configuration; @Resource private ProcessEngine engine; @GetMapping("/index") @ApiOperation("跳转用户页接口") @PreAuthorize("hasRole('管理员')") public String index(String menuid,Model model){ List用户详情信息服务层:roles = queryAllRole(); model.addAttribute("roles",roles); model.addAttribute("menuid",menuid); //用户首页 return "views/user/user_list"; } @GetMapping("/listpage") @ApiOperation("查询用户分页数据接口") @ApiImplicitParams({ @ApiImplicitParam(name = "UserQuery", value = "用户查询对象", defaultValue = "userQuery对象") }) @ResponseBody @PreAuthorize("hasRole('管理员')") public PageList listpage(UserQuery userQuery){ return userService.listpage(userQuery); } //添加用户 @PostMapping("/addUser") @ApiOperation("添加用户接口") @ResponseBody public Map addUser(User user){ Map ret = new HashMap<>(); ret.put("code",-1); if(StringUtils.isEmpty(user.getUsername())){ ret.put("msg","请填写用户名"); return ret; } if(StringUtils.isEmpty(user.getPassword())){ ret.put("msg","请填写密码"); return ret; } if(StringUtils.isEmpty(user.getEmail())){ ret.put("msg","请填写邮箱"); return ret; } if(StringUtils.isEmpty(user.getTel())){ ret.put("msg","请填写手机号"); return ret; } if(StringUtils.isEmpty(user.getHeadImg())){ ret.put("msg","请上传头像"); return ret; } if(userService.addUser(user)<=0) { ret.put("msg", "添加用户失败"); return ret; } ret.put("code",0); ret.put("msg","添加用户成功"); return ret; } @PostMapping("/editSaveUser") @ApiOperation("修改用户接口") @PreAuthorize("hasRole('管理员')") @ResponseBody public Message editSaveUser(User user){ if(StringUtils.isEmpty(user.getUsername())){ return Message.error("请填写用户名"); } if(StringUtils.isEmpty(user.getEmail())){ return Message.error("请填写邮箱"); } if(StringUtils.isEmpty(user.getTel())){ return Message.error("请填写手机号"); } try { userService.editSaveUser(user); return Message.success(); } catch (Exception e) { e.printStackTrace(); return Message.error("修改用户信息失败"); } } //添加用户 @GetMapping("/deleteUser") @ApiOperation("删除用户接口") @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "如:88",required = true) }) @PreAuthorize("hasRole('管理员')") @ResponseBody public AjaxResult deleteUser(@RequestParam(required = true) Long id){ AjaxResult ajaxResult = new AjaxResult(); try { userService.deleteUser(id); } catch (Exception e) { e.printStackTrace(); return new AjaxResult("删除失败"); } return ajaxResult; } @PostMapping(value="/deleteBatchUser") @ApiOperation("批量删除用户接口") @PreAuthorize("hasRole('管理员')") @ResponseBody public AjaxResult deleteBatchUser(String ids){ String[] idsArr = ids.split(","); List list = new ArrayList(); for(int i=0;i queryAllRole(){ return roleService.queryAll(); } //添加用户的角色 @PostMapping("/addUserRole") @ApiOperation("添加用户角色接口") @ApiImplicitParams({ @ApiImplicitParam(name = "paramMap", value = "如:{userId:1,[1,2,3,4]]}") }) @ResponseBody public AjaxResult addUserRole(@RequestBody Map paramMap){ AjaxResult ajaxResult = new AjaxResult(); String userId = (String)paramMap.get("userId"); List roleIds = (List) paramMap.get("roleIds"); try { //添加用户对应的角色 roleService.addUserRole(userId,roleIds); return ajaxResult; }catch (Exception e){ e.printStackTrace(); return new AjaxResult("保存角色失败"); } } //添加用户 @RequestMapping("/regSaveUser") @ResponseBody public Long addTeacher(User user){ System.out.println("保存用户...."+user); userService.addUser(user); //保存工作流程 *** 作 IdentityService is = engine.getIdentityService(); // 添加用户组 org.activiti.engine.identity.User userInfo = userService.saveUser(is, user.getUsername()); // 添加用户对应的组关系 Group stuGroup = new GroupEntityImpl(); stuGroup.setId("stuGroup"); Group tGroup = new GroupEntityImpl(); tGroup.setId("tGroup"); if(user.getType() == 2) { //保存老师组 userService.saveRel(is, userInfo, tGroup); } if(user.getType() == 3) { //保存学生组 userService.saveRel(is, userInfo, stuGroup); } Long userId = user.getId(); return userId; } @RequestMapping(value="/update_pwd",method=RequestMethod.GET) public String updatePwd(){ return "views/user/update_pwd"; } @ResponseBody @PostMapping("/update_pwd") public Message updatePassword(@RequestParam(name="oldPwd",required=true)String oldPwd, @RequestParam(name="newPwd",required=true)String newPwd){ String username = CommonUtils.getLoginUser().getUsername(); User userByUserName = userService.findUserByUserName(username); if(userByUserName!=null){ String password = userByUserName.getPassword(); BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(); boolean matches = bCryptPasswordEncoder.matches(oldPwd, password); if(!matches){ return Message.error("旧密码不正确");//true } userByUserName.setPassword(bCryptPasswordEncoder.encode(newPwd)); if(userService.editUserPassword(userByUserName)<=0){ return Message.error("密码修改失败"); } } return Message.success(); } @ResponseBody @PostMapping("/clear_cache") public Message clearCache(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setHeader("Cache-Control","no-store"); response.setHeader("Pragrma","no-cache"); response.setDateHeader("Expires",0); return Message.success(); } }
@Component public class UserDetailsServiceImpl implements UserDetailsService { @Autowired private IUserService userService; @Autowired private IRoleService roleService; @Autowired private IPermissionService permissionService; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { //根据用户名查询出用户 User user = userService.findUserByUserName(username); if(user != null) { //查询角色集合 Long userid = user.getId(); Listroles = roleService.listRoleByUserId(userid); //查询权限集合 List permissions = permissionService.listPermissionByUserId(userid); //构建所有权限集合==ROLE_角色+权限 HashSet authorities = new HashSet (); for (Role role : roles) { authorities.add(new SimpleGrantedAuthority("ROLE_" + role.getName())); } for (Permission permission : permissions) { authorities.add(new SimpleGrantedAuthority(permission.getName())); } return new UserSecurity(user,authorities); }else{ return null; } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)