-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix:#25 AuthenticationProvider 구현해서 로그인 처리
- Loading branch information
1 parent
50891ed
commit 17e8064
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
src/main/java/com/t3t/authenticationapi/account/component/CustomAuthenticationProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |