2017-08-17 14:10:21 -05:00
|
|
|
/*
|
2019-07-22 09:31:10 -04:00
|
|
|
* Copyright 2002-2019 the original author or authors.
|
2017-09-18 10:47:26 -05:00
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
*
|
2019-03-14 15:30:00 -05:00
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
2017-09-18 10:47:26 -05:00
|
|
|
*
|
|
|
|
|
* 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.
|
2017-08-17 14:10:21 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package sample;
|
|
|
|
|
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
|
import org.springframework.security.config.annotation.method.configuration.EnableReactiveMethodSecurity;
|
2017-10-10 09:46:15 -05:00
|
|
|
import org.springframework.security.core.userdetails.MapReactiveUserDetailsService;
|
2017-08-17 14:10:21 -05:00
|
|
|
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
|
2017-10-10 16:57:15 -05:00
|
|
|
import org.springframework.security.config.web.server.ServerHttpSecurity;
|
2017-08-17 14:10:21 -05:00
|
|
|
import org.springframework.security.core.userdetails.User;
|
|
|
|
|
import org.springframework.security.core.userdetails.UserDetails;
|
|
|
|
|
import org.springframework.security.web.server.SecurityWebFilterChain;
|
|
|
|
|
|
2019-07-22 09:31:10 -04:00
|
|
|
import static org.springframework.security.config.Customizer.withDefaults;
|
|
|
|
|
|
2017-08-17 14:10:21 -05:00
|
|
|
/**
|
|
|
|
|
* @author Rob Winch
|
|
|
|
|
* @since 5.0
|
|
|
|
|
*/
|
|
|
|
|
@EnableWebFluxSecurity
|
|
|
|
|
@EnableReactiveMethodSecurity
|
|
|
|
|
public class SecurityConfig {
|
|
|
|
|
|
|
|
|
|
@Bean
|
2017-10-10 16:57:15 -05:00
|
|
|
SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http) throws Exception {
|
2017-08-17 14:10:21 -05:00
|
|
|
return http
|
2017-10-12 16:20:38 -05:00
|
|
|
// Demonstrate that method security works
|
|
|
|
|
// Best practice to use both for defense in depth
|
2019-07-22 09:31:10 -04:00
|
|
|
.authorizeExchange(exchanges ->
|
|
|
|
|
exchanges
|
|
|
|
|
.anyExchange().permitAll()
|
|
|
|
|
)
|
|
|
|
|
.httpBasic(withDefaults())
|
2017-08-17 14:10:21 -05:00
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Bean
|
2018-01-09 01:34:11 +09:00
|
|
|
public MapReactiveUserDetailsService userDetailsService() {
|
2017-10-20 16:59:52 -05:00
|
|
|
User.UserBuilder userBuilder = User.withDefaultPasswordEncoder();
|
|
|
|
|
UserDetails rob = userBuilder.username("rob").password("rob").roles("USER").build();
|
2017-11-16 06:19:44 +09:00
|
|
|
UserDetails admin = userBuilder.username("admin").password("admin").roles("USER", "ADMIN").build();
|
2017-10-10 09:46:15 -05:00
|
|
|
return new MapReactiveUserDetailsService(rob, admin);
|
2017-08-17 14:10:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|