View Javadoc
1   /*
2    * Copyright 2020 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    *      http://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  
17  package org.bremersee.security.authentication;
18  
19  import lombok.extern.slf4j.Slf4j;
20  import org.bremersee.web.CorsProperties;
21  import org.springframework.beans.factory.ObjectProvider;
22  import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
23  import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
24  import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
25  import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
26  import org.springframework.boot.context.event.ApplicationReadyEvent;
27  import org.springframework.boot.context.properties.EnableConfigurationProperties;
28  import org.springframework.context.annotation.Bean;
29  import org.springframework.context.annotation.Conditional;
30  import org.springframework.context.annotation.Configuration;
31  import org.springframework.context.event.EventListener;
32  import org.springframework.core.env.Environment;
33  import org.springframework.security.authentication.ReactiveAuthenticationManager;
34  import org.springframework.security.config.web.server.ServerHttpSecurity;
35  import org.springframework.security.config.web.server.ServerHttpSecurity.AuthorizeExchangeSpec;
36  import org.springframework.security.core.userdetails.ReactiveUserDetailsService;
37  import org.springframework.security.crypto.password.PasswordEncoder;
38  import org.springframework.security.web.server.SecurityWebFilterChain;
39  
40  /**
41   * The reactive resource server security auto configuration.
42   *
43   * @author Christian Bremer
44   */
45  @ConditionalOnWebApplication(type = Type.REACTIVE)
46  @Conditional({ResourceServerAutoSecurityCondition.class})
47  @ConditionalOnClass({
48      ServerHttpSecurity.class,
49      ReactiveAuthenticationManager.class,
50      PasswordFlowProperties.class
51  })
52  @ConditionalOnMissingBean(type = {
53      "org.bremersee.actuator.security.authentication.ReactiveResourceServerWithActuatorAutoConfiguration"
54  })
55  @EnableConfigurationProperties({CorsProperties.class, AuthProperties.class})
56  @Configuration
57  @Slf4j
58  public class ReactiveResourceServerAutoConfiguration
59      extends AbstractReactiveResourceServerAutoConfiguration {
60  
61    /**
62     * Instantiates a new reactive resource server security auto configuration.
63     *
64     * @param environment the environment
65     * @param corsProperties the cors properties
66     * @param securityProperties the security properties
67     * @param jwtConverterProvider the jwt converter provider
68     * @param userDetailsServiceProvider the user details service provider
69     * @param passwordEncoderProvider the password encoder provider
70     */
71    public ReactiveResourceServerAutoConfiguration(
72        Environment environment,
73        CorsProperties corsProperties,
74        AuthProperties securityProperties,
75        ObjectProvider<JsonPathReactiveJwtConverter> jwtConverterProvider,
76        ObjectProvider<ReactiveUserDetailsService> userDetailsServiceProvider,
77        ObjectProvider<PasswordEncoder> passwordEncoderProvider) {
78      super(environment, corsProperties, securityProperties, jwtConverterProvider,
79          userDetailsServiceProvider, passwordEncoderProvider);
80    }
81  
82    @EventListener(ApplicationReadyEvent.class)
83    @Override
84    public void init() {
85      super.init();
86    }
87  
88    @Override
89    protected AuthorizeExchangeSpec init(ServerHttpSecurity http) {
90      return http.authorizeExchange();
91    }
92  
93    /**
94     * Resource server filter chain.
95     *
96     * @param httpProvider the http provider
97     * @return the security web filter chain
98     */
99    @Bean
100   public SecurityWebFilterChain resourceServerFilterChain(
101       ObjectProvider<ServerHttpSecurity> httpProvider) {
102     return super.resourceServerFilterChain(httpProvider.getIfAvailable());
103   }
104 
105 }