我有以下問題: 我要訪問內部SecurityContextHolder.getContext().getAuthentication
Spring Security沒有保存認證
的代碼我用來保存用戶的認證的認證對象是以下; SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(username, password));
我如何使用它如下;
我用localhost:8080/api/login/<username>/<password>
發送一個請求,然後我想檢索郵件列表使用localhost:8080/api/messages
當我打印出來的認證對象,它總是返回一個匿名用戶,雖然登錄的用戶我在我的登錄方法中設置了身份驗證對象。
你設置和獲取在同一認證線? SecurityContextHolder「將給定的SecurityContext與當前執行線程關聯起來」 – Ayman
不,我不是,但是Spring Security不應該保存誰登錄?否則,每當有人提出請求時,我都需要對用戶進行身份驗證。 – Jelle
正確,但Spring Security應該自動執行此操作,而不必手動註冊AuthenticationToken。我建議你檢查一下在線的一些教程。 – Ayman