xiangpei
2024-03-28 0f431b52e0936456bd165d9553761bfd8a5a0517
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
package com.mindskip.xzs.service.impl;
 
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import com.mindskip.xzs.domain.User;
import com.mindskip.xzs.service.AuthenticationService;
import com.mindskip.xzs.service.UserService;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
 
/**
 * @author 武汉思维跳跃科技有限公司
 */
@Service
public class AuthenticationServiceImpl implements AuthenticationService {
 
    @Resource(name = "DatabaseRSA")
    private RSA rsa;
    private final UserService userService;
 
    public AuthenticationServiceImpl(UserService userService) {
        this.userService = userService;
    }
 
 
    /**
     * @param username username
     * @param password password
     * @return boolean
     */
    @Override
    public boolean authUser(String username, String password) {
        User user = userService.getUserByUserName(username);
        return authUser(user, username, password);
    }
 
 
    @Override
    public boolean authUser(User user, String username, String password) {
        if (user == null) {
            return false;
        }
        String encodePwd = user.getPassword();
        if (null == encodePwd || encodePwd.length() == 0) {
            return false;
        }
        String pwd = pwdDecode(encodePwd);
        return pwd.equals(password);
    }
 
    @Override
    public String pwdEncode(String password) {
        return rsa.encryptBase64(password, KeyType.PublicKey);
    }
 
    @Override
    public String pwdDecode(String encodePwd) {
        return rsa.decryptStr(encodePwd, KeyType.PrivateKey);
    }
 
 
}