|
1 | | -package com.maryanto.dimas.example.configurations; |
| 1 | +package com.maryanto.dimas.example.config; |
2 | 2 |
|
3 | | -import com.google.common.collect.ImmutableList; |
4 | 3 | import org.springframework.beans.factory.annotation.Autowired; |
5 | | -import org.springframework.boot.autoconfigure.security.SecurityProperties; |
6 | 4 | import org.springframework.boot.web.servlet.FilterRegistrationBean; |
7 | 5 | import org.springframework.context.annotation.Bean; |
8 | 6 | import org.springframework.context.annotation.Configuration; |
9 | 7 | import org.springframework.core.annotation.Order; |
10 | 8 | import org.springframework.security.authentication.AuthenticationManager; |
11 | | -import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; |
12 | 9 | import org.springframework.security.config.annotation.authentication.configuration.EnableGlobalAuthentication; |
13 | 10 | import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; |
14 | 11 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
15 | 12 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
16 | 13 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
17 | 14 | import org.springframework.security.config.http.SessionCreationPolicy; |
| 15 | +import org.springframework.security.core.userdetails.User; |
| 16 | +import org.springframework.security.core.userdetails.UserDetails; |
| 17 | +import org.springframework.security.core.userdetails.UserDetailsService; |
18 | 18 | import org.springframework.security.oauth2.provider.ClientDetailsService; |
19 | 19 | import org.springframework.security.oauth2.provider.approval.ApprovalStore; |
20 | 20 | import org.springframework.security.oauth2.provider.approval.TokenApprovalStore; |
21 | 21 | import org.springframework.security.oauth2.provider.approval.TokenStoreUserApprovalHandler; |
22 | 22 | import org.springframework.security.oauth2.provider.request.DefaultOAuth2RequestFactory; |
23 | 23 | import org.springframework.security.oauth2.provider.token.TokenStore; |
24 | 24 | import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore; |
| 25 | +import org.springframework.security.provisioning.InMemoryUserDetailsManager; |
| 26 | +import org.springframework.security.web.csrf.CookieCsrfTokenRepository; |
25 | 27 | import org.springframework.web.cors.CorsConfiguration; |
26 | | -import org.springframework.web.cors.CorsConfigurationSource; |
27 | 28 | import org.springframework.web.cors.UrlBasedCorsConfigurationSource; |
28 | 29 | import org.springframework.web.filter.CorsFilter; |
29 | 30 |
|
30 | 31 | @Configuration |
31 | 32 | @EnableWebSecurity |
32 | 33 | @EnableGlobalMethodSecurity(securedEnabled = true) |
33 | 34 | @EnableGlobalAuthentication |
34 | | -@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) |
35 | | -public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter { |
36 | | - |
| 35 | +public class WebSecurityConfiguration { |
37 | 36 |
|
38 | 37 | @Autowired |
39 | 38 | private ClientDetailsService clientDetailsService; |
40 | 39 |
|
41 | 40 | @Bean |
42 | | - @Override |
43 | | - public AuthenticationManager authenticationManagerBean() throws Exception { |
44 | | - return super.authenticationManagerBean(); |
| 41 | + public UserDetailsService userDetailsService() { |
| 42 | + InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(); |
| 43 | + UserDetails user = User.withUsername("user").password("password").roles("USER").build(); |
| 44 | + UserDetails admin = User.withUsername("admin").password("password").roles("USER", "ADMIN").build(); |
| 45 | + manager.createUser(user); |
| 46 | + manager.createUser(admin); |
| 47 | + return manager; |
45 | 48 | } |
46 | 49 |
|
47 | | - @Override |
48 | | - protected void configure(AuthenticationManagerBuilder auth) throws Exception { |
49 | | -// super.configure(auth); |
50 | | - auth.inMemoryAuthentication() |
51 | | - .withUser("user").password("password").roles("USER").and() |
52 | | - .withUser("admin").password("password").roles("ADMIN", "USER"); |
| 50 | + @Configuration |
| 51 | + @Order(1) |
| 52 | + public static class ApiAuthenticationServer extends WebSecurityConfigurerAdapter { |
| 53 | + |
| 54 | + @Bean |
| 55 | + @Override |
| 56 | + public AuthenticationManager authenticationManagerBean() throws Exception { |
| 57 | + return super.authenticationManagerBean(); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + protected void configure(HttpSecurity http) throws Exception { |
| 62 | +// super.configure(http); |
| 63 | + http.cors().disable() |
| 64 | + .csrf().disable(); |
| 65 | + http.antMatcher("/api/**") |
| 66 | + .authorizeRequests() |
| 67 | + .antMatchers("/oauth/**", "/login").permitAll() |
| 68 | + .anyRequest().authenticated() |
| 69 | + .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); |
| 70 | + } |
| 71 | + |
53 | 72 | } |
54 | 73 |
|
55 | | - @Override |
56 | | - protected void configure(HttpSecurity http) throws Exception { |
| 74 | + @Configuration |
| 75 | + public static class WebFormAuthenticationServer extends WebSecurityConfigurerAdapter { |
| 76 | + |
| 77 | + @Bean |
| 78 | + @Override |
| 79 | + public AuthenticationManager authenticationManagerBean() throws Exception { |
| 80 | + return super.authenticationManagerBean(); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + protected void configure(HttpSecurity http) throws Exception { |
57 | 85 | // super.configure(http); |
58 | | - http |
59 | | - .csrf().disable() |
60 | | - .cors().disable() |
61 | | - .authorizeRequests() |
62 | | - .antMatchers("/oauth/**").permitAll() |
63 | | - .anyRequest().authenticated() |
64 | | - .and().httpBasic() |
65 | | - .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); |
| 86 | + http.cors().disable() |
| 87 | + .csrf().disable(); |
| 88 | + http.authorizeRequests() |
| 89 | + .antMatchers("/oauth/**").permitAll() |
| 90 | + .anyRequest().authenticated() |
| 91 | + .and().formLogin().permitAll() |
| 92 | + .and().httpBasic(); |
| 93 | + } |
| 94 | + |
66 | 95 | } |
67 | 96 |
|
68 | 97 |
|
@@ -99,21 +128,6 @@ public FilterRegistrationBean corsFilter() { |
99 | 128 | return bean; |
100 | 129 | } |
101 | 130 |
|
102 | | - |
103 | | - @Bean |
104 | | - public CorsConfigurationSource corsConfigurationSource() { |
105 | | - final CorsConfiguration configuration = new CorsConfiguration(); |
106 | | - configuration.setAllowedMethods(ImmutableList.of("HEAD", |
107 | | - "GET", "POST", "PUT", "DELETE", "PATCH")); |
108 | | - configuration.setAllowedOrigins(ImmutableList.of("*")); |
109 | | - configuration.setAllowCredentials(true); |
110 | | - configuration.setAllowedHeaders(ImmutableList.of("Authorization", "Cache-Control", "Content-Type")); |
111 | | - final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); |
112 | | - source.registerCorsConfiguration("/**", configuration); |
113 | | - return source; |
114 | | - } |
115 | | - |
116 | | - |
117 | 131 | @Bean |
118 | 132 | public TokenStore tokenStore() { |
119 | 133 | return new InMemoryTokenStore(); |
|
0 commit comments