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;
|
}
|
}
|
}
|