3
3
import org .springframework .beans .factory .annotation .Autowired ;
4
4
import org .springframework .boot .autoconfigure .ImportAutoConfiguration ;
5
5
import org .springframework .boot .context .properties .EnableConfigurationProperties ;
6
+ import org .springframework .context .ApplicationContext ;
6
7
import org .springframework .context .annotation .Bean ;
7
8
import org .springframework .context .annotation .Configuration ;
9
+ import org .springframework .core .io .support .SpringFactoriesLoader ;
8
10
import org .springframework .data .jpa .repository .config .EnableJpaAuditing ;
9
11
import org .springframework .data .web .config .EnableSpringDataWebSupport ;
10
12
import org .springframework .scheduling .annotation .EnableAsync ;
11
- import org .springframework .security .authentication .AuthenticationManager ;
13
+ import org .springframework .security .authentication .*;
14
+ import org .springframework .security .authentication .dao .DaoAuthenticationProvider ;
15
+ import org .springframework .security .config .annotation .ObjectPostProcessor ;
12
16
import org .springframework .security .config .annotation .authentication .builders .AuthenticationManagerBuilder ;
13
- import org .springframework .security .config .annotation .web . WebSecurityConfigurer ;
17
+ import org .springframework .security .config .annotation .authentication . configuration . AuthenticationConfiguration ;
14
18
import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
15
19
import org .springframework .security .config .annotation .web .builders .WebSecurity ;
20
+ //import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
16
21
import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
22
+ import org .springframework .security .config .annotation .web .configurers .AbstractHttpConfigurer ;
23
+ import org .springframework .security .config .annotation .web .configurers .DefaultLoginPageConfigurer ;
17
24
import org .springframework .security .core .userdetails .UserDetailsService ;
18
25
import org .springframework .security .crypto .bcrypt .BCryptPasswordEncoder ;
19
26
import org .springframework .security .crypto .password .PasswordEncoder ;
20
27
import org .springframework .security .web .SecurityFilterChain ;
28
+ import org .springframework .security .web .access .intercept .FilterSecurityInterceptor ;
21
29
import org .springframework .security .web .authentication .AuthenticationSuccessHandler ;
22
30
import org .springframework .security .web .authentication .UsernamePasswordAuthenticationFilter ;
31
+ import org .springframework .security .web .context .request .async .WebAsyncManagerIntegrationFilter ;
32
+ import org .springframework .web .accept .ContentNegotiationStrategy ;
33
+ import org .springframework .web .accept .HeaderContentNegotiationStrategy ;
23
34
import org .springframework .web .servlet .config .annotation .EnableWebMvc ;
24
35
import org .woehlke .java .simpleworklist .domain .security .access .ApplicationUserDetailsService ;
25
36
37
+ import java .util .HashMap ;
38
+ import java .util .List ;
39
+ import java .util .Map ;
40
+
26
41
27
42
@ Configuration
28
43
@ EnableAsync
29
44
@ EnableJpaAuditing
30
45
@ EnableWebMvc
31
46
@ EnableSpringDataWebSupport
32
- @ EnableWebSecurity
33
47
@ ImportAutoConfiguration ({
34
48
WebMvcConfig .class
35
49
})
36
50
@ EnableConfigurationProperties ({
37
51
SimpleworklistProperties .class
38
52
})
39
- public class WebSecurityConfig implements WebSecurityConfigurer <WebSecurity > {
53
+ @ EnableWebSecurity
54
+ public class WebSecurityConfig /* extends WebSecurityConfigurerAdapter implements WebSecurityConfigurer<WebSecurity> */ {
40
55
41
- private final AuthenticationManagerBuilder authenticationManagerBuilder ;
42
- private final AuthenticationSuccessHandler authenticationSuccessHandler ;
56
+ //private final AuthenticationManagerBuilder authenticationManagerBuilder;
57
+ //private final AuthenticationSuccessHandler authenticationSuccessHandler;
58
+ //private final AuthenticationManager authenticationManager;
43
59
private final ApplicationUserDetailsService applicationUserDetailsService ;
44
60
private final SimpleworklistProperties simpleworklistProperties ;
45
61
46
62
@ Autowired
47
63
public WebSecurityConfig (
48
- AuthenticationManagerBuilder auth ,
49
- AuthenticationSuccessHandler authenticationSuccessHandler ,
64
+ //AuthenticationManagerBuilder auth,
65
+ //AuthenticationSuccessHandler authenticationSuccessHandler,
66
+ //AuthenticationManager authenticationManager,
50
67
ApplicationUserDetailsService applicationUserDetailsService ,
51
- SimpleworklistProperties simpleworklistProperties ) {
52
- this .authenticationManagerBuilder = auth ;
53
- this .authenticationSuccessHandler = authenticationSuccessHandler ;
68
+ SimpleworklistProperties simpleworklistProperties
69
+ ) {
70
+ //this.authenticationManagerBuilder = auth;
71
+ //this.authenticationSuccessHandler = authenticationSuccessHandler;
72
+ //this.authenticationManager = authenticationManager;
54
73
this .applicationUserDetailsService = applicationUserDetailsService ;
55
74
this .simpleworklistProperties = simpleworklistProperties ;
56
75
}
@@ -70,63 +89,212 @@ public PasswordEncoder encoder(){
70
89
return new BCryptPasswordEncoder (strength );
71
90
}
72
91
92
+ /*
73
93
@Bean
74
94
public AuthenticationManager authenticationManager() throws Exception {
75
95
return authenticationManagerBuilder
76
96
.userDetailsService(userDetailsService())
77
97
.passwordEncoder(encoder()).and().build();
78
98
}
99
+ */
100
+
101
+ /*
102
+ @Bean
103
+ public AuthenticationManager authenticationManager(
104
+ AuthenticationConfiguration authenticationConfiguration
105
+ ) throws Exception {
106
+ return authenticationConfiguration.getAuthenticationManager();
107
+ }
79
108
80
109
@Bean
81
110
public UsernamePasswordAuthenticationFilter authenticationFilter() throws Exception {
82
111
UsernamePasswordAuthenticationFilter filter = new UsernamePasswordAuthenticationFilter();
83
- filter .setAuthenticationManager (authenticationManager () );
112
+ filter.setAuthenticationManager(authenticationManager);
84
113
filter.setFilterProcessesUrl(simpleworklistProperties.getWebSecurity().getLoginProcessingUrl());
85
114
return filter;
86
115
}
87
116
88
- @ Override
89
- public void init (WebSecurity builder ) throws Exception {
117
+ private AuthenticationManagerBuilder authenticationBuilder;
118
+
119
+ private AuthenticationManagerBuilder localConfigureAuthenticationBldr;
120
+
121
+ private ApplicationContext context;
122
+
123
+ private HttpSecurity http;
124
+
125
+ private boolean disableDefaults;
126
+
127
+ private AuthenticationTrustResolver trustResolver = new AuthenticationTrustResolverImpl();
128
+
129
+ private ContentNegotiationStrategy contentNegotiationStrategy = new HeaderContentNegotiationStrategy();
130
+
131
+ private ObjectPostProcessor<Object> objectPostProcessor = new ObjectPostProcessor<Object>() {
132
+ @Override
133
+ public <T> T postProcess(T object) {
134
+ throw new IllegalStateException(ObjectPostProcessor.class.getName()
135
+ + " is a required bean. Ensure you have used @EnableWebSecurity and @Configuration");
136
+ }
137
+ };
138
+
139
+ private AuthenticationEventPublisher getAuthenticationEventPublisher() {
140
+ if (this.context.getBeanNamesForType(AuthenticationEventPublisher.class).length > 0) {
141
+ return this.context.getBean(AuthenticationEventPublisher.class);
142
+ }
143
+ return this.objectPostProcessor.postProcess(new DefaultAuthenticationEventPublisher());
144
+ }
145
+ */
146
+
147
+ /**
148
+ * Creates the shared objects
149
+ * @return the shared Objects
150
+ */
151
+ /*
152
+ private Map<Class<?>, Object> createSharedObjects() {
153
+ Map<Class<?>, Object> sharedObjects = new HashMap<>();
154
+ sharedObjects.putAll(this.localConfigureAuthenticationBldr.getSharedObjects());
155
+ sharedObjects.put(UserDetailsService.class, userDetailsService());
156
+ sharedObjects.put(ApplicationContext.class, this.context);
157
+ sharedObjects.put(ContentNegotiationStrategy.class, this.contentNegotiationStrategy);
158
+ sharedObjects.put(AuthenticationTrustResolver.class, this.trustResolver);
159
+ return sharedObjects;
160
+ }
161
+
162
+ private void applyDefaultConfiguration(HttpSecurity http) throws Exception {
163
+ http.csrf();
164
+ http.addFilter(new WebAsyncManagerIntegrationFilter());
165
+ http.exceptionHandling();
166
+ http.headers();
167
+ http.sessionManagement();
168
+ http.securityContext();
169
+ http.requestCache();
170
+ http.anonymous();
171
+ http.servletApi();
172
+ http.apply(new DefaultLoginPageConfigurer<>());
173
+ http.logout();
174
+ }
175
+ */
176
+ /**
177
+ * Creates the {@link HttpSecurity} or returns the current instance
178
+ * @return the {@link HttpSecurity}
179
+ * @throws Exception
180
+ */
181
+ @ SuppressWarnings ({ "rawtypes" , "unchecked" })
182
+ /*
183
+ protected final HttpSecurity getHttp() throws Exception {
184
+ if (this.http != null) {
185
+ return this.http;
186
+ }
187
+ AuthenticationEventPublisher eventPublisher = getAuthenticationEventPublisher();
188
+ this.localConfigureAuthenticationBldr.authenticationEventPublisher(eventPublisher);
189
+ this.authenticationBuilder.parentAuthenticationManager(authenticationManager);
190
+ Map<Class<?>, Object> sharedObjects = createSharedObjects();
191
+ this.http = new HttpSecurity(this.objectPostProcessor, this.authenticationBuilder, sharedObjects);
192
+ if (!this.disableDefaults) {
193
+ applyDefaultConfiguration(this.http);
194
+ ClassLoader classLoader = this.context.getClassLoader();
195
+ List<AbstractHttpConfigurer> defaultHttpConfigurers = SpringFactoriesLoader
196
+ .loadFactories(AbstractHttpConfigurer.class, classLoader);
197
+ for (AbstractHttpConfigurer configurer : defaultHttpConfigurers) {
198
+ this.http.apply(configurer);
199
+ }
200
+ }
201
+ configure(this.http);
202
+ return this.http;
203
+ }
204
+
90
205
206
+ ///@Override
207
+ public void init(WebSecurity web) throws Exception {
208
+ HttpSecurity http = getHttp();
209
+ web.addSecurityFilterChainBuilder(http).postBuildAction(() -> {
210
+ FilterSecurityInterceptor securityInterceptor = http.getSharedObject(FilterSecurityInterceptor.class);
211
+ web.securityInterceptor(securityInterceptor);
212
+ });
91
213
}
92
214
93
- @ Override
215
+
216
+ //@Override
94
217
public void configure(WebSecurity builder) throws Exception {
95
218
96
219
}
220
+ */
221
+
222
+ /*
223
+ public void configure(HttpSecurity builder) throws Exception {
97
224
98
- @ Bean
99
- public SecurityFilterChain securityFilterChain (HttpSecurity http ) throws Exception {
100
225
http
101
- .headers ()
102
- .disable ()
103
- .authorizeRequests ()
104
- .antMatchers (
105
- simpleworklistProperties .getWebSecurity ().getAntPatternsPublic ()
226
+ .headers((headers) -> headers.disable() )
227
+ .authorizeRequests((authorizeRequests) -> authorizeRequests
228
+ .antMatchers(
229
+ simpleworklistProperties.getWebSecurity().getAntPatternsPublic()
230
+ )
231
+ .permitAll()
232
+ .anyRequest()
233
+ .fullyAuthenticated()
106
234
)
107
- .permitAll ()
108
- .anyRequest ()
109
- .fullyAuthenticated ()
235
+ .csrf()
110
236
.and()
237
+ .formLogin((formLogin) -> formLogin
238
+ .loginPage(simpleworklistProperties.getWebSecurity().getLoginPage())
239
+ .usernameParameter(simpleworklistProperties.getWebSecurity().getUsernameParameter())
240
+ .passwordParameter(simpleworklistProperties.getWebSecurity().getPasswordParameter())
241
+ .loginProcessingUrl(simpleworklistProperties.getWebSecurity().getLoginProcessingUrl())
242
+ .failureForwardUrl(simpleworklistProperties.getWebSecurity().getFailureForwardUrl())
243
+ .defaultSuccessUrl(simpleworklistProperties.getWebSecurity().getDefaultSuccessUrl())
244
+ //.successHandler(authenticationSuccessHandler)
245
+ .permitAll()
246
+ )
111
247
.csrf()
112
248
.and()
113
- .formLogin ()
114
- .loginPage (simpleworklistProperties .getWebSecurity ().getLoginPage ())
115
- .usernameParameter (simpleworklistProperties .getWebSecurity ().getUsernameParameter ())
116
- .passwordParameter (simpleworklistProperties .getWebSecurity ().getPasswordParameter ())
117
- .loginProcessingUrl (simpleworklistProperties .getWebSecurity ().getLoginProcessingUrl ())
118
- .failureForwardUrl (simpleworklistProperties .getWebSecurity ().getFailureForwardUrl ())
119
- .defaultSuccessUrl (simpleworklistProperties .getWebSecurity ().getDefaultSuccessUrl ())
120
- //.successHandler(authenticationSuccessHandler)
121
- .permitAll ()
249
+ .logout((logout)-> logout
250
+ .logoutUrl(simpleworklistProperties.getWebSecurity().getLogoutUrl())
251
+ .deleteCookies(simpleworklistProperties.getWebSecurity().getCookieNamesToClear())
252
+ .invalidateHttpSession(simpleworklistProperties.getWebSecurity().getInvalidateHttpSession())
253
+ .permitAll()
254
+ );
255
+
256
+ }
257
+ */
258
+ @ Bean
259
+ public DaoAuthenticationProvider authenticationProvider (){
260
+ DaoAuthenticationProvider d = new DaoAuthenticationProvider ();
261
+ d .setPasswordEncoder (encoder ());
262
+ d .setUserDetailsService (userDetailsService ());
263
+ return d ;
264
+ }
265
+
266
+ @ Bean
267
+ public SecurityFilterChain securityFilterChain (HttpSecurity http ) throws Exception {
268
+ http
269
+ .headers ((headers ) -> headers .disable () )
270
+ .authorizeRequests ((authorizeRequests ) -> authorizeRequests
271
+ .antMatchers (
272
+ simpleworklistProperties .getWebSecurity ().getAntPatternsPublic ()
273
+ )
274
+ .permitAll ()
275
+ .anyRequest ()
276
+ .fullyAuthenticated ()
277
+ )
278
+ .csrf ()
122
279
.and ()
280
+ .formLogin ((formLogin ) -> formLogin
281
+ .loginPage (simpleworklistProperties .getWebSecurity ().getLoginPage ())
282
+ .usernameParameter (simpleworklistProperties .getWebSecurity ().getUsernameParameter ())
283
+ .passwordParameter (simpleworklistProperties .getWebSecurity ().getPasswordParameter ())
284
+ .loginProcessingUrl (simpleworklistProperties .getWebSecurity ().getLoginProcessingUrl ())
285
+ .failureForwardUrl (simpleworklistProperties .getWebSecurity ().getFailureForwardUrl ())
286
+ .defaultSuccessUrl (simpleworklistProperties .getWebSecurity ().getDefaultSuccessUrl ())
287
+ //.successHandler(authenticationSuccessHandler)
288
+ .permitAll ()
289
+ )
123
290
.csrf ()
124
291
.and ()
125
- .logout ()
126
- .logoutUrl (simpleworklistProperties .getWebSecurity ().getLogoutUrl ())
127
- .deleteCookies (simpleworklistProperties .getWebSecurity ().getCookieNamesToClear ())
128
- .invalidateHttpSession (simpleworklistProperties .getWebSecurity ().getInvalidateHttpSession ())
129
- .permitAll ();
292
+ .logout ((logout )-> logout
293
+ .logoutUrl (simpleworklistProperties .getWebSecurity ().getLogoutUrl ())
294
+ .deleteCookies (simpleworklistProperties .getWebSecurity ().getCookieNamesToClear ())
295
+ .invalidateHttpSession (simpleworklistProperties .getWebSecurity ().getInvalidateHttpSession ())
296
+ .permitAll ()
297
+ );
130
298
return http .build ();
131
299
}
132
300
0 commit comments