aws APiゲートウェイとapiゲートウェイカスタムオーソライザを使用しています。次のように私は、APIゲートウェイのカスタム承認者のために持っているコードは次のとおりです。aws APIゲートウェイをajax経由で呼び出せません
console.log('Loading function');
exports.handler = (event, context, callback) => {
var token = event.authorizationToken;
// Call oauth provider, crack jwt token, etc.
// In this example, the token is treated as the status for simplicity.
switch (token.toLowerCase()) {
case 'allow':
callback(null, generatePolicy('user', 'Allow', event.methodArn));
break;
case 'deny':
callback(null, generatePolicy('user', 'Deny', event.methodArn));
break;
case 'unauthorized':
callback("Unauthorized"); // Return a 401 Unauthorized response
break;
default:
callback("Error: Invalid token");
}
};
var generatePolicy = function(principalId, effect, resource) {
var authResponse = {};
authResponse.principalId = principalId;
if (effect && resource) {
var policyDocument = {};
policyDocument.Version = '2012-10-17'; // default version
policyDocument.Statement = [];
var statementOne = {};
statementOne.Action = 'execute-api:Invoke'; // default action
statementOne.Effect = effect;
statementOne.Resource = resource;
policyDocument.Statement[0] = statementOne;
authResponse.policyDocument = policyDocument;
}
// Can optionally return a context object of your choosing.
authResponse.context = {};
authResponse.context.stringKey = "stringval";
authResponse.context.numberKey = 123;
authResponse.context.booleanKey = true;
return authResponse;
あなたがそれを見ることができるよう、AWSのウェブサイトで提供単なるモックアップ例です。 次に、この認可者を使用してAPIゲートウェイでgetメソッドを設定しました。また、メソッドの実行では、オーソライザによって使用される、authorizationTokenというカスタムヘドアを追加しました。
私は郵便配達すべてが良いです使用します。
私は次のようにAJAXを経由して、それを呼び出すようにしようとすると、しかし、私は次のエラーを取得する:ここで
XMLHttpRequest cannot load https://590vv3bkda.execute-api.us-east-1.amazonaws.com/hamedstg/tjresource/story. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 401.
は私のアヤックスであります電話:
$
.ajax(
'https://590vv3bkda.execute-api.us-east-1.amazonaws.com/xxxxxxx',
{
method : 'GET',
headers : {
'authorizationToken' : 'allow'
},
beforeSend : function(
xhr) {
xhr
.setRequestHeader(
'authorizationToken',
'allow');
}
})
.then(function(data) {
console.log(data);
});
また、私はena awsでapi上の出血CORS。
誰でも手助けできますか?
テストサイトにアクセスする際に、 'localhost:8080'の代わりに' lvh.me:8080'または 'www.127.0.0.1.xip.ip:8080'を置き換えてください。 –
@ Michael-sqlbot awi Apiゲートウェイでこの設定を行うにはどうすればよいですか? –