1
1
package com .bezkoder .springjwt .security ;
2
2
3
+ import com .bezkoder .springjwt .security .jwt .AuthEntryPointJwt ;
4
+ import com .bezkoder .springjwt .security .jwt .AuthTokenFilter ;
5
+ import com .bezkoder .springjwt .security .services .UserDetailsServiceImpl ;
3
6
import org .springframework .beans .factory .annotation .Autowired ;
4
7
import org .springframework .context .annotation .Bean ;
5
8
import org .springframework .context .annotation .Configuration ;
6
9
import org .springframework .security .authentication .AuthenticationManager ;
7
10
import org .springframework .security .authentication .dao .DaoAuthenticationProvider ;
8
- //import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
9
11
import org .springframework .security .config .annotation .authentication .configuration .AuthenticationConfiguration ;
10
12
import org .springframework .security .config .annotation .method .configuration .EnableGlobalMethodSecurity ;
11
13
import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
12
- //import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
13
- //import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
14
14
import org .springframework .security .config .http .SessionCreationPolicy ;
15
15
import org .springframework .security .crypto .bcrypt .BCryptPasswordEncoder ;
16
16
import org .springframework .security .crypto .password .PasswordEncoder ;
17
17
import org .springframework .security .web .SecurityFilterChain ;
18
18
import org .springframework .security .web .authentication .UsernamePasswordAuthenticationFilter ;
19
19
20
- import com .bezkoder .springjwt .security .jwt .AuthEntryPointJwt ;
21
- import com .bezkoder .springjwt .security .jwt .AuthTokenFilter ;
22
- import com .bezkoder .springjwt .security .services .UserDetailsServiceImpl ;
23
-
20
+ // ERG BELANGRIJK -- sinds Spring Boot 2.7 is de inrichting van Security anders geworden.
21
+ // Wat is gecommentarieerd ... was < 2.7.
24
22
@ Configuration
25
23
@ EnableGlobalMethodSecurity (
26
- // securedEnabled = true,
27
- // jsr250Enabled = true,
28
- prePostEnabled = true )
24
+ // securedEnabled = true, was < SB 2.7
25
+ // jsr250Enabled = true, was < SB 2.7
26
+ prePostEnabled = true )
29
27
public class WebSecurityConfig { // extends WebSecurityConfigurerAdapter {
30
28
@ Autowired
31
29
UserDetailsServiceImpl userDetailsService ;
@@ -42,23 +40,21 @@ public AuthTokenFilter authenticationJwtTokenFilter() {
42
40
// public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
43
41
// authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
44
42
// }
45
-
43
+
46
44
@ Bean
47
45
public DaoAuthenticationProvider authenticationProvider () {
48
- DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider ();
49
-
50
- authProvider .setUserDetailsService (userDetailsService );
51
- authProvider .setPasswordEncoder (passwordEncoder ());
52
-
53
- return authProvider ;
46
+ DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider ();
47
+ authProvider .setUserDetailsService (userDetailsService );
48
+ authProvider .setPasswordEncoder (passwordEncoder ());
49
+ return authProvider ;
54
50
}
55
51
56
52
// @Bean
57
53
// @Override
58
54
// public AuthenticationManager authenticationManagerBean() throws Exception {
59
55
// return super.authenticationManagerBean();
60
56
// }
61
-
57
+
62
58
@ Bean
63
59
public AuthenticationManager authenticationManager (AuthenticationConfiguration authConfig ) throws Exception {
64
60
return authConfig .getAuthenticationManager ();
@@ -80,20 +76,23 @@ public PasswordEncoder passwordEncoder() {
80
76
//
81
77
// http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
82
78
// }
83
-
79
+
84
80
@ Bean
85
81
public SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
86
82
http .cors ().and ().csrf ().disable ()
87
- .exceptionHandling ().authenticationEntryPoint (unauthorizedHandler ).and ()
88
- .sessionManagement ().sessionCreationPolicy (SessionCreationPolicy .STATELESS ).and ()
89
- .authorizeRequests ().antMatchers ("/api/auth/**" ).permitAll ()
90
- .antMatchers ("/api/test/**" ).permitAll ()
91
- .anyRequest ().authenticated ();
92
-
83
+ .exceptionHandling ().authenticationEntryPoint (unauthorizedHandler ).and ()
84
+ .sessionManagement ().sessionCreationPolicy (SessionCreationPolicy .STATELESS ).and ()
85
+ .authorizeRequests ().antMatchers ("/api/auth/**" ).permitAll ()
86
+ .antMatchers ("/api/test/**" ).permitAll ()
87
+ .antMatchers ("/api/test2/user" ).hasAnyRole ("USER" , "MODERATOR" , "ADMIN" )
88
+ .antMatchers ("/api/test2/mod" ).hasAnyRole ("MODERATOR" )
89
+ .antMatchers ("/api/test2/admin" ).hasRole ("ADMIN" )
90
+ .anyRequest ().authenticated ();
91
+
93
92
http .authenticationProvider (authenticationProvider ());
94
93
95
94
http .addFilterBefore (authenticationJwtTokenFilter (), UsernamePasswordAuthenticationFilter .class );
96
-
95
+
97
96
return http .build ();
98
97
}
99
98
}
0 commit comments