source: src/main/java/com/example/moviezone/config/CustomUsernamePasswordAuthenticationProvider.java@ 8a18cf5

Last change on this file since 8a18cf5 was 5444409, checked in by milamihajlovska <mila.mihajlovska01@…>, 21 months ago

update register and registerWorker

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