chen.lin
3 天以前 9140aee230de0ef41de9682a9353fbd372e2bcaa
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
package com.vincent.rsf.openApi.security.service;
 
import com.vincent.rsf.openApi.entity.app.App;
import com.vincent.rsf.openApi.service.AppService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
 
@Slf4j
@Service
public class AppAuthService {
 
    @Resource
    private AppService appService;
    @Resource
    private PasswordEncoder passwordEncoder;
 
    public boolean validateApp(String appId, String appSecret) {
        if (appId == null || appSecret == null) {
            return false;
        }
        try {
            App app = appService.getById(appId);
            if (app == null) {
                return false;
            }
            if (app.getEnable() != null && app.getEnable() != 1) {
                return false;
            }
            String stored = app.getScrect();
            if (stored == null) {
                return false;
            }
            // 存的是 BCrypt 哈希则用 matches,否则兼容明文
            if (stored.startsWith("$2")) {
                return passwordEncoder.matches(appSecret, stored);
            }
            return appSecret.equals(stored);
        } catch (Exception e) {
            log.error("validateApp异常 appId={}", appId, e);
            return false;
        }
    }
 
    public App getAppInfo(String appId) {
        if (appId == null) {
            return null;
        }
        try {
            return appService.getById(appId);
        } catch (Exception e) {
            log.error("getAppInfo失败 appId={}", appId, e);
            return null;
        }
    }
}