一、介紹
OAuth2是一種授權框架,通常用於提供Web應用程序或服務對持授權的用戶數據的訪問許可權。而JWT(JSON Web Token)則是一種可以跨域傳輸的安全令牌,是用於身份驗證的開放標準。
jwtoauth2是基於OAuth2的標準實現。它使用JWT作為標準的令牌格式,與OAuth2一起來對外部客戶端應用程序的訪問進行授權。
二、jwtoauth2與oauth2區別
1、令牌的格式
在OAuth2中,令牌通常是一個字元串,該字元串由一組隨機字元和一個可選的刷新令牌組成,以及一個可選的過期時間。
Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
而在jwtoauth2中,令牌是一個經過哈希處理的JSON對象。它包含所有必要的數據,如令牌類型、頒發者、頒發時間、過期時間和訪問許可權等。
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ikpva MTIsImV4cCI6MTUxNjIzOTM4NCwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_ad Qssw5c
2、令牌的加密方式
OAuth2主要使用傳輸層安全性協議(TLS)來加密令牌,而jwtoauth2則使用JSON Web簽名(JWS)或JSON Web加密(JWE)來保證令牌的安全性。
3、令牌的用途
在OAuth2中,令牌主要用於授權訪問受保護資源。在jwtoauth2中,令牌不僅可以用於授權訪問受保護資源,還可以用於設備身份驗證、用戶身份驗證等多種用途。
三、jwtoauth2示例代碼
以下示例介紹了如何通過jwtoauth2在Spring Boot中使用OAuth2。
1、添加依賴項
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-oauth2-jose</artifactId> </dependency>
2、配置Spring Security
@Configuration @EnableResourceServer public class ResourceServerConfig extends ResourceServerConfigurerAdapter { @Override public void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/api/**").authenticated() .and().oauth2ResourceServer().jwt(); } @Bean JwtDecoder jwtDecoder() { NimbusJwtDecoder jwtDecoder = NimbusJwtDecoder.withJwkSetUri(jwkSetUri).build(); jwtDecoder.setClaimSetConverter(new UsernameSubClaimAdapter()); return jwtDecoder; } } @Component public class UsernameSubClaimAdapter implements Converter<Jwt, Jwt> { @Override public Jwt convert(Jwt jwt) { Map<String, Object> claims = new LinkedHashMap<>(jwt.getClaims()); Object username = jwt.getClaims().get("sub"); claims.put("username", username); return new Jwt(jwt.getTokenValue(), jwt.getIssuedAt(), jwt.getExpiresAt(), jwt.getHeaders(), claims); } }
3、配置OAuth2客戶端
@Configuration public class ClientConfig { @Bean ClientRegistrationRepository clientRegistrationRepository() { List<ClientRegistration> registrations = new ArrayList<>(); registrations.add(gitHubRegistration()); return new InMemoryClientRegistrationRepository(registrations); } private ClientRegistration gitHubRegistration() { return ClientRegistration.withRegistrationId("github") .clientId("github-client-id") .clientSecret("github-client-secret") .clientAuthenticationMethod(ClientAuthenticationMethod.BASIC) .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE) .redirectUri("{baseUrl}/login/oauth2/code/{registrationId}") .scope("read:user") .authorizationUri("https://github.com/login/oauth/authorize") .tokenUri("https://github.com/login/oauth/access_token") .userInfoUri("https://api.github.com/user") .userNameAttributeName("id") .clientName("GitHub") .build(); } }
4、啟動應用程序後,可以通過下面的URI進行OAuth2授權:
http://localhost:8080/oauth2/authorization/github
5、在授權後,可以通過下面的URI獲取受保護資源:
http://localhost:8080/api/user
四、結論
jwtoauth2是OAuth2協議的補充,它使用JWT代替了OAuth2的令牌格式,更加安全可靠。通過對比,我們可以清晰地認識到它們在令牌格式、令牌加密方式、令牌用途等方面的區別。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/284935.html