|
| 1 | +/* |
| 2 | + * Copyright 2020-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package sample.config; |
| 17 | + |
| 18 | +import java.util.UUID; |
| 19 | + |
| 20 | +import com.nimbusds.jose.jwk.JWKSet; |
| 21 | +import com.nimbusds.jose.jwk.RSAKey; |
| 22 | +import com.nimbusds.jose.jwk.source.JWKSource; |
| 23 | +import com.nimbusds.jose.proc.SecurityContext; |
| 24 | +import sample.jose.Jwks; |
| 25 | +import sample.security.FederatedIdentityConfigurer; |
| 26 | +import sample.security.FederatedIdentityIdTokenCustomizer; |
| 27 | + |
| 28 | +import org.springframework.context.annotation.Bean; |
| 29 | +import org.springframework.context.annotation.Configuration; |
| 30 | +import org.springframework.core.Ordered; |
| 31 | +import org.springframework.core.annotation.Order; |
| 32 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 33 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; |
| 34 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; |
| 35 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; |
| 36 | +import org.springframework.security.config.Customizer; |
| 37 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 38 | +import org.springframework.security.config.annotation.web.configuration.OAuth2AuthorizationServerConfiguration; |
| 39 | +import org.springframework.security.oauth2.core.AuthorizationGrantType; |
| 40 | +import org.springframework.security.oauth2.core.ClientAuthenticationMethod; |
| 41 | +import org.springframework.security.oauth2.core.oidc.OidcScopes; |
| 42 | +import org.springframework.security.oauth2.server.authorization.JdbcOAuth2AuthorizationConsentService; |
| 43 | +import org.springframework.security.oauth2.server.authorization.JdbcOAuth2AuthorizationService; |
| 44 | +import org.springframework.security.oauth2.server.authorization.JwtEncodingContext; |
| 45 | +import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationConsentService; |
| 46 | +import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService; |
| 47 | +import org.springframework.security.oauth2.server.authorization.OAuth2TokenCustomizer; |
| 48 | +import org.springframework.security.oauth2.server.authorization.client.JdbcRegisteredClientRepository; |
| 49 | +import org.springframework.security.oauth2.server.authorization.client.RegisteredClient; |
| 50 | +import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository; |
| 51 | +import org.springframework.security.oauth2.server.authorization.config.ClientSettings; |
| 52 | +import org.springframework.security.oauth2.server.authorization.config.ProviderSettings; |
| 53 | +import org.springframework.security.web.SecurityFilterChain; |
| 54 | + |
| 55 | +/** |
| 56 | + * @author Steve Riesenberg |
| 57 | + * @since 0.2.3 |
| 58 | + */ |
| 59 | +@Configuration(proxyBeanMethods = false) |
| 60 | +public class AuthorizationServerConfig { |
| 61 | + |
| 62 | + @Bean |
| 63 | + @Order(Ordered.HIGHEST_PRECEDENCE) |
| 64 | + public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception { |
| 65 | + OAuth2AuthorizationServerConfiguration.applyDefaultSecurity(http); |
| 66 | + http.apply(new FederatedIdentityConfigurer()); |
| 67 | + return http.formLogin(Customizer.withDefaults()).build(); |
| 68 | + } |
| 69 | + |
| 70 | + @Bean |
| 71 | + public OAuth2TokenCustomizer<JwtEncodingContext> idTokenCustomizer() { |
| 72 | + return new FederatedIdentityIdTokenCustomizer(); |
| 73 | + } |
| 74 | + |
| 75 | + // @formatter:off |
| 76 | + @Bean |
| 77 | + public RegisteredClientRepository registeredClientRepository(JdbcTemplate jdbcTemplate) { |
| 78 | + RegisteredClient registeredClient = RegisteredClient.withId(UUID.randomUUID().toString()) |
| 79 | + .clientId("messaging-client") |
| 80 | + .clientSecret("{noop}secret") |
| 81 | + .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC) |
| 82 | + .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE) |
| 83 | + .authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN) |
| 84 | + .authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS) |
| 85 | + .redirectUri("http://127.0.0.1:8080/login/oauth2/code/messaging-client-oidc") |
| 86 | + .redirectUri("http://127.0.0.1:8080/authorized") |
| 87 | + .scope(OidcScopes.OPENID) |
| 88 | + .scope("message.read") |
| 89 | + .scope("message.write") |
| 90 | + .clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build()) |
| 91 | + .build(); |
| 92 | + |
| 93 | + // Save registered client in db as if in-memory |
| 94 | + JdbcRegisteredClientRepository registeredClientRepository = new JdbcRegisteredClientRepository(jdbcTemplate); |
| 95 | + registeredClientRepository.save(registeredClient); |
| 96 | + |
| 97 | + return registeredClientRepository; |
| 98 | + } |
| 99 | + // @formatter:on |
| 100 | + |
| 101 | + @Bean |
| 102 | + public OAuth2AuthorizationService authorizationService(JdbcTemplate jdbcTemplate, RegisteredClientRepository registeredClientRepository) { |
| 103 | + return new JdbcOAuth2AuthorizationService(jdbcTemplate, registeredClientRepository); |
| 104 | + } |
| 105 | + |
| 106 | + @Bean |
| 107 | + public OAuth2AuthorizationConsentService authorizationConsentService(JdbcTemplate jdbcTemplate, RegisteredClientRepository registeredClientRepository) { |
| 108 | + return new JdbcOAuth2AuthorizationConsentService(jdbcTemplate, registeredClientRepository); |
| 109 | + } |
| 110 | + |
| 111 | + @Bean |
| 112 | + public JWKSource<SecurityContext> jwkSource() { |
| 113 | + RSAKey rsaKey = Jwks.generateRsa(); |
| 114 | + JWKSet jwkSet = new JWKSet(rsaKey); |
| 115 | + return (jwkSelector, securityContext) -> jwkSelector.select(jwkSet); |
| 116 | + } |
| 117 | + |
| 118 | + @Bean |
| 119 | + public ProviderSettings providerSettings() { |
| 120 | + return ProviderSettings.builder().issuer("http://localhost:9000").build(); |
| 121 | + } |
| 122 | + |
| 123 | + @Bean |
| 124 | + public EmbeddedDatabase embeddedDatabase() { |
| 125 | + // @formatter:off |
| 126 | + return new EmbeddedDatabaseBuilder() |
| 127 | + .generateUniqueName(true) |
| 128 | + .setType(EmbeddedDatabaseType.H2) |
| 129 | + .setScriptEncoding("UTF-8") |
| 130 | + .addScript("org/springframework/security/oauth2/server/authorization/oauth2-authorization-schema.sql") |
| 131 | + .addScript("org/springframework/security/oauth2/server/authorization/oauth2-authorization-consent-schema.sql") |
| 132 | + .addScript("org/springframework/security/oauth2/server/authorization/client/oauth2-registered-client-schema.sql") |
| 133 | + .build(); |
| 134 | + // @formatter:on |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments