1 /*
2  *
3  *  * Copyright 2019-2020 the original author or authors.
4  *  *
5  *  * Licensed under the Apache License, Version 2.0 (the "License");
6  *  * you may not use this file except in compliance with the License.
7  *  * You may obtain a copy of the License at
8  *  *
9  *  *      https://www.apache.org/licenses/LICENSE-2.0
10  *  *
11  *  * Unless required by applicable law or agreed to in writing, software
12  *  * distributed under the License is distributed on an "AS IS" BASIS,
13  *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  * See the License for the specific language governing permissions and
15  *  * limitations under the License.
16  *
17  */

18
19 package org.springdoc.security;
20
21 import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
22 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
23 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
24 import org.springframework.context.annotation.Bean;
25 import org.springframework.context.annotation.Configuration;
26 import org.springframework.security.core.Authentication;
27 import org.springframework.security.core.annotation.AuthenticationPrincipal;
28 import org.springframework.security.oauth2.provider.endpoint.FrameworkEndpointHandlerMapping;
29
30 import static org.springdoc.core.Constants.SPRINGDOC_ENABLED;
31 import static org.springdoc.core.SpringDocUtils.getConfig;
32
33 @Configuration
34 @ConditionalOnProperty(name = SPRINGDOC_ENABLED, matchIfMissing = true)
35 public class SpringDocSecurityConfiguration {
36
37     static {
38         getConfig().addRequestWrapperToIgnore(Authentication.class)
39                 .addResponseTypeToIgnore(Authentication.class)
40                 .addAnnotationsToIgnore(AuthenticationPrincipal.class);
41     }
42
43     @Configuration
44     @ConditionalOnBean(FrameworkEndpointHandlerMapping.class)
45     class SpringSecurityOAuth2ProviderConfiguration {
46         @Bean
47         @ConditionalOnMissingBean
48         SpringSecurityOAuth2Provider springSecurityOAuth2Provider(FrameworkEndpointHandlerMapping oauth2EndpointHandlerMapping) {
49             return new SpringSecurityOAuth2Provider(oauth2EndpointHandlerMapping);
50         }
51     }
52 }
53