|
| 1 | +/* |
| 2 | + * Copyright 2012-2021 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 | + * https://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 smoketest.graphql; |
| 18 | + |
| 19 | +import org.springframework.context.annotation.Bean; |
| 20 | +import org.springframework.context.annotation.Configuration; |
| 21 | +import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; |
| 22 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 23 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 24 | +import org.springframework.security.core.userdetails.User; |
| 25 | +import org.springframework.security.core.userdetails.UserDetails; |
| 26 | +import org.springframework.security.provisioning.InMemoryUserDetailsManager; |
| 27 | +import org.springframework.security.web.DefaultSecurityFilterChain; |
| 28 | + |
| 29 | +import static org.springframework.security.config.Customizer.withDefaults; |
| 30 | + |
| 31 | +@Configuration |
| 32 | +@EnableWebSecurity |
| 33 | +@EnableGlobalMethodSecurity(prePostEnabled = true) |
| 34 | +public class SecurityConfig { |
| 35 | + |
| 36 | + @Bean |
| 37 | + DefaultSecurityFilterChain springWebFilterChain(HttpSecurity http) throws Exception { |
| 38 | + return http.csrf((csrf) -> csrf.disable()) |
| 39 | + // Demonstrate that method security works |
| 40 | + // Best practice to use both for defense in depth |
| 41 | + .authorizeRequests((requests) -> requests.anyRequest().permitAll()).httpBasic(withDefaults()).build(); |
| 42 | + } |
| 43 | + |
| 44 | + @Bean |
| 45 | + public static InMemoryUserDetailsManager userDetailsService() { |
| 46 | + User.UserBuilder userBuilder = User.withDefaultPasswordEncoder(); |
| 47 | + UserDetails rob = userBuilder.username("rob").password("rob").roles("USER").build(); |
| 48 | + UserDetails admin = userBuilder.username("admin").password("admin").roles("USER", "ADMIN").build(); |
| 49 | + return new InMemoryUserDetailsManager(rob, admin); |
| 50 | + } |
| 51 | + |
| 52 | +} |
0 commit comments