CustomAuthenticationProvider.java
1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.daeucna.board.security;
import org.springframework.beans.factory.annotation.Autowired;
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.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;
import com.daeucna.board.security.domain.UserDto;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
@Autowired
private CustomLoadUserByUsername customLoadUserByUsername;
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
UserDto user = (UserDto) customLoadUserByUsername.loadUserByUsername(authentication.getName().toString());
String reqPassword = authentication.getCredentials().toString();
if(!passwordEncoder.matches(reqPassword, user.getPassword())) {
log.error("login Check Error");
log.info("enc:" + bCryptPasswordEncoder.encode(reqPassword));
throw new BadCredentialsException("Not Found User");
}
return new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities());
}
@Override
public boolean supports(Class<?> authentication) {
return true;
}
}