@Override public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException { UserEntity userEntity = userService.getInfoByUsername(s); if (ObjectUtils.isEmpty(userEntity)) { throw new UsernameNotFoundException(String.format("this user %s not found", s)); } // 封装权限信息 List<GrantedAuthority> authorities = new ArrayList<>(); userEntity.getUserRoles().forEach(v -> authorities.add(new SimpleGrantedAuthority(v.getRoleName()))); UserDetails userDetails = new User(userEntity.getUsername(), userEntity.getPassword(), authorities); return userDetails; }
@Override public Integer updatePassword(UserEntity entity) { return userRepository.updateByPassword(entity.getId(), entity.getPassword()); }
@PreAuthorize("hasAuthority(('USER')) && hasPermission(#entity.id, 'update|user')") @RequestMapping(value = "user/update", method = RequestMethod.PUT) CommonResult<UserEntity> update(@RequestBody UserEntity entity) { Assert.notNull(entity, MessageEnums.PARAMS_NOT_NULL.getValue()); UserEntity targetUserEntity = userService.getUserById(entity.getId()); entity.setPassword(targetUserEntity.getPassword()); return CommonResult.success(userService.update(entity)); }
@Override public UserEntity update(UserEntity entity) { entity.setPassword(userRepository.findByUsername(entity.getUsername()).getPassword()); return userRepository.save(entity); }
@PreAuthorize("hasAuthority(('USER')) && hasPermission(#param.id, 'update|user')") @RequestMapping(value = "user/update/password", method = RequestMethod.PUT) CommonResult<UserEntity> updatePassword(@RequestBody @Validated UserParamForPassword param) { Assert.notNull(param, MessageEnums.PARAMS_NOT_NULL.getValue()); // 抽取邮箱是否为当前用户设置的邮箱地址 UserEntity user = userService.getUserById(param.getId()); if (!param.getEmail().equals(user.getEmail())) { return CommonResult.validateError(MessageEnums.USER_EMAIL_NOT_AGREE); } // 抽取原密码是否正确 if (!ShaUtils.hash256(param.getPassword()).equals(user.getPassword())) { return CommonResult.validateError(MessageEnums.USER_PASSWORD_INPUT_ERROR); } // 抽取修改后密码是否与原密码一致 if (param.getRepassword().equals(param.getPassword())) { return CommonResult.validateError(MessageEnums.USER_PASSWORD_INPUT_SAME); } UserEntity entity = new UserEntity(); entity.setId(param.getId()); entity.setPassword(ShaUtils.hash256(param.getRepassword())); return CommonResult.success(userService.updatePassword(entity)); }