2020-06-23 22:03:16 +07:00
|
|
|
/*
|
|
|
|
* Copyright 2020 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.
|
|
|
|
*/
|
2020-07-22 16:06:09 +07:00
|
|
|
package org.springframework.security.config.annotation.web.configuration;
|
2020-06-23 22:03:16 +07:00
|
|
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
2020-11-19 20:29:34 +07:00
|
|
|
import org.springframework.core.Ordered;
|
|
|
|
import org.springframework.core.annotation.Order;
|
|
|
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
|
|
import org.springframework.security.config.annotation.web.configurers.oauth2.server.authorization.OAuth2AuthorizationServerConfigurer;
|
|
|
|
import org.springframework.security.web.SecurityFilterChain;
|
|
|
|
import org.springframework.security.web.util.matcher.RequestMatcher;
|
2020-06-23 22:03:16 +07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@link Configuration} for OAuth 2.0 Authorization Server support.
|
|
|
|
*
|
|
|
|
* @author Joe Grandja
|
|
|
|
* @since 0.0.1
|
2020-11-19 20:29:34 +07:00
|
|
|
* @see OAuth2AuthorizationServerConfigurer
|
2020-06-23 22:03:16 +07:00
|
|
|
*/
|
2020-09-03 17:39:58 +07:00
|
|
|
@Configuration(proxyBeanMethods = false)
|
2020-06-23 22:03:16 +07:00
|
|
|
public class OAuth2AuthorizationServerConfiguration {
|
|
|
|
|
|
|
|
@Bean
|
2020-11-19 20:29:34 +07:00
|
|
|
@Order(Ordered.HIGHEST_PRECEDENCE)
|
|
|
|
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
|
|
|
|
applyDefaultSecurity(http);
|
|
|
|
return http.build();
|
2020-06-23 22:03:16 +07:00
|
|
|
}
|
|
|
|
|
2020-11-19 20:29:34 +07:00
|
|
|
// @formatter:off
|
|
|
|
public static void applyDefaultSecurity(HttpSecurity http) throws Exception {
|
|
|
|
OAuth2AuthorizationServerConfigurer<HttpSecurity> authorizationServerConfigurer =
|
|
|
|
new OAuth2AuthorizationServerConfigurer<>();
|
2021-01-05 15:53:21 +07:00
|
|
|
RequestMatcher endpointsMatcher = authorizationServerConfigurer
|
|
|
|
.getEndpointsMatcher();
|
2020-11-19 20:29:34 +07:00
|
|
|
|
|
|
|
http
|
2021-01-05 15:53:21 +07:00
|
|
|
.requestMatcher(endpointsMatcher)
|
2020-11-19 20:29:34 +07:00
|
|
|
.authorizeRequests(authorizeRequests ->
|
|
|
|
authorizeRequests.anyRequest().authenticated()
|
|
|
|
)
|
2021-01-05 15:53:21 +07:00
|
|
|
.csrf(csrf -> csrf.ignoringRequestMatchers(endpointsMatcher))
|
2020-11-19 20:29:34 +07:00
|
|
|
.apply(authorizationServerConfigurer);
|
|
|
|
}
|
|
|
|
// @formatter:on
|
2020-06-23 22:03:16 +07:00
|
|
|
}
|