我正在为SFCC Commerce Cloud (Demandware)商店创建自定义控制器。
因为我需要与第三方系统进行通信,所以我创建了一个自定义REST API控制器,以便能够在SFCC内接收一些数据。
我创建了一个rest控制器,以便通过POST接收信息。如何为我的控制器提供身份验证机制?
OCAPI提供了默认情况下受保护的资源,您可以使用OAuth进行身份验证,但自定义控制器是不受保护的,我想知道如何添加OAuth或其他身份验证机制。
我的控制器:
server.post('Test', server.middleware.https, function (req, res, next) {
//Some logic that should be protected...
}
发布于 2021-07-13 20:32:08
您可以根据请求使用加密的参数,并在控制器上添加要解密的逻辑。
发布于 2021-07-19 13:55:10
您可以使用私钥和证书来验证请求。如果请求总是来自特定的域,则可以添加证书。或者添加公钥和私钥对。
server.post('InboundHookRequest', server.middleware.https, function (req, res, next) {
var payload = null,
requestStored = false;
if (verifySignature(req) === true) {
try {
payload = JSON.parse(req.body);
// Do the logic here
} catch (e) {
Logger.error(e);
}
if (requestStored === true) {
okResponse(res);
return next();
}
}
notOkResponse(res);
return next();
});
然后在中进行验证
function verifySignature(req) {
var signature,
algoSupported,
result;
signature = new Signature();
algoSupported = signature.isDigestAlgorithmSupported("SHA256withRSA"); // or other algo
if (algoSupported === true) {
try {
var certRef = new CertificateRef(WEBHOOK_CONFIG.CERT_NAME);
result = signature.verifySignature("YOURINCOMINGREQHEADER", content, certRef, "SHA256withRSA");;
if (result === true) {
return true;
}
} catch (e) {
Logger.error(e); // Certificate doesn't exist or verification issue
}
}
}
return false;
}
签名:https://documentation.b2c.commercecloud.salesforce.com/DOC2/topic/com.demandware.dochelp/DWAPI/scriptapi/html/api/class_dw_crypto_Signature.html?resultof=%22%53%69%67%6e%61%74%75%72%65%22%20%22%73%69%67%6e%61%74%75%72%22%20证书和私钥:https://documentation.b2c.commercecloud.salesforce.com/DOC2/topic/com.demandware.dochelp/content/b2c_commerce/topics/b2c_security_best_practices/b2c_certificates_and_private_keys.html?resultof=%22%70%72%69%76%61%74%65%22%20%22%70%72%69%76%61%74%22%20%22%6b%65%79%73%22%20%22%6b%65%69%22%20
https://stackoverflow.com/questions/68188827
复制相似问题