인프런 커뮤니티 질문&답변
작성자 없음
작성자 정보가 삭제된 글입니다.
BCryptPasswordEncoder 관련 Error
작성
·
3K
0
package com.junyharang.jwtstudy.config.jwt;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.junyharang.jwtstudy.auth.PrincipalDetails;
import com.junyharang.jwtstudy.model.Member;
import lombok.RequiredArgsConstructor;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
// 스프링 시큐리티에 UsernamePasswordAuthenticationFilter가 있다.
// /login이 요청 오면 username, password를 전송하면 (Post로)
// UsernamePasswordAuthenticationFilter가 동작한다.
// 현재는 SecurityConfig에서 FormLogin을 disable을 시켜서 동작하지 않는다.
@RequiredArgsConstructor
public class JwtAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
private final AuthenticationManager authenticationManager;
@Override // /login 요청이 들어오면 로그인 시도를 위해 실행되는 Method
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
System.out.println("JwtAuthenticationFilter : 로그인 시도 중 입니다!");
// 1. username, password를 받는다.
try {
// BufferedReader reader = request.getReader();
//
// String input = null;
//
// while ((input = reader.readLine()) != null) {
// System.out.println(input);
// } // while 문 끝
// JSON으로 전달된 값을 Parsing 할수 있게 해주는 객체 생성
ObjectMapper om = new ObjectMapper();
Member member = om.readValue(request.getInputStream(), Member.class);
System.out.println(member);
// Token 생성(회원의 이름과 Password를 통해)
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(member.getUsername(), member.getPassword());
// PrincipalDetailsService의 loadUserByUsername()이 실행 된다.
// authenticationManager에 Token을 넣어 호출한다.
// authentication 변수에는 Login 정보가 담긴다.
Authentication authentication = authenticationManager.authenticate(authenticationToken);
// authentication 객체가 Session 영역에 저장된다.
PrincipalDetails principal = (PrincipalDetails) authentication.getPrincipal();
// 출력이 된다면 Login이 되었다는 의미
System.out.println(principal.getMember().getUsername());
return authentication;
} catch (IOException e) {
e.printStackTrace();
} // try-cache 문 끝
System.out.println("===========================================");
// 2. authenticationManager로 정상 인지 로그인 시도하면 PrincipalDetailsService가 호출된다.
// 해당 Class안에 loadUserByUsername()가 자동 호출
// 3. PrincipalDetails를 Session에 담는다.
// Session에 해당 내용을 담는 이유는 담지 않으면 권한에 대한 처리를 할 수 없기 때문이다.
// 4. JWT를 만들어서 응답해 준다.
return null;
} // attemptAuthentication(HttpServletRequest request, HttpServletResponse response) 끝
} // class 끝
안녕하세요? 25강에 해당 부분을 하다가 DB에 회원값이 들어가 있는 걸 보고, 회원가입 로직이 누락 되었다는 부분을 찾기 위해 Controller를 보고 아래 내용을 추가 하였습니다.
@PostMapping("join") public String join(@RequestBody Member member) {
member.setPassword(bCryptPasswordEncoder.encode(member.getPassword()));
member.setRolse("ROLE_USER");
memberRepository.save(member);
return "회원가입완료";
}
그리고 나서 SecurityConfig에 아래 내용을 추가 하였구요.
@Autowired private MemberRepository memberRepository;
@Autowired private CorsConfig corsConfig;
@Bean public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
} // passwordEncoder() 끝
private final CorsFilter corsFilter;
근데 아래와 같은 Error가 발생 합니다.
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2022-01-19 00:25:02.184 ERROR 17752 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: Parameter 1 of constructor in com.junyharang.jwtstudy.controller.RestAPIController required a bean of type 'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder' that could not be found. Action: Consider defining a bean of type 'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder' in your configuration.






