Spring Security密码编码器需要bean,即使它已经在配置中

gab6jxml  于 2023-03-02  发布在  Spring
关注(0)|答案(1)|浏览(187)

我正在使用Spring Security和JWT创建一个身份验证方法来保护我的API。我正在使用register方法的密码编码器来保护用户。我已经为它声明了一个bean,但是我收到了一个错误消息:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 1 of constructor in com.akuntansi.akuntansi.repo.AuthenticationService required a bean of type 'org.springframework.security.crypto.password.PasswordEncoder' that could not be found.

Action:

Consider defining a bean of type 'org.springframework.security.crypto.password.PasswordEncoder' in your configuration.

下面是我的配置,得到了所有的bean::

package com.akuntansi.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import com.akuntansi.akuntansi.repo.UserRepo;

import lombok.RequiredArgsConstructor;

@Configuration
@RequiredArgsConstructor
public class AppConfig {

    private final UserRepo userRepo;

    @Bean
    public UserDetailsService userDetailsService(){
        return username -> userRepo.findByUsername(username).orElseThrow(() -> new UsernameNotFoundException("User not found!"));
    }

    @Bean
    public AuthenticationProvider authenticationProvider(){
        DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
        authProvider.setUserDetailsService(userDetailsService());
        authProvider.setPasswordEncoder(passwordEncoder());

        return authProvider;
    }
    
    @Bean
     public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception{
        return config.getAuthenticationManager();
    }
}

下面是AuthenticationService.java

package com.akuntansi.akuntansi.repo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import com.akuntansi.akuntansi.model.Role;
import com.akuntansi.akuntansi.model.Users;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class AuthenticationService {
    
private final UserRepo userRepo;
private final PasswordEncoder passwordEncoder;
private final JWTService jwtService;
private final AuthenticationManager authenticationManager;

    public AuthenticationResponse register(RegisterRequest request){
        var user =  Users.builder()
                    .username(request.getUsername())
                    .password(passwordEncoder.encode(request.getPassword()))
                    .role(Role.USER)
                    .build();
                    
        userRepo.save(user);
        var jwtToken = jwtService.generateJwtToken(user);

        return AuthenticationResponse.builder()
                .token(jwtToken) 
                .build();
    }

    public AuthenticationResponse login(AuthenticationRequest request){
        authenticationManager.authenticate(
            new UsernamePasswordAuthenticationToken(
                request.getUsername(),
                request.getPassword()
            )
        );
        var user = userRepo.findByUsername(request.getUsername()).orElseThrow();
        var jwtToken = jwtService.generateJwtToken(user);
        return AuthenticationResponse.builder()
                .token(jwtToken)
                .build();
    }
}

我不知道出了什么问题,因为正如您所看到的,我已经为它创建了一个bean类型。

moiiocjp

moiiocjp1#

尝试使用“@Resource”或“@Autowired”,而不是“@RequiredArgsConstructor”

相关问题