<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 如何在 Spring 中處理登錄身份驗證 > 原文: [https://javatutorial.net/how-to-handle-login-authentication-in-spring](https://javatutorial.net/how-to-handle-login-authentication-in-spring) 在本文中,您將學習如何使用 Spring Security 實現登錄身份驗證功能。 ![spring-featured-image](https://img.kancloud.cn/64/23/64231db4bf6e880066d0d4c2e31ac166_780x330.jpg) 登錄頁面表示一種表單,要求用戶名和密碼之類的詳細信息。 可以在 Angular 中完成相同的登錄頁面,并且身份驗證過程本身將由 Spring Security 執行。 從服務器中獲取一個令牌,然后將其發送回客戶端。 此外,服務器希望**相同的**令牌用于依維簽名請求**及以后**。 ## 工作流程 如上所述,當用戶提交登錄表單時,他必須輸入用戶名和密碼(最常見)。 當他**提交**表單時,會有一個 API 調用,該 API 調用來自 Spring Web 。 該路徑可能類似于`/login`,并通過 HTTP POST 方法調用。 您可以在前端(例如 Angular)中編寫身份驗證邏輯。 但是在本文中,我們將在服務器端(也稱為 Spring)上編寫它。 我們可以通過兩種方法來實現我們的目標。 一種是通過編寫控制器類,另一種是通過擴展[`UsernamePasswordAuthenticationFilter`](https://docs.spring.io/spring-security/site/docs/current/api/org/springframework/security/web/authentication/UsernamePasswordAuthenticationFilter.html)。我們將使用第一種方法 - 通過創建`login`方法來驗證用戶身份并返回`true`或`false`。 `ControllerClass.java` ```java // the POST method @PostMapping(value="/login") public boolean login(@RequsetBody Employee reqEmployee, HttpServletRequest request) { Authentication auth = null; UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(reqEmployee.getUsername(), reqEmployee.getPassword()); try { auth = authenticationProvider.authenticate(token); SecurityContextHolder.getContext().setAuthentication(auth); Employee employee = (Employee) authentication.getPrincipal(); employee.setPassword(null); return true; } catch (BadCredentialsException exception) { // you can also log the exception message, for example using Logger return false; } } ``` 這是一種簡單的 POST 方法,根據用戶是否已成功通過身份驗證,它會返回`true`或`false`。 **細分** 首先,我們創建**身份驗證**以充當**身份驗證**實例,首先我們將其設為`null`。 然后,我們為用戶輸入的用戶名和密碼生成一個身份驗證令牌。 然后我們有一個`try{} catch(){}`塊,其中`try`根據令牌對用戶進行身份驗證,如果一切成功,則返回`true`。 另一方面,如果未通過身份驗證并返回`false`,則將運行`catch`。 同樣,此方法應在`Controller`類中。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看