Spring/Spring err

[Spring err] 스프링 시큐리티 - Consider defining a bean of type 'org.springframework.security.crypto.password.PasswordEncoder' in your configuration.

민돌v 2022. 4. 20. 18:12

Parameter 1 of constructor in com.example.zaritalk.service.UserService required a bean of type 'org.springframework.security.crypto.password.PasswordEncoder' that could not be found.

 

package com.example.zaritalk.service;

import com.example.zaritalk.domain.user.User;
import com.example.zaritalk.domain.user.UserRole;
import com.example.zaritalk.dto.SignupRequestDto;
import com.example.zaritalk.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.security.crypto.password.PasswordEncoder;
import java.util.Optional;

@Service
@RequiredArgsConstructor
public class UserService {
    private final UserRepository userRepository;
    private final PasswordEncoder passwordEncoder;

    public User registerUser(SignupRequestDto requestDto) {
        String nickname = requestDto.getNickname();
        Optional<User> found = userRepository.findByNickname(nickname);
        if (found.isPresent()) {
            throw new IllegalArgumentException("중복된 사용자 ID 가 존재합니다.");
        }
        String password = passwordEncoder.encode(requestDto.getPassword());
        UserRole role = UserRole.valueOf(requestDto.getUserRole());

        User user = User.builder()
                .nickname(nickname)
                .password(password)
                .account_type(role)
                .build();
        userRepository.save(user);

        return user;
    }

}

 

=> passwordEncoder 가 빈으로 등록되지 않아서 생기는 문제

(BCryptPasswordEncoder를 Super Class로 한 Class가 없어서)

 

1. WebCoSecurityConfig에 BCryptPasswordEncoder를 빈으로 등록하거나 

package com.example.zaritalk.security;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        http.headers().frameOptions().disable();
        http.authorizeRequests()
                .antMatchers("/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .defaultSuccessUrl("/").permitAll()
                .and()
                .logout()
                .permitAll();
    }


    //passwordEncoder
    @Bean
    public BCryptPasswordEncoder encodePassword() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}

 

2. passwordEncoder를 빈으로 등록하거나

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

 

 

 

참고 

https://okky.kr/article/613221?note=1762220