source: src/main/java/com/example/baziproekt/config/CustomUsernamePasswordAuthenticationProvider.java@ 0e4d807

Last change on this file since 0e4d807 was 0e4d807, checked in by Ivona <ivonatapshanovska@…>, 10 months ago

Initial commit

  • Property mode set to 100644
File size: 1.8 KB
Line 
1package com.example.baziproekt.config;
2
3import com.example.baziproekt.service.KorisniciService;
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;
12import java.util.Objects;
13
14
15@Component
16public 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}
Note: See TracBrowser for help on using the repository browser.