|
@@ -1,131 +0,0 @@
|
|
-/*
|
|
|
|
- * Copyright 2020-2023 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.UUID;
|
|
|
|
-
|
|
|
|
-import com.nimbusds.jose.jwk.JWKSet;
|
|
|
|
-import com.nimbusds.jose.jwk.RSAKey;
|
|
|
|
-import com.nimbusds.jose.jwk.source.JWKSource;
|
|
|
|
-import com.nimbusds.jose.proc.SecurityContext;
|
|
|
|
-import sample.jose.Jwks;
|
|
|
|
-
|
|
|
|
-import org.springframework.context.annotation.Bean;
|
|
|
|
-import org.springframework.context.annotation.Configuration;
|
|
|
|
-import org.springframework.core.Ordered;
|
|
|
|
-import org.springframework.core.annotation.Order;
|
|
|
|
-import org.springframework.security.config.Customizer;
|
|
|
|
-import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
|
|
-import org.springframework.security.config.annotation.web.configurers.oauth2.server.resource.OAuth2ResourceServerConfigurer;
|
|
|
|
-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.jwt.JwtDecoder;
|
|
|
|
-import org.springframework.security.oauth2.server.authorization.InMemoryOAuth2AuthorizationConsentService;
|
|
|
|
-import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationConsentService;
|
|
|
|
-import org.springframework.security.oauth2.server.authorization.client.InMemoryRegisteredClientRepository;
|
|
|
|
-import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
|
|
|
|
-import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository;
|
|
|
|
-import org.springframework.security.oauth2.server.authorization.config.annotation.web.configuration.OAuth2AuthorizationServerConfiguration;
|
|
|
|
-import org.springframework.security.oauth2.server.authorization.config.annotation.web.configurers.OAuth2AuthorizationServerConfigurer;
|
|
|
|
-import org.springframework.security.oauth2.server.authorization.settings.AuthorizationServerSettings;
|
|
|
|
-import org.springframework.security.oauth2.server.authorization.settings.ClientSettings;
|
|
|
|
-import org.springframework.security.web.SecurityFilterChain;
|
|
|
|
-import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;
|
|
|
|
-import org.springframework.security.web.util.matcher.RequestMatcher;
|
|
|
|
-
|
|
|
|
-/**
|
|
|
|
- * @author Joe Grandja
|
|
|
|
- * @author Daniel Garnier-Moiroux
|
|
|
|
- */
|
|
|
|
-@Configuration(proxyBeanMethods = false)
|
|
|
|
-public class AuthorizationServerConfig {
|
|
|
|
- private static final String CUSTOM_CONSENT_PAGE_URI = "/oauth2/consent";
|
|
|
|
-
|
|
|
|
- @Bean
|
|
|
|
- @Order(Ordered.HIGHEST_PRECEDENCE)
|
|
|
|
- public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
|
|
|
|
- OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
|
|
|
|
- new OAuth2AuthorizationServerConfigurer();
|
|
|
|
- authorizationServerConfigurer
|
|
|
|
- .authorizationEndpoint(authorizationEndpoint ->
|
|
|
|
- authorizationEndpoint.consentPage(CUSTOM_CONSENT_PAGE_URI))
|
|
|
|
- .oidc(Customizer.withDefaults()); // Enable OpenID Connect 1.0
|
|
|
|
-
|
|
|
|
- RequestMatcher endpointsMatcher = authorizationServerConfigurer
|
|
|
|
- .getEndpointsMatcher();
|
|
|
|
-
|
|
|
|
- http
|
|
|
|
- .securityMatcher(endpointsMatcher)
|
|
|
|
- .authorizeHttpRequests(authorize ->
|
|
|
|
- authorize.anyRequest().authenticated()
|
|
|
|
- )
|
|
|
|
- .csrf(csrf -> csrf.ignoringRequestMatchers(endpointsMatcher))
|
|
|
|
- .exceptionHandling(exceptions ->
|
|
|
|
- exceptions.authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/login"))
|
|
|
|
- )
|
|
|
|
- .oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt)
|
|
|
|
- .apply(authorizationServerConfigurer);
|
|
|
|
- return http.build();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // @formatter:off
|
|
|
|
- @Bean
|
|
|
|
- public RegisteredClientRepository registeredClientRepository() {
|
|
|
|
- RegisteredClient registeredClient = 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")
|
|
|
|
- .clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build())
|
|
|
|
- .build();
|
|
|
|
- return new InMemoryRegisteredClientRepository(registeredClient);
|
|
|
|
- }
|
|
|
|
- // @formatter:on
|
|
|
|
-
|
|
|
|
- @Bean
|
|
|
|
- public JWKSource<SecurityContext> jwkSource() {
|
|
|
|
- RSAKey rsaKey = Jwks.generateRsa();
|
|
|
|
- JWKSet jwkSet = new JWKSet(rsaKey);
|
|
|
|
- return (jwkSelector, securityContext) -> jwkSelector.select(jwkSet);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Bean
|
|
|
|
- public JwtDecoder jwtDecoder(JWKSource<SecurityContext> jwkSource) {
|
|
|
|
- return OAuth2AuthorizationServerConfiguration.jwtDecoder(jwkSource);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Bean
|
|
|
|
- public AuthorizationServerSettings authorizationServerSettings() {
|
|
|
|
- return AuthorizationServerSettings.builder().build();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Bean
|
|
|
|
- public OAuth2AuthorizationConsentService authorizationConsentService() {
|
|
|
|
- // Will be used by the ConsentController
|
|
|
|
- return new InMemoryOAuth2AuthorizationConsentService();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-}
|
|
|