From 17e80640c0cdb2794c4150fd880be05b3a038d11 Mon Sep 17 00:00:00 2001 From: JooHyunLee Date: Tue, 23 Apr 2024 18:07:26 +0900 Subject: [PATCH] =?UTF-8?q?fix:#25=20AuthenticationProvider=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=ED=95=B4=EC=84=9C=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CustomAuthenticationProvider.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/main/java/com/t3t/authenticationapi/account/component/CustomAuthenticationProvider.java diff --git a/src/main/java/com/t3t/authenticationapi/account/component/CustomAuthenticationProvider.java b/src/main/java/com/t3t/authenticationapi/account/component/CustomAuthenticationProvider.java new file mode 100644 index 0000000..6266050 --- /dev/null +++ b/src/main/java/com/t3t/authenticationapi/account/component/CustomAuthenticationProvider.java @@ -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); + } + +}