source: source/MovieZilla-master/src/main/java/com/example/demo/config/CustomUsernamePasswordAuthenticationProvider.java

Last change on this file was fc7ec52, checked in by darkopopovski <darkopopovski39@…>, 2 years ago

all files

  • Property mode set to 100644
File size: 2.0 KB
Line 
1package com.example.demo.config;
2
3import com.example.demo.repository.UserRepository;
4import com.example.demo.service.UserService;
5import org.springframework.security.authentication.AuthenticationProvider;
6import org.springframework.security.authentication.BadCredentialsException;
7import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
8import org.springframework.security.core.Authentication;
9import org.springframework.security.core.AuthenticationException;
10import org.springframework.security.core.userdetails.UserDetails;
11
12
13import org.springframework.security.crypto.password.PasswordEncoder;
14import org.springframework.stereotype.Component;
15
16@Component
17public class CustomUsernamePasswordAuthenticationProvider implements AuthenticationProvider {
18
19 private final UserService userService;
20 private final PasswordEncoder passwordEncoder;
21
22 public CustomUsernamePasswordAuthenticationProvider(UserService userService, PasswordEncoder passwordEncoder) {
23 this.userService = userService;
24 this.passwordEncoder = passwordEncoder;
25 }
26
27 @Override
28 public Authentication authenticate(Authentication authentication) throws AuthenticationException {
29 String username = authentication.getName();
30 String password = authentication.getCredentials().toString();
31
32 if ("".equals(username) || "".equals(password)) {
33 throw new BadCredentialsException("Invalid Credentials");
34 }
35 UserDetails userDetails = this.userService.loadUserByUsername(username);
36 if(passwordEncoder.matches(password,userDetails.getPassword()))
37 {
38 throw new BadCredentialsException("Invalid Credentials");
39 }
40 return new UsernamePasswordAuthenticationToken(userDetails,userDetails.getPassword(),
41 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.