Try   HackMD

實現jwt認證機制時發生的蠢事情

* 想要的結果 :

  • 當我在寫登入認證機制,加入spring security6的auth機制,沒有帳號要先註冊有了帳號就可以登入,目前的設計為註冊和登入成功都會產生token後端存到redis,並設置緩存時間,最後讓每個打api的都要戴上bearer token,security才可以授予你權利通過否則就是403,把寫好得程式碼進行測試~~~~

(註冊和登入api在auth下因此可不帶token)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

​​​​1.hello是用來測試的api(我們打開postman測試),結果如預期403

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

​​​​2.進行註冊(password1和2分別為輸入兩次密碼)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

​​​​3.利用創立好的email和password登入(200成功複製token)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

​​​​4.貼上token一樣為403(回去看intellij)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

(要用parseClaimJws而不是圖片中的parseClaimJwt)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

​​​​5.改好以後再重新run

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

​​​​6.戴上剛剛登入生成的token(成功debug)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

* 總結 :

大致上我遇到bug的原因和要檢討的內容:

  • 沒有把redis打開(造成無法存入報錯)
  • 用了錯的函式(解析jwt沒使用jws 加上簽名的function)
  • 要記得設置origin 讓server判斷哪些request可不帶token
  • spring security 的architeture可以再多深入了解!!!
  • 感謝各位觀看 ,如有錯誤地方,麻煩指正我!!**