Ignore:
Timestamp:
07/07/23 12:14:58 (16 months ago)
Author:
HristijanMitic00 <hristijan.mitic.01@…>
Branches:
main
Parents:
1dd9226
Message:

First commit

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/project/fmo/app/projcetfmo/config/CustomUsernamePasswordAuthenticationProvider.java

    r1dd9226 rd14176d  
    1 package project.fmo.app.projcetfmo.config;public class CustomUsernamePasswordAuthenticationProvider {
     1package project.fmo.app.projcetfmo.config;
     2
     3import org.springframework.security.authentication.AuthenticationProvider;
     4import org.springframework.security.authentication.BadCredentialsException;
     5import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
     6import org.springframework.security.core.Authentication;
     7import org.springframework.security.core.AuthenticationException;
     8import org.springframework.security.core.userdetails.UserDetails;
     9import org.springframework.security.crypto.password.PasswordEncoder;
     10import org.springframework.stereotype.Component;
     11import project.fmo.app.projcetfmo.Service.KorisnikService;
     12
     13@Component
     14public class CustomUsernamePasswordAuthenticationProvider implements AuthenticationProvider {
     15
     16    private final KorisnikService korisnikService;
     17    private final PasswordEncoder passwordEncoder;
     18
     19    public CustomUsernamePasswordAuthenticationProvider(KorisnikService korisnikService, PasswordEncoder passwordEncoder) {
     20        this.korisnikService = korisnikService;
     21        this.passwordEncoder = passwordEncoder;
     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.korisnikService.loadUserByUsername(username);
     35
     36        if (!password.equals(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<?> authentication) {
     45        return authentication.equals(UsernamePasswordAuthenticationToken.class);
     46    }
    247}
Note: See TracChangeset for help on using the changeset viewer.