Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customizable WebSocketCreator #23

Closed
paomian opened this issue Jul 18, 2015 · 11 comments
Closed

Customizable WebSocketCreator #23

paomian opened this issue Jul 18, 2015 · 11 comments

Comments

@paomian
Copy link
Contributor

paomian commented Jul 18, 2015

createWebSocket 可以加入鉴权功能,可以对 cookie 或者 Sec-WebSocket-Protocol 请求头进行鉴权,提供一个可以根据不同鉴权返回不同 handler 的功能

@sunng87
Copy link
Owner

sunng87 commented Jul 18, 2015

这类业务看看能不能做到 app 里而不是 adapter 里,如果有 app 里做不到的可以提炼到上游来

@paomian
Copy link
Contributor Author

paomian commented Jul 18, 2015

鉴权能做到 app 里,但是必须要 adapter 里提供一个方法才行,因为 sokcet 的握手连接是在 WebSocketCreator 的 createWebSocket 这一步的,现在这个项目的逻辑是这一步直接就返回了一个 WebSocketHandler ,就是只要发起了 WebSocket 请求就是默认会连接的,而你在 on-conn 里做鉴权已经晚了,因为连接已经完成,可以在握手的时候就进行鉴权进行不必要的资源浪费

一个基本的鉴权的例子,还可以从 request 里拿更多的东西进行鉴权
我们默认就返回了 handler
createWebSocket 函数

@sunng87
Copy link
Owner

sunng87 commented Jul 18, 2015

嗯,准确说是你需要允许用户传入一个自定义的 websocket creator,这个可以在 adapter 层面做

@paomian
Copy link
Contributor Author

paomian commented Jul 18, 2015

好象有个问题,不知道是什么情况,现在正在跟进,如果在 creator 中直接调用 resp 的.sendForbidden 不会导致 handle 的遍历结束,就是 base request 不会调用 setHandled(true) 很奇怪还没搞清楚是怎么弄的。

@paomian
Copy link
Contributor Author

paomian commented Jul 18, 2015

@paomian
Copy link
Contributor Author

paomian commented Jul 18, 2015

我觉得这是 jetty 的一个 bug
我觉得也应该在这里加入

baseRequest.setHandled(true);

@sunng87
Copy link
Owner

sunng87 commented Jul 18, 2015

@paomian 这里如果没有调用 setHandled(true) 的话,是响应写不回去还是线程不会释放?

@sunng87 sunng87 changed the title 准备加入鉴权功能 Customizable WebSocketCreator Jul 18, 2015
@paomian
Copy link
Contributor Author

paomian commented Jul 18, 2015

不知道会不会释放,但是会继续向下调用在 HandlerList 中的 handler 的 handle 方法,直到有一个有反馈或者走完,因此会进入 ring 的 handle

@sunng87
Copy link
Owner

sunng87 commented Jul 18, 2015

那应该在 ring 的 handler 里首先判断一下 response 是否 commited,如果 commited 就跳过

@paomian
Copy link
Contributor Author

paomian commented Jul 18, 2015

我重写了 WebSocketHandler 的 handle 的方法,测试了不会报错了。

(defn- proxy-ws-handler
  "Returns a Jetty websocket handler"
  [ws-fns {:as options
           :keys [ws-max-idle-time]
           :or {ws-max-idle-time 500000}}]
  (proxy [WebSocketHandler] []
    (configure [^WebSocketServletFactory factory]
      (-> (.getPolicy factory)
          (.setIdleTimeout ws-max-idle-time))
      (.setCreator factory (reify-ws-creator ws-fns)))
    (handle [^String target, ^Request request req res]
      (let [wsf (proxy-super getWebSocketFactory)]
        (if (.isUpgradeRequest wsf req res)
          (if (.acceptWebSocket wsf req res)
            (.setHandled request true)
            (when (.isCommitted res)
              (.setHandled request true)))
          (proxy-super handle target request req res))))))

sunng87 added a commit that referenced this issue Jul 25, 2015
picked up patch from @paomian [#23] to ensure rejected websocket request won't
propagate to normal servlet handlers.
@sunng87
Copy link
Owner

sunng87 commented Jul 25, 2015

implemented in 0.9

@sunng87 sunng87 closed this as completed Jul 25, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants