<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 Boot 2 – OAuth2 Auth 和資源服務器 > 原文: [https://howtodoinjava.com/spring-boot2/oauth2-auth-server/](https://howtodoinjava.com/spring-boot2/oauth2-auth-server/) 在本 **Spring Security oauth2** 教程中,學習構建**授權服務器**來對您的身份進行身份驗證,以提供`access_token`,您可以使用該服務器向資源服務器**請求數據**。 ## 1\. 概述 OAuth 2 是一種授權方法,用于通過 HTTP 協議提供對受保護資源的訪問。 首先,oauth2 使第三方應用程序可以獲得對 HTTP 服務的有限訪問權限: * 通過協調資源所有者和 HTTP 服務之間的批準交互來代表資源所有者 * 或允許第三方應用程序代表自己獲取訪問權限。 #### 1.1. 角色 OAuth 定義了四個角色: * **資源所有者** – 應用程序的用戶。 * **客戶端** – 需要訪問資源服務器上用戶數據的應用程序(用戶正在使用)。 * **資源服務器** – 存儲用戶數據和 http 服務,這些服務可以將用戶數據返回到經過身份驗證的客戶端。 * **授權服務器** – 負責驗證用戶的身份并提供授權令牌。 資源服務器接受此令牌并驗證您的身份。 ![Oauth2 Flow](https://img.kancloud.cn/e0/45/e045b97ab47bac2b805a180769befc1e_551x319.jpg) Oauth2 流程 #### 1.2. 訪問令牌與刷新令牌 **訪問令牌**是表示頒發給客戶端的授權的字符串。 令牌代表資源所有者授予并由資源服務器和授權服務器強制執行的特定訪問范圍和持續時間。 **刷新令牌**由授權服務器發布(與訪問令牌一起)給客戶端,用于在當前訪問令牌變為無效或過期時獲取新的訪問令牌,或用于獲取具有相同權限的其他訪問令牌或更窄的范圍(訪問令牌的生存期和權限可能比資源所有者授權的要短)。 授權服務器可以決定是否發出刷新令牌。 * 訪問令牌的責任是在數據過期之前訪問數據。 * 當現有訪問令牌過期時,刷新令牌的職責是請求新的訪問令牌。 ## 2\. Oauth2 – 授權服務器 要使用[ spring security oauth2 模塊](https://spring.io/projects/spring-security-oauth)創建授權服務器,我們需要使用注解`@EnableAuthorizationServer`并擴展類`AuthorizationServerConfigurerAdapter`。 `OAuth2AuthorizationServer.java` ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer; import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter; import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer; import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer; @Configuration @EnableAuthorizationServer public class OAuth2AuthorizationServer extends AuthorizationServerConfigurerAdapter { @Autowired private BCryptPasswordEncoder passwordEncoder; @Override public void configure(AuthorizationServerSecurityConfigurer security) throws Exception { security .tokenKeyAccess("permitAll()") .checkTokenAccess("isAuthenticated()") .allowFormAuthenticationForClients(); } @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients .inMemory() .withClient("clientapp").secret(passwordEncoder.encode("123456")) .authorizedGrantTypes("password", "authorization_code", "refresh_token") .authorities("READ_ONLY_CLIENT") .scopes("read_profile_info") .resourceIds("oauth2-resource") .redirectUris("http://localhost:8081/login") .accessTokenValiditySeconds(120) .refreshTokenValiditySeconds(240000); } } ``` * Spring Security oauth 公開了兩個用于檢查令牌的端點(`/oauth/check_token`和`/oauth/token_key`),這些令牌默認情況下在`denyAll()`之后受到保護。`tokenKeyAccess()`和`checkTokenAccess()`方法打開這些端點供使用。 * `ClientDetailsServiceConfigurer`用于定義客戶端詳細信息服務的內存中或 JDBC 實現。我們使用了內存實現。它具有以下重要屬性: `clientId` – (必填)客戶端 ID。 `password` – (對于受信任的客戶端是必需的)客戶端密碼(如果有)。 `scope` – 客戶端受限的范圍。 如果范圍未定義或為空(默認值),則客戶端不受范圍的限制。 `authorizedGrantTypes` – 授權給客戶端使用的授權類型。 默認值為空。 `previlege` – 授予客戶端的權限(常規的 Spring Security 權限)。 `redirectUris` – 將用戶代理重定向到客戶端的重定向端點。 它必須是一個絕對 URL。 ## 3\. Oauth2 – 資源服務器 要創建資源服務器組件,請使用`@EnableResourceServer`注解并擴展`ResourceServerConfigurerAdapter`類。 `OAuth2ResourceServer.java` ```java import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter; @Configuration @EnableResourceServer public class OAuth2ResourceServer extends ResourceServerConfigurerAdapter { @Override public void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/api/**").authenticated() .antMatchers("/").permitAll(); } } ``` 上面的 config 在所有從`/api`開始的端點上啟用保護。 其他所有端點均可自由訪問。 資源服務器還提供了一種對用戶本身進行身份驗證的機制。 在大多數情況下,它將是基于表單的登錄名。 `SecurityConfig.java` ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; 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; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; @Configuration @Order(1) public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .antMatcher("/**") .authorizeRequests() .antMatchers("/oauth/authorize**", "/login**", "/error**") .permitAll() .and() .authorizeRequests() .anyRequest().authenticated() .and() .formLogin().permitAll(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser("humptydumpty").password(passwordEncoder().encode("123456")).roles("USER"); } @Bean public BCryptPasswordEncoder passwordEncoder(){ return new BCryptPasswordEncoder(); } } ``` 在[`WebSecurityConfigurerAdapter`](https://howtodoinjava.com/spring5/security5/security-java-config-enablewebsecurity-example/)類上方,設置基于表單的登錄頁面,并使用`permitAll()`打開授權 URL。 ## 4\. Oauth2 保護的 REST 資源 出于演示目的,我僅創建了一個 API,該 API 會返回登錄用戶的姓名和電子郵件。 `RestResource.java` ```java import org.springframework.http.ResponseEntity; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class RestResource { @RequestMapping("/api/users/me") public ResponseEntity<UserProfile> profile() { //Build some dummy data to return for testing User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); String email = user.getUsername() + "@howtodoinjava.com"; UserProfile profile = new UserProfile(); profile.setName(user.getUsername()); profile.setEmail(email); return ResponseEntity.ok(profile); } } ``` `UserProfile.java` ```java public class UserProfile { private String name; private String email; //Setters and getters @Override public String toString() { return "UserProfile [name=" + name + ", email=" + email + "]"; } } ``` ## 5\. 演示 我們有一個 API `http://localhost:8080/api/users/me`,可以通過直接在登錄名中輸入用戶名/密碼來訪問它,但是第三方應用程序不能像在瀏覽器中那樣訪問 API。 他們需要 oauth2 令牌。 #### 5.1. 從用戶獲取授權授權代碼 如上面的序列圖所示,第一步是從 URL:`http://localhost:8080/oauth/authorize?client_id=clientapp&response_type=code&scope=read_profile_info`獲得資源所有者的授權授予 它將帶來一個登錄頁面。 提供用戶名和密碼。 對于此演示,請使用`humptydumpty`和`123456`。 ![Login page](https://img.kancloud.cn/f4/1c/f41c399bea144cd4e44fa80546f1e486_435x220.jpg) 登錄頁面 登錄后,您將被重定向到“授予訪問權限”頁面,您可以在其中選擇授予對第三方應用程序的訪問權限。 ![Get authorization grant](https://img.kancloud.cn/97/b0/97b0791d11209e30bf8991c94eff9291_491x250.jpg) 獲取權限授權 它將重定向到類似`http://localhost:8081/login?code=EAR76A`的 URL。 這里`'EAR76A'`是第三方應用程序的授權代碼。 #### 5.2. 從授權服務器獲取訪問令牌 現在,應用程序將使用授權授予來獲取訪問令牌。 在這里,我們需要提出以下要求。 使用此處第一步中獲得的代碼。 `Access token request from postman` ```java http://localhost:8080/oauth/token Headers: Content-Type: application/x-www-form-urlencoded authorization: Basic Y2xpZW50YXBwOjEyMzQ1Ng== Form data - application/x-www-form-urlencoded: grant_type=authorization_code code=EAR76A redirect_uri=http://localhost:8081/login ``` 它將在單獨的窗口中詢問客戶端應用程序憑據。 ![Client auth](https://img.kancloud.cn/42/c7/42c73630a13863edefd96ab8415c60aa_464x371.jpg) 客戶端授權 或從 cURL 發出類似請求。 `Access token request from cURL` ```java curl -X POST --user clientapp:123456 http://localhost:8081/oauth/token -H "content-type: application/x-www-form-urlencoded" -d "code=FfrzTj&grant_type=authorization_code&redirect_uri=http%3A%2F%2Flocalhost%3A8082%2Flogin&scope=read_user_info" ``` `Access token response` ```java { "access_token": "59ddb16b-6943-42f5-8e2f-3acb23f8e3c1", "token_type": "bearer", "refresh_token": "cea0aa8f-f732-44fc-8ba3-5e868d94af64", "expires_in": 4815, "scope": "read_profile_info" } ``` > 閱讀更多:[如何在 Windows 中執行 cURL 命令](https://howtodoinjava.com/for-fun-only/curl-in-windows/) #### 5.3. 從資源服務器訪問用戶數據 獲得訪問令牌后,我們可以轉到資源服務器以獲取受保護的用戶數據。 達到以下要求: `Get resource request` ```java curl -X GET http://localhost:8080/api/users/me -H "authorization: Bearer 59ddb16b-6943-42f5-8e2f-3acb23f8e3c1" ``` 它將返回響應。 `Get resource response` ```java {"name":"humptydumpty","email":"humptydumpty@howtodoinjava.com"} ``` ## 6\. Spring Security oauth2 應用程序的 Maven 依賴項 此 **spring security 5 oauth2 示例**使用的 pom 文件是: `pom.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>com.howtodoinjava</groupId> <artifactId>spring-oauth2-resource-server-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-oauth2-resource-server-demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.security.oauth.boot</groupId> <artifactId>spring-security-oauth2-autoconfigure</artifactId> <version>2.1.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> ``` 將您的問題留在我的評論中。 學習愉快! 參考文獻: [Oauth2 協議](https://tools.ietf.org/html/rfc6749) [OAuth2 自動配置](https://docs.spring.io/spring-security-oauth2-boot/docs/current/reference/htmlsingle/) [下載源碼](https://github.com/lokeshgupta1981/SpringExamples/tree/master/oauth2)
                  <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>

                              哎呀哎呀视频在线观看