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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Spring REST – 訪問被拒絕請求的 JSON 響應 > 原文: [https://howtodoinjava.com/spring-restful/access-denied-json-response/](https://howtodoinjava.com/spring-restful/access-denied-json-response/) 在應預先驗證主體但已在`AbstractPreAuthenticatedProcessingFilter`中收到`null`的情況下,從 REST API 學習以 jSON 格式返回帶有錯誤詳細信息的拒絕訪問的響應,并包含錯誤詳細信息。 ## 1\. 添加自定義`AuthenticationEntryPoint` `ExceptionTranslationFilter`使用此類來啟動身份驗證方案。 如果您正在使用`AbstractPreAuthenticatedProcessingFilter`處理預先認證的認證請求,如[自定義令牌認證示例](https://howtodoinjava.com/spring-restful/custom-token-auth-example/)所示,則默認情況下,所有未認證的用戶都將重定向到默認訪問被拒絕的頁面,即`Http403ForbiddenEntryPoint`。 默認行為顯示拒絕訪問的 HTML 頁面。 我們可以更改定義自己的自定義`AuthenticationEntryPoint`的行為。 此身份驗證入口點已添加到`exceptionHandling()`配置中。 * `ExceptionTranslationFilter`用于捕獲任何 Spring Security 異常,以便可以返回 HTTP 錯誤響應或啟動適當的`AuthenticationEntryPoint`。 * 如果用戶請求安全的 HTTP 資源但未通過身份驗證,則將調用`AuthenticationEntryPoint`。 這樣做的工作是向用戶呈現適當的響應,以便可以開始身份驗證。 在給定的配置中,我們保護所有以`"/api"`開頭的 URL。 `Spring security 配置` ```java httpSecurity. antMatcher("/api/**") .csrf() .disable() .sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.STATELESS) .and() .addFilter(filter) /*.addFilterBefore(new ExceptionTranslationFilter( new Http403ForbiddenEntryPoint()), filter.getClass() )*/ .authorizeRequests() .anyRequest() .authenticated(); //Exception handling configuration httpSecurity .exceptionHandling() .authenticationEntryPoint((request, response, e) -> { response.setContentType("application/json;charset=UTF-8"); response.setStatus(HttpServletResponse.SC_FORBIDDEN); response.getWriter().write(new JSONObject() .put("timestamp", LocalDateTime.now()) .put("message", "Access denied") .toString()); }); ``` > 請不要使用[`AccessDeniedHandler`](https://docs.spring.io/spring-security/site/docs/5.1.4.RELEASE/api/index.html?org/springframework/security/web/access/AccessDeniedHandler.html),因為它僅適用于不需要角色的經過身份驗證的用戶。 ## 2\. 演示 #### 2.1. 沒有身份驗證令牌 `API 請求` ```java HTTP POST http://localhost:8080/SpringRestExample/api/rest/employee-management/employees/1 Headers: Content-Type application/json ``` `API 響應` ```java Response code - 403 Forbidden { "message" : "Access denied", "timestamp" : "2019-05-03T23:59:52.103" } ``` #### 2.2. 使用不正確的身份驗證令牌 `API 請求` ```java HTTP POST http://localhost:8080/SpringRestExample/api/rest/employee-management/employees/1 Headers: Content-Type application/json AUTH_API_KEY 123456 ``` `API 響應` ```java Response code - 403 Forbidden { "message" : "Access denied", "timestamp" : "2019-05-04T00:04:15.457" } ``` #### 2.2. 使用正確的身份驗證令牌 `API 請求` ```java HTTP POST http://localhost:8080/SpringRestExample/api/rest/employee-management/employees/1 Headers: Content-Type application/json AUTH_API_KEY abcd123456 ``` `API 響應` ```java Response code - 200 OK { //API response body } ``` 請問您有關使用自定義`authenticationEntryPoint`將拒絕訪問的響應從 Spring REST API 轉換為 JSON 響應的問題。 [下載源碼](https://howtodoinjava.com/wp-content/downloads/SpringRestExample.zip) 學習愉快!
                  <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>

                              哎呀哎呀视频在线观看