source: Git/src/main/java/com/wediscussmovies/project/configuration/CustomUsernamePasswordAuthenticationProvider.java@ 5b447b0

main
Last change on this file since 5b447b0 was 5b447b0, checked in by Test <matonikolov77@…>, 2 years ago

Adding models and resources

  • Property mode set to 100644
File size: 1.9 KB
Line 
1package com.wediscussmovies.project.configuration;
2
3import com.wediscussmovies.project.service.UserService;
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.userdetails.UserDetails;
9import org.springframework.security.crypto.password.PasswordEncoder;
10import org.springframework.stereotype.Component;
11
12import javax.naming.AuthenticationException;
13
14@Component
15public class CustomUsernamePasswordAuthenticationProvider implements AuthenticationProvider {
16
17 private final UserService userService;
18 private final PasswordEncoder passwordEncoder;
19
20 public CustomUsernamePasswordAuthenticationProvider(UserService userService, PasswordEncoder passwordEncoder) {
21 this.userService = userService;
22 this.passwordEncoder = passwordEncoder;
23 }
24
25 @Override
26 public Authentication authenticate(Authentication authentication){
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 if (!passwordEncoder.matches(password, 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<?> aClass) {
45 return aClass.equals(UsernamePasswordAuthenticationToken.class);
46 }
47}
48
Note: See TracBrowser for help on using the repository browser.