陶杰
2024-08-22 ee9032d9baf5f33e376d2d2699136e0a7b26bec7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.mzl.flower.config.security.provider;
 
import com.mzl.flower.config.security.token.SupAuthenticationToken;
import com.mzl.flower.constant.Constants;
import com.mzl.flower.service.impl.SupUserDetailsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.Assert;
 
import java.util.ArrayList;
import java.util.List;
 
@Slf4j
public class SupAuthenticationProvider extends SelfAuthenticationProvider {
 
    private UserDetailsService userDetailsService;
 
    private PasswordEncoder passwordEncoder;
 
    @Override
    protected void additionalAuthenticationChecks(UserDetails userDetails, Authentication authentication) throws AuthenticationException {
        if (authentication.getCredentials() == null) {
            log.debug("Authentication failed: no credentials provided");
 
            throw new BadCredentialsException(messages.getMessage(
                    "AbstractUserDetailsAuthenticationProvider.badCredentials",
                    "Bad credentials"));
        }
 
        String presentedPassword = authentication.getCredentials().toString();
 
        if (!passwordEncoder.matches(presentedPassword, userDetails.getPassword())
                && !presentedPassword.equals(userDetails.getPassword())) {
            log.debug("Authentication failed: password does not match stored value");
 
            throw new BadCredentialsException(messages.getMessage(
                    "AbstractUserDetailsAuthenticationProvider.badCredentials",
                    "Bad credentials"));
        }
    }
 
    @Override
    protected Authentication createSuccessAuthentication(Object principal, Authentication authentication, UserDetails user) {
        SupAuthenticationToken result = new SupAuthenticationToken(principal, authentication.getCredentials(), user.getAuthorities());
        result.setDetails(authentication.getDetails());
        return result;
    }
 
    @Override
    protected UserDetails retrieveUser(String username, Authentication authentication) throws AuthenticationException {
        try {
            SupUserDetailsService webUserDetailsService = (SupUserDetailsService) this.getUserDetailsService();
            UserDetails loadedUser = webUserDetailsService.loadUserByUsername(username, prepareUserType());
            if (loadedUser == null) {
                throw new InternalAuthenticationServiceException(
                        "UserDetailsService returned null, which is an interface contract violation");
            }
            return loadedUser;
        } catch (UsernameNotFoundException ex) {
            throw ex;
        } catch (InternalAuthenticationServiceException ex) {
            throw ex;
        } catch (Exception ex) {
            throw new InternalAuthenticationServiceException(ex.getMessage(), ex);
        }
    }
 
    protected List<String> prepareUserType(){
        List<String> userTypes = new ArrayList<>();
        userTypes.add(Constants.USER_TYPE.supplier.name());
        return userTypes;
    }
 
    protected void doAfterPropertiesSet() throws Exception {
        Assert.notNull(this.userDetailsService, "A UserDetailsService must be set");
    }
 
    public void setUserDetailsService(UserDetailsService userDetailsService) {
        this.userDetailsService = userDetailsService;
    }
 
    public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
    }
 
    protected UserDetailsService getUserDetailsService() {
        return userDetailsService;
    }
 
    @Override
    public boolean supports(Class<?> authentication) {
        return SupAuthenticationToken.class.isAssignableFrom(authentication);
    }
}