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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Spring Boot Security Rest 基本身份驗證示例 > 原文: [https://howtodoinjava.com/spring-boot2/security-rest-basic-auth-example/](https://howtodoinjava.com/spring-boot2/security-rest-basic-auth-example/) 學習使用**基本身份驗證**保護在 [Spring Boot](https://howtodoinjava.com/spring-boot-tutorials/) 應用程序內部創建的其余 api。 受保護的 rest api 在訪問其安全的數據之前會詢問身份驗證詳細信息。 ## 1\. Maven 依賴 為了保護其余的 api,我們必須在項目運行時中包含[與 spring security](https://howtodoinjava.com/spring-security-tutorial/) 相關的 jar 文件。 添加所有必需 jar 的最簡單方法是添加`spring-boot-starter-security`依賴項。 `pom.xml` ```java <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> <relativePath /> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> </dependencies> ``` ## 2\. 配置`WebSecurityConfigurerAdapter` 為了在 Spring Boot Rest API 中啟用身份驗證和授權支持,我們可以配置實用程序類`WebSecurityConfigurerAdapter`。 它有助于要求用戶在訪問我們應用程序中的任何配置的 URL(或所有 URL)之前先進行身份驗證。 `SecurityConfig.java` ```java package com.howtodoinjava.rest.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .csrf().disable() .authorizeRequests().anyRequest().authenticated() .and() .httpBasic(); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("admin") .password("{noop}password") .roles("USER"); } } ``` ## 3\. Spring Boot Security Rest 基本身份驗證演示 出于演示目的,我們可以編寫下面給出的簡單 REST API。 #### 3.1. REST API `EmployeeController.java` ```java @RestController @RequestMapping(path = "/employees") public class EmployeeController { @Autowired private EmployeeDAO employeeDao; @GetMapping(path="/", produces = "application/json") public Employees getEmployees() { return employeeDao.getAllEmployees(); } } ``` #### 3.2. 訪問沒有“授權”標頭的 REST API 訪問 URL 的 Rest API:HTTP GET `http://localhost:8080/employees/` ![Require username and password](https://img.kancloud.cn/82/31/82316078e9762d036fbcd87b8bec41bb_552x379.jpg) 需要用戶名和密碼 #### 3.3. 使用“授權”標頭訪問 rest api 在傳遞帶有已編碼的**基本身份驗證**用戶名和密碼組合的**授權**請求標頭后,我們將能夠訪問其余的 api 響應。 訪問 URL 的 Rest API:HTTP GET `http://localhost:8080/employees/` ![Successful api call](https://img.kancloud.cn/73/ae/73aefbe474698f676322f76ee94799f6_811x629.jpg) 成功的 API 調用 ## 4\. 結論 在這個 SpringBoot 安全性 REST 基本認證示例中,我們學習了**用基本認證**來保護 REST API。 它分兩個步驟完成。 第一步是包括所需的依賴關系,例如 `spring-boot-starter-security`。 第二步是配置`WebSecurityConfigurerAdapter`并添加身份驗證詳細信息。 [下載源碼](https://howtodoinjava.com/wp-content/downloads/springboot-basicauth.zip) 參考文獻: [Spring Security 參考](https://docs.spring.io/spring-security/site/docs/3.2.0.RC2/reference/htmlsingle/#jc) [HTTP 基本認證](https://en.wikipedia.org/wiki/Basic_access_authentication)
                  <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>

                              哎呀哎呀视频在线观看