[d14176d] | 1 | package project.fmo.app.projcetfmo.config;
|
---|
| 2 |
|
---|
| 3 | import org.springframework.security.authentication.AuthenticationProvider;
|
---|
| 4 | import org.springframework.security.authentication.BadCredentialsException;
|
---|
| 5 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
---|
| 6 | import org.springframework.security.core.Authentication;
|
---|
| 7 | import org.springframework.security.core.AuthenticationException;
|
---|
| 8 | import org.springframework.security.core.userdetails.UserDetails;
|
---|
| 9 | import org.springframework.security.crypto.password.PasswordEncoder;
|
---|
| 10 | import org.springframework.stereotype.Component;
|
---|
| 11 | import project.fmo.app.projcetfmo.Service.KorisnikService;
|
---|
| 12 |
|
---|
| 13 | @Component
|
---|
| 14 | public class CustomUsernamePasswordAuthenticationProvider implements AuthenticationProvider {
|
---|
| 15 |
|
---|
| 16 | private final KorisnikService korisnikService;
|
---|
| 17 | private final PasswordEncoder passwordEncoder;
|
---|
| 18 |
|
---|
| 19 | public CustomUsernamePasswordAuthenticationProvider(KorisnikService korisnikService, PasswordEncoder passwordEncoder) {
|
---|
| 20 | this.korisnikService = korisnikService;
|
---|
| 21 | this.passwordEncoder = passwordEncoder;
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 |
|
---|
| 25 | @Override
|
---|
| 26 | public Authentication authenticate(Authentication authentication) throws AuthenticationException {
|
---|
| 27 | String username = authentication.getName();
|
---|
| 28 | String password = authentication.getCredentials().toString();
|
---|
| 29 |
|
---|
| 30 | if ("".equals(username) || "".equals(password)) {
|
---|
| 31 | throw new BadCredentialsException("Invalid Credentials");
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | UserDetails userDetails = this.korisnikService.loadUserByUsername(username);
|
---|
| 35 |
|
---|
| 36 | if (!password.equals(userDetails.getPassword())) {
|
---|
| 37 | throw new BadCredentialsException("Password is incorrect!");
|
---|
| 38 | }
|
---|
| 39 | return new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(), userDetails.getAuthorities());
|
---|
| 40 |
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | @Override
|
---|
| 44 | public boolean supports(Class<?> authentication) {
|
---|
| 45 | return authentication.equals(UsernamePasswordAuthenticationToken.class);
|
---|
| 46 | }
|
---|
[1dd9226] | 47 | }
|
---|