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
'Spring > Spring err' 카테고리의 다른 글
spring swagger "/error" 해결하기 (0) | 2022.10.18 |
---|---|
[Spring Controller test Error] - Invalid use of argument matchers! - "Mock argument matcher 에러" (0) | 2022.05.26 |
[Spring] JPA FetchType.LAZY 시 JSON에러 (0) | 2022.03.23 |
[Spring err] com.amazonaws.SdkClientException: Failed to connect to service endpoint 에러 -(aws 의존성 주입시 에러) (2) | 2022.03.18 |
인텔리제이 spring boot 실행 에러 (0) | 2022.01.15 |