Skip to content

Commit

Permalink
fix:#25 AuthenticationProvider 구현해서 로그인 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
joohyun1996 committed Apr 23, 2024
1 parent 50891ed commit 17e8064
Showing 1 changed file with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.t3t.authenticationapi.account.component;

import com.t3t.authenticationapi.account.auth.CustomUserDetails;
import com.t3t.authenticationapi.account.dto.UserEntity;
import com.t3t.authenticationapi.account.service.DefaultUserDetailsService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;


@Configuration
@RequiredArgsConstructor
public class CustomAuthenticationProvider implements AuthenticationProvider {
private final DefaultUserDetailsService userDetailsService;

@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder(){
return new BCryptPasswordEncoder();
}

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String username = authentication.getName();
String password = authentication.getCredentials().toString();

CustomUserDetails userDetails = (CustomUserDetails) userDetailsService.loadUserByUsername(username);

String dbPassword = userDetails.getPassword();
if(!bCryptPasswordEncoder().matches(password,dbPassword)){
throw new BadCredentialsException("id, pw not match");
}

return new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
}

@Override
public boolean supports(Class<?> authentication) {
return authentication.equals(UsernamePasswordAuthenticationToken.class);
}

}

0 comments on commit 17e8064

Please sign in to comment.