인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

인프런 커뮤니티 질문&답변

대배랙님의 프로필 이미지
대배랙

작성한 질문수

스프링 시큐리티

5) DB 연동 인증 처리(2) : CustomAuthenticationProvider

provider안쪽에

작성

·

267

0


log.info("username : "+username);
log.info("password : "+password );

  AccountContext accountContext = (AccountContext) userDetailsService.loadUserByUsername(username);

이부분에서

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception

java.lang.NullPointerException: null

이에러가나는데

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    log.info("로그로그로그");

여기 자체로 넘어오지를 않습니다.

어떤거를 손봐야할까요..

@RequiredArgsConstructor
public class CustomUserDetailsService implements UserDetailsService {

    private final UserRepository userRepository;

지금 보통 RequiredArgsConstructor 로 초기화중입니다.

빼고 해봐도 별차이가없네요..

 

@Getter
public class AccountContext extends User {

    private Account account;

    public AccountContext(Account account, ArrayList<GrantedAuthority> roles) {
        super(account.getUsername(), account.getPassword(), roles);
        this.account = account;
    }

}

AccountContext 코드입니다.

제가 혼자 해보려고해도 시간이많이없어서 질문올립니다..

오래된강의인거는 알고있었지만 너무많이 바뀐거같아요..

 

답변 2

0

정수원님의 프로필 이미지
정수원
지식공유자

현재 AuthenticationProvider 객체를 생성하고 있는데 빈으로 정의하지 않고 있는 것 같습니다.

첫번째로 설정클래스에서 객체를 생성하고 있는데 @Bean 이 없습니다.

public AuthenticationProvider authenticationProvider() {
    return new CustomAuthenticationProvider(passwordEncoder());
}

 

그리고

@Slf4j
public class CustomAuthenticationProvider implements AuthenticationProvider {

 

직접 클래스를 정의할 때도 @Component 가 정의되지 않고 있습니다.

그래서 CustomUserDetailsService 를 주입받지 못하고 있어서 null 이 되고 있습니다.

결론은 AuthenticationProvider 객체를 생성할 때 빈으로 정의하시면 됩니다.

0

정수원님의 프로필 이미지
정수원
지식공유자

지금 실행하고 있는 소스를 공유 부탁드려요

제가 테스트 해 보겠습니다.

대배랙님의 프로필 이미지
대배랙
질문자

https://github.com/Baekwangyeol/nullpointexception

깃허브 생성해서 올렸습니다.! 오늘도 이것저것 했는데 안넘어가져서 올려용ㅠㅠ

대배랙님의 프로필 이미지
대배랙

작성한 질문수

질문하기