【问题标题】:How to secure Vert.x 3.3 Eventbus Bridge with Keycloak如何使用 Keycloak 保护 Vert.x 3.3 Eventbus Bridge
【发布时间】:2016-08-29 11:34:22
【问题描述】:

我目前正在编写一个简单的原型 Vert.x 3.3 应用程序(使用 Jitpack.io 构建的最新 Github 版本),它试图使用 Keycloak 保护 http 端点和事件总线。

我有两个垂直和静态索引页面,带有一个发送消息的按钮。在不安全的情况下一切正常,但我不知道如何使用 Keycloak 保护 SockJS 事件总线桥。保护 http 端点工作正常。

由于 Vert.x 3.3 还没有正式发布,所以我还没有找到很多信息。 http://vertx.io/blog/vertx-3-and-keycloak-tutorial/ 仅涉及保护 http 端点,而关于要求授权的 3.21 文档专门用于使用 BasicAuthHandler,我不确定如何修改它以使用 Keycloak: http://vertx.io/docs/vertx-web/java/#_requiring_authorisation_for_messages

目前我有以下代码:

public class VertxEventBusTest extends AbstractVerticle {

@Override
public void start(Future<Void> startFuture) throws Exception {
System.out.println("Primary Verticle Started");
Router router = Router.router(vertx);
HttpServer server = vertx.createHttpServer().requestHandler(router::accept);

OAuth2Auth oAuth2Auth = OAuth2Auth.createKeycloak(vertx, OAuth2FlowType.AUTH_CODE, getKeycloakJson());
OAuth2AuthHandler oAuth2AuthHandler = OAuth2AuthHandler.create(oAuth2Auth, "http://localhost:8091");
oAuth2AuthHandler.setupCallback(router.get("/callback"));

SockJSHandlerOptions options = new SockJSHandlerOptions().setHeartbeatInterval(2000);
BridgeOptions bridgeOptions = new BridgeOptions();
bridgeOptions.addInboundPermitted(new PermittedOptions().setAddress("click"));
bridgeOptions.addOutboundPermitted(new PermittedOptions().setAddress("click"));
SockJSHandler sockJSHandler = SockJSHandler.create(vertx, options).bridge(bridgeOptions);
router.route("/").handler(oAuth2AuthHandler);
router.route("/eventbus/*").handler(oAuth2AuthHandler);
router.route("/eventbus/*").handler(sockJSHandler);

vertx.eventBus().<JsonObject>consumer("click", msg -> System.out.println("Msg Received on Verticle1: " + msg.body()));

router.route().handler(StaticHandler.create().setWebRoot("webroot"));

server.listen(8091, result -> {
  if (result.succeeded()) {
    startFuture.complete();
  } else {
    startFuture.fail(result.cause());
  }
});
}

private JsonObject getKeycloakJson() {
return new JsonObject("{\n" +
                          "  \"realm\": \"Prototype\",\n" +
                          "  \"realm-public-key\": \"<public key>",\n" +
                          "  \"auth-server-url\": \"http://localhost:8180/auth\",\n" +
                          "  \"ssl-required\": \"external\",\n" +
                          "  \"resource\": \"prototype-eventbus\",\n" +
                          "  \"credentials\": {\n" +
                          "    \"secret\": \"<secret>\"\n" +
                          "  }\n" +
                          "}");
  }
}

我的静态 Html 是:

<head>
  <meta charset="UTF-8">
  <script src='sockjs-0.3.4.js'></script>
  <script src='vertx-eventbus.js'></script>
  <script src='index-js.js'></script>
  <title>VERT.X Test</title>
</head>
<body onload="load()">
<!-- JavaScript includes. -->

<button onclick="zclicker()">Test Click</button>
</body>
</html>

Javascript:

var eb = new EventBus('http://localhost:8091/eventbus');

function load() {
  eb.onopen = function () {
    eb.registerHandler('click', function (data) {})
  };
}

function zclicker() {
  eb.publish('click', {
    'clicked': true
  });
}

垂直运行时没有错误,但是chrome开发者工具通过keycloak登录后显示以下错误:

XMLHttpRequest cannot load http://localhost:8180/auth/realms/Prototype/protocol/openid-connect/auth?re…direct_uri%3D%2Feventbus%2Finfo&state=&client_id=prototype-eventbus&scope=. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8091' is therefore not allowed access.

【问题讨论】:

    标签: javascript java security vert.x keycloak


    【解决方案1】:

    这看起来像是 CORS 问题。

    根据您的错误,您可能只能在响应中添加以下标头:

    Access-Control-Allow-Origin: *
    

    Access-Control-Allow-Origin: http://localhost:8091
    

    取决于您对安全性的关注程度。

    这对于 text/html 的简单 GET 通常就足够了,但是如果您想处理其他内容类型(例如 JSON)或 PUT / 或 DELETE 方法,那么您必须添加更多的标头以允许它们。

    一些资源:

    http://enable-cors.org/server.html

    http://www.html5rocks.com/en/tutorials/cors/

    【讨论】:

    • 我对 CORS 很熟悉,但不确定我将如何去做,因为在这种情况下,指定的地址正在通过 EventBus 的实例化进行查询。这似乎是未正确设置 Keycloak OAuth 处理程序以适用于事件总线的症状,因为在 http 端点的情况下,它正确处理了重定向。
    • 只是为了确保我添加了一个 CORS 处理程序,但它没有解决我的问题:vertx.io/docs/vertx-web/java/#_cors_handling
    • 是的,很公平——值得一试。可能最好等到 3.3 发布。
    • 这是否解决了您的问题?
    猜你喜欢
    • 2018-01-04
    • 1970-01-01
    • 2021-03-08
    • 2021-06-25
    • 2022-01-16
    • 1970-01-01
    • 2020-07-25
    • 1970-01-01
    相关资源
    最近更新 更多