Oat++ 后端实现跨域

这里记录在官方的例子中,加入跨域。 Oat++ Example-CRUD

在官方的例子中,加入跨域。
Oat++ Example-CRUD

修改AppComponent.hpp文件中的代码,如下:

cpp 复制代码
#include "AppComponent.hpp"

#include "controller/UserController.hpp"
#include "controller/StaticController.hpp"

#include "oatpp-swagger/Controller.hpp"

#include "oatpp/network/Server.hpp"
#include "oatpp/web/server/interceptor/RequestInterceptor.hpp"
#include "oatpp/web/server/interceptor/ResponseInterceptor.hpp"
#include "oatpp/web/protocol/http/outgoing/ResponseFactory.hpp"

#include <iostream>

class CORSInterceptor : public oatpp::web::server::interceptor::RequestInterceptor, public oatpp::web::server::interceptor::ResponseInterceptor {
public:
    std::shared_ptr<oatpp::web::protocol::http::outgoing::Response> intercept(const std::shared_ptr<oatpp::web::protocol::http::incoming::Request>& request) override {
        if (request->getStartingLine().method == "OPTIONS") {
            auto response = oatpp::web::protocol::http::outgoing::ResponseFactory::createResponse(oatpp::web::protocol::http::Status::CODE_200, nullptr);
         /*   response->putHeader("Access-Control-Allow-Origin", "*");*/
          /*  response->putHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
            response->putHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");*/
            //response->putHeader("Access-Control-Max-Age", "3600"); // Cache preflight response
            return response;
        }
        return nullptr;
    }

    std::shared_ptr<oatpp::web::protocol::http::outgoing::Response> intercept(const std::shared_ptr<oatpp::web::protocol::http::incoming::Request>& request, const std::shared_ptr<oatpp::web::protocol::http::outgoing::Response>& response) override {
        response->putHeader("Access-Control-Allow-Origin", "*");
        response->putHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
        response->putHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
        return response;
    }
};

void run() {

    AppComponent components; // Create scope Environment components

    /* Get router component */
    OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router);

    /* Get connection handler component */
    OATPP_COMPONENT(std::shared_ptr<oatpp::network::ConnectionHandler>, connectionHandler);

    /* Create a CORSInterceptor instance */
    auto corsInterceptor = std::make_shared<CORSInterceptor>();

    /* Add CORSInterceptor to the connection handler */
    auto httpConnectionHandler = std::static_pointer_cast<oatpp::web::server::HttpConnectionHandler>(connectionHandler);
    httpConnectionHandler->addRequestInterceptor(corsInterceptor);
    httpConnectionHandler->addResponseInterceptor(corsInterceptor);

    oatpp::web::server::api::Endpoints docEndpoints;

    docEndpoints.append(router->addController(UserController::createShared())->getEndpoints());

    router->addController(oatpp::swagger::Controller::createShared(docEndpoints));
    router->addController(StaticController::createShared());

    /* Get connection provider component */
    OATPP_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, connectionProvider);

    /* create server */
    oatpp::network::Server server(connectionProvider, connectionHandler);

    OATPP_LOGD("Server", "Running on port %s...", connectionProvider->getProperty("port").toString()->c_str());

    server.run();

    /* stop db connection pool */
    OATPP_COMPONENT(std::shared_ptr<oatpp::provider::Provider<oatpp::sqlite::Connection>>, dbConnectionProvider);
    dbConnectionProvider->stop();

}

/**
 *  main
 */
int main(int argc, const char* argv[]) {

    oatpp::base::Environment::init();

    run();

    /* Print how much objects were created during app running, and what have left-probably leaked */
    /* Disable object counting for release builds using '-D OATPP_DISABLE_ENV_OBJECT_COUNTERS' flag for better performance */
    std::cout << "\nEnvironment:\n";
    std::cout << "objectsCount = " << oatpp::base::Environment::getObjectsCount() << "\n";
    std::cout << "objectsCreated = " << oatpp::base::Environment::getObjectsCreated() << "\n\n";

    oatpp::base::Environment::destroy();

    return 0;
}
复制代码
复制代码
相关推荐
汤米粥20 天前
网络请求自定义header导致跨域问题
vue.js·跨域·cors
undefined&&懒洋洋21 天前
解决跨域问题
前端·网络·后端·http·跨域·cors·1024程序员节
想要打 Acm 的小周同学呀1 个月前
跨域的解决方案
前端·跨域·请求
秋窗72 个月前
调用百度翻译API遇到的跨域问题解决方案
nginx·api·跨域
闲来无事垂钓2 个月前
Node.js学习记录(二)
node.js·接口·express·路由·跨域·cors·jsonp
ZhaiMou2 个月前
前端跨域问题详解与解决方案指南
前端·ajax·面试·node.js·html5·js·跨域
努力学习的小宇同学3 个月前
普通项目解决跨域问题和springSecurity解决跨域问题
跨域
玛卡`三少3 个月前
《Web项目跨域请求后端Api设置Cookie失败问题?》
前端·跨域
爬山虎还上班3 个月前
Http自定义Header导致的跨域问题
http·跨域
菊の物语3 个月前
vite解决前端跨域步骤
前端·vite·跨域