Java API 開發中的一個常見需求就是實作使用者的身份驗證和授權功能。為了提供更安全可靠的API服務,授權功能變得特別重要。 Spring Security OAuth 是一個優秀的開源框架,可以幫助我們在 Java API 中實現授權功能。本文將介紹如何使用 Spring Security OAuth 進行安全授權。
- 什麼是 Spring Security OAuth?
Spring Security OAuth 是 Spring Security 框架的一個擴展,它可以幫助我們實現 OAuth 認證和授權功能。
OAuth 是一個開放標準,用於授權第三方應用程式存取資源。它可以幫助我們實現業務邏輯解耦和安全性強的應用程式。 OAuth 授權流程通常包含以下角色:
- 使用者:資源的擁有者;
- 用戶端:申請存取使用者資源的應用程式;
- 授權伺服器:處理使用者授權的伺服器;
- 資源伺服器:儲存使用者資源的伺服器;
- #授權流程
Spring Security OAuth 實作了OAuth授權流程中的四個端點:
- /oauth/authorize:授權伺服器的授權端點;
- /oauth/token:授權伺服器的令牌端點;
- /oauth/confirm_access:用戶端確認授權的端點;
- /oauth/error:授權伺服器錯誤訊息的端點;
Spring Security OAuth 實作了OAuth 2.0 的四大授權模式:
- 授權碼模式:使用場景為應用程式啟動時需要使用者授權;
- 密碼模式:使用場景為用戶端自主管理使用者憑證;
- 簡化模式:使用場景為客戶端跑在瀏覽器中,不需要客戶端保護使用者憑證;
- 客戶端模式:使用場景為客戶端不需要使用者授權,請求的存取權令牌只代表了客戶端本身;
- 新增Spring Security OAuth 依賴
在專案中加入Spring Security OAuth 依賴。在 pom.xml 中進行以下配置:
<dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth2</artifactId> <version>2.3.4.RELEASE</version> </dependency>
- 配置授權伺服器
我們需要定義授權伺服器以便進行授權。在 Spring Security OAuth 中,可以透過啟用 OAuth2 認證伺服器和實作 AuthorizationServerConfigurer 介面來定義授權伺服器。
@Configuration @EnableAuthorizationServer public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { @Autowired TokenStore tokenStore; @Autowired AuthenticationManager authenticationManager; @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() .withClient("client") .secret("{noop}secret") .authorizedGrantTypes("client_credentials", "password") .scopes("read", "write") .accessTokenValiditySeconds(3600) .refreshTokenValiditySeconds(7200); } @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.tokenStore(tokenStore) .authenticationManager(authenticationManager); } }
在上面的程式碼中,我們定義了一個基於記憶體的客戶端細節服務,並配置了授權類型為 client_credentials 和 password,也指定了存取令牌的有效期和刷新令牌的有效期。另外,我們也定義了 endpoints 以及他們所需的 tokenStore 和 authenticationManager。
- 設定資源伺服器
要使用 Spring Security OAuth 安全授權,我們還需要設定資源伺服器。在 Spring Security OAuth 中,我們可以透過實作 ResourceServerConfigurer 介面來定義資源伺服器。
@Configuration @EnableResourceServer public class ResourceServerConfig extends ResourceServerConfigurerAdapter { @Override public void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/api/**").authenticated() .anyRequest().permitAll(); } @Override public void configure(ResourceServerSecurityConfigurer config) throws Exception { config.resourceId("my_resource_id"); } }
在上面的程式碼中,我們定義了 /api/** 給予身份驗證,而其他請求允許匿名存取。我們也配置了資源 ID "my_resource_id" 以便在後續的授權流程中使用。
- 設定 Web 安全性
為了使用 Spring Security OAuth 安全性授權,我們也需要設定 Web 安全性。在 Spring Security OAuth 中,可以透過實作 SecurityConfigurer 介面來定義安全性。
@Configuration public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("user") .password("{noop}password") .roles("USER"); } @Override @Bean public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/oauth/**") .permitAll() .anyRequest() .authenticated() .and() .formLogin() .permitAll(); } }
在上面的程式碼中,我們定義了一個基於記憶體的使用者詳細資訊服務,並聲明了需要身份驗證的請求(也就是/oauth/** 後面的路徑都需要身份驗證,其他的路徑都可以匿名存取)。我們還配置了一個簡單的表單登入以便使用者登入應用程式。
- 實作 UserDetailsService
我們需要實作 UserDetailsService 介面以便在安全授權中使用。這裡我們直接使用記憶體來保存使用者帳號和密碼,並不涉及資料庫操作。
@Service public class UserDetailsServiceImpl implements UserDetailsService { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { if ("user".equals(username)) { return new User("user", "{noop}password", AuthorityUtils.createAuthorityList("ROLE_USER")); } else { throw new UsernameNotFoundException("username not found"); } } }
- 實作 API
接下來我們需要實作一個簡單的 API。我們在 /api/** 路徑下新增了一個 getGreeting() API,用於向客戶端傳回一條問候語。
@RestController @RequestMapping("/api") public class ApiController { @GetMapping("/greeting") public String getGreeting() { return "Hello, World!"; } }
- 測試授權流程
最後,我們需要測試授權流程是否如預期運作。首先,我們使用授權碼模式來取得授權碼:
http://localhost:8080/oauth/authorize?response_type=code&client_id=client&redirect_uri=http://localhost:8080&scope=read
在你的瀏覽器中存取上面的 URL,你將會被要求輸入使用者名稱和密碼以便授權。輸入使用者名稱 user 和密碼 password 然後點擊授權,你將被重定向到 http://localhost:8080/?code=xxx,其中 xxx 是授權碼。
接下來,我們使用密碼模式來取得存取權杖:
curl -X POST http://localhost:8080/oauth/token -H 'content-type: application/x-www-form-urlencoded' -d 'grant_type=password&username=user&password=password&client_id=client&client_secret=secret'
你將會收到一個JSON 回應,其中包含存取權杖和刷新令牌:
{ "access_token":"...", "token_type":"bearer", "refresh_token":"...", "expires_in":3600, "scope":"read" }
現在你可以使用這個存取權令牌來存取API 服務:
curl -X GET http://localhost:8080/api/greeting -H 'authorization: Bearer xxx'
其中xxx 是你的存取權杖。你將會收到一條 JSON 回應,其中包含問候語 "Hello, World!"。
在這篇文章中,我們介紹如何使用 Spring Security OAuth 進行安全授權。 Spring Security OAuth 是一個非常強大的框架,可以幫助我們實現 OAuth 授權流程中的所有角色。在實際應用中,我們可以根據不同的安全需求選擇不同的授權模式和服務配置。
以上是Java API 開發中使用 Spring Security OAuth 進行安全授權的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

Atom編輯器mac版下載
最受歡迎的的開源編輯器