<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、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Jersey REST 客戶端認證示例 > 原文: [https://howtodoinjava.com/jersey/jersey-rest-client-authentication/](https://howtodoinjava.com/jersey/jersey-rest-client-authentication/) 了解如何使用[`HttpAuthenticationFeature`](https://github.com/jersey/jersey/blob/master/core-client/src/main/java/org/glassfish/jersey/client/authentication/HttpAuthenticationFeature.java)構建**Jersey REST 客戶端**,該客戶端可用于訪問認證/授權安全性后面的 REST API。 例如,我們將為在 [Jersey Secured REST API 教程](//howtodoinjava.com/jersey/jersey-rest-security/)中保護的服務創建 **jersey 客戶端**; 并且我將擴展為 [**Jersey RESTful 客戶端示例**](//howtodoinjava.com/jersey/jersey-restful-client-examples/)創建的源代碼。 ```java Table of Contents 1\. HttpAuthenticationFeature 2\. How to secure REST APIs 3\. Jersey REST Client Code ``` ## 1\. Jersey 客戶端 – `HttpAuthenticationFeature` `HttpAuthenticationFeature`類提供 HttpBasic 和 Digest 客戶端認證功能。 該功能以 4 種模式之一工作,即`BASIC`,`BASIC NON-PREEMPTIVE`,`DIGEST`和`UNIVERSAL`。 讓我們快速了解它們。 1. **`BASIC`** – 一種搶占式認證方式,即信息始終與每個 HTTP 請求一起發送。 此模式必須與 SSL/TLS 結合使用,因為密碼僅以 BASE64 編碼發送。 2. **`BASIC NON-PREEMPTIVE`** – 一種非優先的認證方式,即僅當服務器拒絕帶有 401 狀態碼的請求后再添加認證信息,才添加認證信息。 3. **`DIGEST`** – HTTP 摘要認證。 不需要使用 SSL/TLS。 4. **`UNIVERSAL`** – 非搶占模式下基本認證和摘要認證的組合,即在 401 響應的情況下,將根據`WWW-Authenticate` HTTP 標頭中定義的請求認證使用適當的認證。 要使用`HttpAuthenticationFeature`,請構建一個實例并向客戶端注冊。 #### 1.1 基本認證方式 ```java HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("username", "password"); final Client client = ClientBuilder.newClient(); client.register(feature); ``` #### 1.2 基本認證 - 非強制模式 ```java HttpAuthenticationFeature feature = HttpAuthenticationFeature.basicBuilder() .nonPreemptive() .credentials("username", "password") .build(); final Client client = ClientBuilder.newClient(); client.register(feature); ``` #### 1.3 通用模式 ```java //HttpAuthenticationFeature feature = HttpAuthenticationFeature.universal("username", "password"); //Universal builder having different credentials for different schemes HttpAuthenticationFeature feature = HttpAuthenticationFeature.universalBuilder() .credentialsForBasic("username1", "password1") .credentials("username2", "password2").build(); final Client client = ClientBuilder.newClient(); client.register(feature); ``` ## 2\. 如何保護 REST API 對于啟用認證的 REST api,請使用與角色相關的注解,例如`@RolesAllowed`。 例如,這是安全的 REST API 的代碼。 ```java @Path("/employees") public class JerseyService { @RolesAllowed("ADMIN") @GET @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public Employees getAllEmployees() { Employees list = new Employees(); list.setEmployeeList(new ArrayList<Employee>()); list.getEmployeeList().add(new Employee(1, "Lokesh Gupta")); list.getEmployeeList().add(new Employee(2, "Alex Kolenchiskey")); list.getEmployeeList().add(new Employee(3, "David Kameron")); return list; } } ``` > 閱讀更多:[Jersey 安全 REST API 教程](//howtodoinjava.com/jersey/jersey-rest-security/) ## 3\. Jersey REST 客戶端代碼 以下是 **Jersey REST 客戶端基本認證**示例,該示例接受用于認證的用戶名和密碼詳細信息。 ```java public static void main(String[] args) throws IOException { httpGETCollectionExample(); } private static void httpGETCollectionExample() { ClientConfig clientConfig = new ClientConfig(); HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("howtodoinjava", "password"); clientConfig.register( feature) ; clientConfig.register(JacksonFeature.class); Client client = ClientBuilder.newClient( clientConfig ); WebTarget webTarget = client.target("http://localhost:8080/JerseyDemos/rest").path("employees"); Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); Response response = invocationBuilder.get(); System.out.println(response.getStatus()); System.out.println(response.getStatusInfo()); if(response.getStatus() == 200) { Employees employees = response.readEntity(Employees.class); List<Employee> listOfEmployees = employees.getEmployeeList(); System.out.println(Arrays.toString( listOfEmployees.toArray(new Employee[listOfEmployees.size()]) )); } } ``` #### 3.1 正確的用戶名/密碼的輸出 ```java 200 OK [Employee [id=1, name=Lokesh Gupta], Employee [id=2, name=Alex Kolenchiskey], Employee [id=3, name=David Kameron]] ``` #### 3.2 不正確的用戶名/密碼的輸出 ```java 401 Unauthorized ``` 將您的問題放在評論部分。 學習愉快!
                  <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>

                              哎呀哎呀视频在线观看