Springboot:在哪里扩展UserDetailsService?

问题描述 投票:0回答:1

我是springboot的新手,我潜入了一个项目来学习。我有一个 UserService 和一个 UserService java 文件。我需要使用 UserDetailsService 扩展,但我不知道在哪里扩展它?

用例:在单独的 JWTAUTHFILTER java 文件中检索用户名

if(userEmail != null && SecurityContextHolder.getContext().getAuthentication() == null){
          UserDetails userDetails = userService.findByUsername(userEmail);
            
        }

我的UserService.java



public interface UserService {
    UserDto createUser(UserDto user);
   
}

我的 Impl 类:



@Service
public class UserImpl implements UserService {

    private final UserRepository userRepository;
    private PasswordEncoder passwordEncoder;

    @Autowired
    public UserImpl(final UserRepository userRepository){
        this.userRepository = userRepository;
    }

    @Override
    public UserDto createUser(UserDto user) {
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        UserEntity userEntity = userToUserEntity(user);
        userRepository.save(userEntity);
        return user;
    }

    @Override
    public Optional<UserDto> findByUsername(String username) {
        Optional<UserEntity> userName = userRepository.findByUsername(username);
        return userName.map(user -> userEntityToUserModel(user));
    }

    

    private UserEntity userToUserEntity(final UserDto user){
        // UserEntity userEntity = new UserEntity();
        // userEntity.setUsername(user.getUsername());
        // userEntity.setId(user.getId());
        // userEntity.setPassword(user.getPassword());
        // userEntity.setRole(user.getRole()); 
        return UserEntity.builder()
                .id(user.getId())
                .username(user.getUsername())
                .role(user.getRole())
                .password(user.getPassword())
                .build();
    }

    private UserDto userEntityToUserModel (final UserEntity userEntity){
        return UserDto.builder()
                .id(userEntity.getId())
                .password(userEntity.getPassword())
                .role(userEntity.getRole())
                .username(userEntity.getUsername())
                .build();
    }

    



}

我很困惑,我正在使用的教程没有使用 DTO 和单独的服务和服务实现,有人可以帮助我吗。

java spring-boot authentication
1个回答
0
投票

您的

CustomUserDetailsService
可以如下所示:

public class CustomUserDetailsService implements UserDetailsService {
    // You can autowire your UserService class here

    @Override
    public UserDetails loadUserByUsername(String userEmail) {
        if(userEmail != null && SecurityContextHolder.getContext().getAuthentication() == null) {
          // In your initial implementation, you tried to store the value below in a UserService object, but your findByUsername() method returns an Optional<UserDto>
          Optional<UserDto> userDtoOptional = userService.findByUsername(userEmail);
          if (userDtoOptional.isEmpty()) { return null; }

          UserDto userDto = userDtoOptional.get();

          // Now, you need to convert your UserDto data into a UserDetails object. I'm going to assume that your UserDto class has a field called username and one called password. But you can customize it by your choice.
          return User.withUsername(userDto.getUsername()).password(user.getPassword()).build();
        }
    }
}

差不多就是这样了。 Spring Security 需要一个

UserDetailsService
类型的 bean 来检查用户登录时是否存在(它将整个 User 实体从存储库中取出,以便稍后在其逻辑中进行密码检查)。

© www.soinside.com 2019 - 2024. All rights reserved.