source: src/main/java/project/fmo/app/projcetfmo/config/CustomUsernamePasswordAuthenticationProvider.java@ d14176d

main
Last change on this file since d14176d was d14176d, checked in by HristijanMitic00 <hristijan.mitic.01@…>, 12 months ago

First commit

  • Property mode set to 100644
File size: 1.9 KB
Line 
1package project.fmo.app.projcetfmo.config;
2
3import org.springframework.security.authentication.AuthenticationProvider;
4import org.springframework.security.authentication.BadCredentialsException;
5import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
6import org.springframework.security.core.Authentication;
7import org.springframework.security.core.AuthenticationException;
8import org.springframework.security.core.userdetails.UserDetails;
9import org.springframework.security.crypto.password.PasswordEncoder;
10import org.springframework.stereotype.Component;
11import project.fmo.app.projcetfmo.Service.KorisnikService;
12
13@Component
14public 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 }
47}
Note: See TracBrowser for help on using the repository browser.