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

後端處理/解析收到的 token #196

Closed
1 task done
ricksu978 opened this issue Jan 21, 2024 · 3 comments
Closed
1 task done

後端處理/解析收到的 token #196

ricksu978 opened this issue Jan 21, 2024 · 3 comments
Assignees

Comments

@ricksu978
Copy link
Member

ricksu978 commented Jan 21, 2024

Q: 前端 甚麼時候 需要用 websocket 送資料 給後端??
A: 聊天室

AC:

  • 後端 處理/解析 在 handshake 時收到的 token, 將 user 資料放進 principal
@kuoche1712003
Copy link
Contributor

試了一個晚上沒有找到什麼比較好的解法跟 Spring Security 整合再一起
嘗試在連線時把 Authentication 放進 Securitycontextholder
但在接收 event 從 Securitycontextholder 拿不到 Authentication

後來就決定用比較 Hacky 的方式
在 Socket.IO 連線時去解 Token 然後查 UserId
然後把連線加入房間, 房間名稱是他的 UserId
之後想要知道連線的 UserId 就把他所有有加入的房間拿出來
找到有 USER_ prefix 的就是他的 UserId

@kuoche1712003
Copy link
Contributor

另外我還有順便把聊天室的 Event 也接上了

@kuoche1712003
Copy link
Contributor

kuoche1712003 commented Jan 28, 2024

在 Socket.IO 連線時
會從 url parameter 的 token 取出 jwt token
再來會解析 jwt token 取出 subject ex. google-oauth2|102527320242660434908
從資料庫查出他對應的 userId ex. 173921799328193
然後把這個 SocketIOClient join room ex. USER_173921799328193

之後接收到 event
我們可以從 client 獲得其所在的房間 ex. ROOM_1972919392, USER_173921799328193
從中找到 USER_ 開頭的房間
然後就知道這個連線的 userId 為 173921799328193

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

No branches or pull requests

2 participants