-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Redis implementations of core components to demo sample
- Loading branch information
Showing
28 changed files
with
2,205 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
samples/demo-authorizationserver/src/main/java/sample/config/RedisConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright 2020-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package sample.config; | ||
|
||
import java.util.Arrays; | ||
|
||
import sample.data.redis.RedisOAuth2AuthorizationConsentService; | ||
import sample.data.redis.RedisOAuth2AuthorizationService; | ||
import sample.data.redis.RedisRegisteredClientRepository; | ||
import sample.data.redis.convert.BytesToClaimsHolderConverter; | ||
import sample.data.redis.convert.BytesToOAuth2AuthorizationRequestConverter; | ||
import sample.data.redis.convert.BytesToUsernamePasswordAuthenticationTokenConverter; | ||
import sample.data.redis.convert.ClaimsHolderToBytesConverter; | ||
import sample.data.redis.convert.OAuth2AuthorizationRequestToBytesConverter; | ||
import sample.data.redis.convert.UsernamePasswordAuthenticationTokenToBytesConverter; | ||
import sample.data.redis.repository.OAuth2AuthorizationGrantAuthorizationRepository; | ||
import sample.data.redis.repository.OAuth2RegisteredClientRepository; | ||
import sample.data.redis.repository.OAuth2UserConsentRepository; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.core.convert.RedisCustomConversions; | ||
import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository; | ||
|
||
/** | ||
* @author Joe Grandja | ||
* @since 1.4 | ||
*/ | ||
@Profile("redis") | ||
@Configuration(proxyBeanMethods = false) | ||
public class RedisConfig { | ||
|
||
@Bean | ||
public RedisConnectionFactory redisConnectionFactory() { | ||
return new JedisConnectionFactory(); | ||
} | ||
|
||
@Bean | ||
public RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory redisConnectionFactory) { | ||
RedisTemplate<byte[], byte[]> redisTemplate = new RedisTemplate<>(); | ||
redisTemplate.setConnectionFactory(redisConnectionFactory); | ||
return redisTemplate; | ||
} | ||
|
||
@Bean | ||
public RedisCustomConversions redisCustomConversions() { | ||
return new RedisCustomConversions(Arrays.asList(new UsernamePasswordAuthenticationTokenToBytesConverter(), | ||
new BytesToUsernamePasswordAuthenticationTokenConverter(), | ||
new OAuth2AuthorizationRequestToBytesConverter(), new BytesToOAuth2AuthorizationRequestConverter(), | ||
new ClaimsHolderToBytesConverter(), new BytesToClaimsHolderConverter())); | ||
} | ||
|
||
@Bean | ||
public RedisRegisteredClientRepository registeredClientRepository( | ||
OAuth2RegisteredClientRepository registeredClientRepository) { | ||
RedisRegisteredClientRepository redisRegisteredClientRepository = new RedisRegisteredClientRepository(registeredClientRepository); | ||
RegisteredClients.defaults().forEach(redisRegisteredClientRepository::save); | ||
return redisRegisteredClientRepository; | ||
} | ||
|
||
@Bean | ||
public RedisOAuth2AuthorizationService authorizationService(RegisteredClientRepository registeredClientRepository, | ||
OAuth2AuthorizationGrantAuthorizationRepository authorizationGrantAuthorizationRepository) { | ||
return new RedisOAuth2AuthorizationService(registeredClientRepository, | ||
authorizationGrantAuthorizationRepository); | ||
} | ||
|
||
@Bean | ||
public RedisOAuth2AuthorizationConsentService authorizationConsentService( | ||
OAuth2UserConsentRepository userConsentRepository) { | ||
return new RedisOAuth2AuthorizationConsentService(userConsentRepository); | ||
} | ||
|
||
} |
97 changes: 97 additions & 0 deletions
97
samples/demo-authorizationserver/src/main/java/sample/config/RegisteredClients.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright 2020-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package sample.config; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import org.springframework.security.oauth2.core.AuthorizationGrantType; | ||
import org.springframework.security.oauth2.core.ClientAuthenticationMethod; | ||
import org.springframework.security.oauth2.core.oidc.OidcScopes; | ||
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient; | ||
import org.springframework.security.oauth2.server.authorization.settings.ClientSettings; | ||
import org.springframework.security.oauth2.server.authorization.settings.TokenSettings; | ||
|
||
/** | ||
* @author Joe Grandja | ||
* @since 1.4 | ||
*/ | ||
final class RegisteredClients { | ||
|
||
// @formatter:off | ||
static List<RegisteredClient> defaults() { | ||
RegisteredClient messagingClient = RegisteredClient.withId(UUID.randomUUID().toString()) | ||
.clientId("messaging-client") | ||
.clientSecret("{noop}secret") | ||
.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC) | ||
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE) | ||
.authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN) | ||
.authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS) | ||
.redirectUri("http://127.0.0.1:8080/login/oauth2/code/messaging-client-oidc") | ||
.redirectUri("http://127.0.0.1:8080/authorized") | ||
.postLogoutRedirectUri("http://127.0.0.1:8080/logged-out") | ||
.scope(OidcScopes.OPENID) | ||
.scope(OidcScopes.PROFILE) | ||
.scope("message.read") | ||
.scope("message.write") | ||
.scope("user.read") | ||
.clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build()) | ||
.build(); | ||
|
||
RegisteredClient deviceClient = RegisteredClient.withId(UUID.randomUUID().toString()) | ||
.clientId("device-messaging-client") | ||
.clientAuthenticationMethod(ClientAuthenticationMethod.NONE) | ||
.authorizationGrantType(AuthorizationGrantType.DEVICE_CODE) | ||
.authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN) | ||
.scope("message.read") | ||
.scope("message.write") | ||
.build(); | ||
|
||
RegisteredClient tokenExchangeClient = RegisteredClient.withId(UUID.randomUUID().toString()) | ||
.clientId("token-client") | ||
.clientSecret("{noop}token") | ||
.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC) | ||
.authorizationGrantType(new AuthorizationGrantType("urn:ietf:params:oauth:grant-type:token-exchange")) | ||
.scope("message.read") | ||
.scope("message.write") | ||
.build(); | ||
|
||
RegisteredClient mtlsDemoClient = RegisteredClient.withId(UUID.randomUUID().toString()) | ||
.clientId("mtls-demo-client") | ||
.clientAuthenticationMethod(ClientAuthenticationMethod.TLS_CLIENT_AUTH) | ||
.clientAuthenticationMethod(ClientAuthenticationMethod.SELF_SIGNED_TLS_CLIENT_AUTH) | ||
.authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS) | ||
.scope("message.read") | ||
.scope("message.write") | ||
.clientSettings( | ||
ClientSettings.builder() | ||
.x509CertificateSubjectDN("CN=demo-client-sample,OU=Spring Samples,O=Spring,C=US") | ||
.jwkSetUrl("http://127.0.0.1:8080/jwks") | ||
.build() | ||
) | ||
.tokenSettings( | ||
TokenSettings.builder() | ||
.x509CertificateBoundAccessTokens(true) | ||
.build() | ||
) | ||
.build(); | ||
|
||
return Arrays.asList(messagingClient, deviceClient, tokenExchangeClient, mtlsDemoClient); | ||
} | ||
// @formatter:on | ||
|
||
} |
Oops, something went wrong.