[0e4d807] | 1 | package com.example.baziproekt.config;
|
---|
| 2 |
|
---|
| 3 | import com.example.baziproekt.service.KorisniciService;
|
---|
| 4 | import org.springframework.security.authentication.AuthenticationProvider;
|
---|
| 5 | import org.springframework.security.authentication.BadCredentialsException;
|
---|
| 6 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
---|
| 7 | import org.springframework.security.core.Authentication;
|
---|
| 8 | import org.springframework.security.core.AuthenticationException;
|
---|
| 9 | import org.springframework.security.core.userdetails.UserDetails;
|
---|
| 10 | import org.springframework.security.crypto.password.PasswordEncoder;
|
---|
| 11 | import org.springframework.stereotype.Component;
|
---|
| 12 | import java.util.Objects;
|
---|
| 13 |
|
---|
| 14 |
|
---|
| 15 | @Component
|
---|
| 16 | public class CustomUsernamePasswordAuthenticationProvider implements AuthenticationProvider {
|
---|
| 17 |
|
---|
| 18 | private final KorisniciService userService;
|
---|
| 19 |
|
---|
| 20 | public CustomUsernamePasswordAuthenticationProvider(KorisniciService userService) {
|
---|
| 21 | this.userService = userService;
|
---|
| 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.userService.loadUserByUsername(username);
|
---|
| 35 |
|
---|
| 36 |
|
---|
| 37 |
|
---|
| 38 | if (!Objects.equals(password,userDetails.getPassword())) {
|
---|
| 39 | throw new BadCredentialsException("Password is incorrect!");
|
---|
| 40 | }
|
---|
| 41 | return new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(), userDetails.getAuthorities());
|
---|
| 42 |
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | @Override
|
---|
| 46 | public boolean supports(Class<?> aClass) {
|
---|
| 47 | return aClass.equals(UsernamePasswordAuthenticationToken.class);
|
---|
| 48 | }
|
---|
| 49 | } |
---|