source: src/main/java/com/example/fooddeliverysystem/configuration/UsernameAndPasswordAuthProvider.java

Last change on this file was 8d11f8c, checked in by jovanmanchev <jovanmanchev3003@…>, 20 months ago

code added, trial 2

  • Property mode set to 100644
File size: 1.8 KB
Line 
1package com.example.fooddeliverysystem.configuration;
2
3import com.example.fooddeliverysystem.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.AuthenticationException;
9import org.springframework.security.core.userdetails.UserDetails;
10import org.springframework.security.crypto.password.PasswordEncoder;
11import org.springframework.stereotype.Component;
12
13@Component
14public class UsernameAndPasswordAuthProvider implements AuthenticationProvider {
15
16
17 private final UserService userService;
18 private final PasswordEncoder passwordEncoder;
19
20 public UsernameAndPasswordAuthProvider(UserService userService, PasswordEncoder passwordEncoder) {
21 this.userService = userService;
22 this.passwordEncoder = passwordEncoder;
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 UserDetails userDetails = this.userService.loadUserByUsername(username);
31 if (!passwordEncoder.matches(password, userDetails.getPassword())) {
32 throw new BadCredentialsException("Password is incorrect!");
33 }
34
35 return new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(), userDetails.getAuthorities());
36 }
37
38 @Override
39 public boolean supports(Class<?> authentication) {
40 return authentication.equals(UsernamePasswordAuthenticationToken.class);
41 }
42}
Note: See TracBrowser for help on using the repository browser.