extend Spring Security UserDetailService class
This commit is contained in:
parent
684954efee
commit
5aa2ff89c0
@ -2,11 +2,12 @@ package com.example.springdemo.service;
|
||||
|
||||
import com.example.springdemo.entities.Users;
|
||||
import com.example.springdemo.entities.password.UsersPassword;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface UsersService {
|
||||
public interface UsersService extends UserDetailsService {
|
||||
|
||||
Users addUser(Users user, UsersPassword userPassword);
|
||||
|
||||
|
@ -1,17 +1,31 @@
|
||||
package com.example.springdemo.service;
|
||||
package com.example.springdemo.serviceImpl;
|
||||
|
||||
import com.example.springdemo.dao.UsersPasswordRepository;
|
||||
import com.example.springdemo.dao.UsersRepository;
|
||||
import com.example.springdemo.entities.RBAC.Permissions;
|
||||
import com.example.springdemo.entities.RBAC.Roles;
|
||||
import com.example.springdemo.entities.Users;
|
||||
import com.example.springdemo.entities.password.UsersPassword;
|
||||
import com.example.springdemo.service.PermissionsService;
|
||||
import com.example.springdemo.service.UsersService;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@Component
|
||||
public class UsersServiceImpl implements UsersService {
|
||||
@Resource
|
||||
private UsersRepository usersRepository;
|
||||
@ -19,6 +33,9 @@ public class UsersServiceImpl implements UsersService {
|
||||
@Resource
|
||||
private UsersPasswordRepository usersPasswordRepository;
|
||||
|
||||
@Resource
|
||||
private PermissionsService permissionsService;
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
//添加用户时同步添加密码
|
||||
@ -74,9 +91,38 @@ public class UsersServiceImpl implements UsersService {
|
||||
}
|
||||
|
||||
@Override
|
||||
@Modifying
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
//更新用户密码
|
||||
public int updateUserPasswordById(String password, Long userId) {
|
||||
return usersPasswordRepository.updatePassword(password, userId);
|
||||
}
|
||||
|
||||
//根据用户名获取用户信息
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||
Collection<GrantedAuthority> authorities = new ArrayList<>();
|
||||
// 从数据库中取出用户信息
|
||||
Users user = usersRepository.findByName(username).orElse(null);
|
||||
// 判断用户是否存在
|
||||
if (user == null) {
|
||||
throw new UsernameNotFoundException("用户名不存在");
|
||||
}
|
||||
// 取出密码
|
||||
String password = usersPasswordRepository.findByUsersId(user.getId()).getPassword();
|
||||
|
||||
// 用户角色
|
||||
Roles role = user.getRoles();
|
||||
authorities.add(new SimpleGrantedAuthority("ROLE_".concat(role.getName())));
|
||||
|
||||
//用户权限
|
||||
List<Permissions> permissions = permissionsService.findPermissionsByRoleId(role.getId());
|
||||
for (Permissions permission : permissions) {
|
||||
SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(permission.getName());
|
||||
authorities.add(simpleGrantedAuthority);
|
||||
}
|
||||
|
||||
// 返回UserDetails实现类
|
||||
return new User(user.getName(), "{noop}" + password, authorities);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user