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;
}
复制代码
复制代码
相关推荐
冲上云霄的Jayden7 天前
Paddlex服务化代理处理跨域、替换Response中Json key
json·fastapi·代理·跨域·uvicorn·paddlex·服务化
pitt19977 天前
Chrome 开发环境快速屏蔽 CORS 跨域限制!
chrome·跨域·cors·解决跨越技巧
李少兄11 天前
跨域问题的解决方案
java·前端·跨域
坐吃山猪1 个月前
跨域-告别CORS烦恼
前端·后端·跨域·cors
wv1124061 个月前
请求Geoserver的WTMS服务返回200不返回图片问题-跨域导致
geoserver·跨域·wtms
小Mie不吃饭2 个月前
2025新时代 | 分析并解决企业跨域问题
前端·后端·springboot·api·web·跨域
Lysun0013 个月前
vue2配置跨域后请求的是本机
开发语言·前端·javascript·跨域
随笔写3 个月前
前端处理跨域的几种方式
前端·跨域
GitNohup3 个月前
Spring boot处理跨域问题
java·spring boot·跨域
噢,我明白了3 个月前
同源策略:为什么XMLHttpRequest不能跨域请求资源?
javascript·跨域