【问题标题】:Header can not set - SlimFramework标头无法设置 - SlimFramework
【发布时间】:2019-12-30 14:34:52
【问题描述】:

我又遇到了同样的问题。

旧帖here

我有一个 Angular 应用程序和用于 api 连接的 SlimFramework。

本地它工作正常,但是当我发布到我的网站时出现我的 Header 没有设置的错误。 但是 API 测试工具上的信息说它是允许来自 * IP 的。

有人可以帮我吗?

这里是一个有效的令牌:Basic TyOSZcfBwMC6DR9kbAWeMnPmhF4ohZu2n9LccQEyt6uXNt8PTT

谢谢

    $app = new \Slim\App(["settings" => $config]);
$container = $app->getContainer();
$app->options('/{routes:.+}', function ($request, $response, $args) {
    return $response;
});
$app->add(function ($req, $res, $next) {
    $response = $next($req, $res);
    return $response
        ->withHeader('Access-Control-Allow-Origin', '*')
        ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
        ->withHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, DELETE, PUT');
});

$container['logger'] = function($c) {
    $logger = new \Monolog\Logger('my_logger');
    $file_handler = new \Monolog\Handler\StreamHandler("../../logs/app.log");
    $logger->pushHandler($file_handler);
    return $logger;
};
$app->get('/token', function ($request, $response){
    $db = new DbOperation();
    if (!$request->hasHeader('Authorization')) {
        return $response->withJson([
            "success"=> false,
            "message" => "Header not set.",
            "textcode"=> "MSG2"
        ], 401);
    }
    $token = $request->getHeader('Authorization');

    if($db->checkToken($token[0])){
        $user = $db->userInfo($token[0]);
        if($db->checkActivate($user['auth_user'])){
            if($db->checkExpired($user['auth_user'])){
                return $response->withJson([
                    "success"=> false,
                    "message" => "The validity of the login has expired. If you have any questions, please contact the administrator..",
                    "textcode"=> "MSG6"
                ], 401);
            } else {
                return $response->withJson(["success"=> true], 200);
            }
        } else {
            return $response->withJson([
                "success"=> false,
                "message" => "This account has not yet been activated.",
                "textcode"=> "MSG8"
            ], 401);
        }
    } else {
        return $response->withJson([
            "success"=> false,
            "message"=>'Invalid token',
            "textcode"=> "MSG1"
        ], 403);
    }
});

【问题讨论】:

  • 请不要将代码包含为图像。它是文本,因此包含它。没有人会从图像中复制您的代码并尝试使用,甚至可能不会打开图像。
  • 用curl做请求,经常会发现问题。

标签: php angularjs api slim


【解决方案1】:

您的基本身份验证凭据不会解码为任何有意义的内容。 PHP 倾向于默默地忽略它认为格式错误的授权标头。尝试使用Basic dGVzdDp0ZXN0 之类的东西,它会解码为test:test

不过,从 3.5.0 版开始,Slim 已添加了解决方法。升级您的 Slim 安装可能也会有所帮助。

【讨论】:

  • 谢谢。我用Setenvif authorization ^(.+)$ auth_head=$1 getenv("auth_head") 解决了这个问题
猜你喜欢
  • 2017-08-19
  • 2015-05-05
  • 2012-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-06
  • 2017-11-30
  • 2021-01-03
相关资源
最近更新 更多