source: bus-n-go-do-5ta-faza/src/main/java/mk/ukim/finki/busngo/config/CustomUsernamePasswordAuthenticationProvider.java

Last change on this file was baf4cc4, checked in by ppaunovski <paunovskipavel@…>, 3 months ago

split group project and individual project into two separate folders

  • Property mode set to 100644
File size: 1.9 KB
Line 
1package mk.ukim.finki.busngo.config;
2
3import mk.ukim.finki.busngo.service.KorisnikService;
4import org.springframework.security.authentication.AuthenticationProvider;
5import org.springframework.security.authentication.BadCredentialsException;
6import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
7import org.springframework.security.core.Authentication;
8import org.springframework.security.core.AuthenticationException;
9import org.springframework.security.core.userdetails.UserDetails;
10import org.springframework.security.crypto.password.PasswordEncoder;
11import org.springframework.stereotype.Component;
12
13@Component
14public class CustomUsernamePasswordAuthenticationProvider implements AuthenticationProvider {
15
16 private final KorisnikService userService;
17 private final PasswordEncoder passwordEncoder;
18
19 public CustomUsernamePasswordAuthenticationProvider(KorisnikService userService, PasswordEncoder passwordEncoder) {
20 this.userService = userService;
21 this.passwordEncoder = passwordEncoder;
22 }
23
24 @Override
25 public Authentication authenticate(Authentication authentication) throws AuthenticationException {
26 String username = authentication.getName();
27 String password = authentication.getCredentials().toString();
28
29 if (username.isEmpty() || password.isEmpty()) {
30 throw new BadCredentialsException("Empty credentials!");
31 }
32
33 UserDetails userDetails = this.userService.loadUserByEmail(username);
34
35 if (!passwordEncoder.matches(password, userDetails.getPassword())) {
36 throw new BadCredentialsException("Password is incorrect!");
37 }
38 return new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(), userDetails.getAuthorities());
39 }
40
41 @Override
42 public boolean supports(Class<?> aClass) {
43 return aClass.equals(UsernamePasswordAuthenticationToken.class);
44 }
45
46}
Note: See TracBrowser for help on using the repository browser.