<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Spring Security 基本身份驗證示例 > 原文: [https://howtodoinjava.com/spring-security/http-basic-authentication-example-using-spring-3/](https://howtodoinjava.com/spring-security/http-basic-authentication-example-using-spring-3/) [基本身份驗證](https://en.wikipedia.org/wiki/Basic_access_authentication)通常用于無狀態客戶端,該客戶端在每個請求上傳遞其憑據。 通常將其與基于表單的身份驗證結合使用,在這種情況下,既可以通過基于瀏覽器的用戶界面也可以通過網絡服務來使用應用程序。 但是,基本身份驗證以純文本形式傳輸密碼,因此,僅應在諸如 HTTPS 之類的加密傳輸層上實際使用該密碼。 由于必須隨每個 HTTP 請求一起發送基本身份驗證標頭,因此 Web 瀏覽器需要在合理的時間內緩存憑據,以避免不斷提示用戶輸入用戶名和密碼。 緩存策略因瀏覽器而異。 ## Spring Security 中的`BasicAuth` 實現基本 http 身份驗證的最簡單解決方案是在 Spring Security配置文件中使用“ `http-basic`”標簽,如下所示: ```java <http> <intercept-url pattern="/**" access="ROLE_USER" /> <http-basic /> </http> ``` 應用程序中的上述設置將強制用戶驗證應用程序中的任何網頁或任何其他資源。 有趣的是,您無需創建任何登錄頁面或會話管理機制。 瀏覽器將在用戶代表您之前顯示一個登錄框。 并且因為每個請求都包含 http 無狀態機制中的身份驗證信息,所以您也不需要維護會話。 ## 基本身份驗證演示 #### 在 Spring Security配置中配置`basic-auth` 在基于[ Spring 登錄表單的安全示例](https://howtodoinjava.com/spring/spring-security/login-form-based-spring-3-security-example/)中創建的員工管理應用程序中,我們手動創建了登錄表單并將其配置為各種 URL 模式。 讓我們對其進行修改以使用 http 基本身份驗證。 我們修改后的`application-security.xml`現在看起來像這樣。 `application-security.xml` ```java < ?xml version="1.0" encoding="UTF-8"?> xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.3.xsd"> <http auto-config="true" use-expressions="true"> <intercept-url pattern="/login" access="permitAll" /> <intercept-url pattern="/logout" access="permitAll" /> <intercept-url pattern="/accessdenied" access="permitAll" /> <intercept-url pattern="/**" access="hasRole('ROLE_USER')" /> <logout logout-success-url="/logout" /> <http-basic /> </http> <authentication-manager alias="authenticationManager"> <authentication-provider> <user-service> <user lokesh" password="password" authorities="ROLE_USER" /> </user-service> </authentication-provider> </authentication-manager> ``` #### 驗證`BasicAuth` 現在,讓我們再次構建應用程序,然后在應用程序服務器中運行它。 **1)在瀏覽器**中點擊網址`http://localhost:8080/Spring3HibernateIntegration` 出現登錄窗口。 請注意,這是瀏覽器生成的登錄框,應用程序僅向瀏覽器提供了相關的標頭。 [![HTTP basic authentication window](https://img.kancloud.cn/a5/ce/a5ce2d14b48a7fae3d8e1ad3c2b247a2_646x177.jpg)](https://howtodoinjava.files.wordpress.com/2013/04/http-basic-authenication.png) HTTP 基本認證窗口 **2)輸入錯誤的用戶名和密碼** 這將使瀏覽器再次顯示清除的登錄框,或者在某些情況下將顯示錯誤頁面。 [![Login error in http basic authentication](https://img.kancloud.cn/c0/83/c083d051da289ccc7814ae2a979bb3c8_852x192.jpg)](https://howtodoinjava.files.wordpress.com/2013/04/http-basic-authetication-error.png) HTTP 基本認證中的登錄錯誤 **3)輸入正確的用戶名和密碼** 正確的用戶名和密碼:“`lokesh`”和“`password`”。 當您輸入上述憑證時,員工管理屏幕將出現在瀏覽器屏幕中。 [![Employee management screen](https://img.kancloud.cn/06/e9/06e9babbc586b97be3f515200825fad0_346x361.jpg)](https://howtodoinjava.files.wordpress.com/2013/04/employee-management-screen.png) 員工管理界面 如果您在運行上述配置時發現任何問題,請告訴我。 學習愉快!
                  <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>

                              哎呀哎呀视频在线观看