4
4
import org .springframework .context .annotation .Bean ;
5
5
import org .springframework .context .annotation .Configuration ;
6
6
import org .springframework .security .authentication .AuthenticationManager ;
7
- import org .springframework .security .config .annotation .authentication .builders .AuthenticationManagerBuilder ;
7
+ import org .springframework .security .authentication .dao .DaoAuthenticationProvider ;
8
+ //import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
9
+ import org .springframework .security .config .annotation .authentication .configuration .AuthenticationConfiguration ;
8
10
import org .springframework .security .config .annotation .method .configuration .EnableGlobalMethodSecurity ;
9
11
import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
10
- import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
11
- import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
12
+ // import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
13
+ // import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
12
14
import org .springframework .security .config .http .SessionCreationPolicy ;
13
15
import org .springframework .security .crypto .bcrypt .BCryptPasswordEncoder ;
14
16
import org .springframework .security .crypto .password .PasswordEncoder ;
17
+ import org .springframework .security .web .SecurityFilterChain ;
15
18
import org .springframework .security .web .authentication .UsernamePasswordAuthenticationFilter ;
16
19
17
20
import com .bezkoder .springjwt .security .jwt .AuthEntryPointJwt ;
18
21
import com .bezkoder .springjwt .security .jwt .AuthTokenFilter ;
19
22
import com .bezkoder .springjwt .security .services .UserDetailsServiceImpl ;
20
23
21
24
@ Configuration
22
- @ EnableWebSecurity
23
25
@ EnableGlobalMethodSecurity (
24
26
// securedEnabled = true,
25
27
// jsr250Enabled = true,
26
28
prePostEnabled = true )
27
- public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
29
+ public class WebSecurityConfig { // extends WebSecurityConfigurerAdapter {
28
30
@ Autowired
29
31
UserDetailsServiceImpl userDetailsService ;
30
32
@@ -36,31 +38,62 @@ public AuthTokenFilter authenticationJwtTokenFilter() {
36
38
return new AuthTokenFilter ();
37
39
}
38
40
39
- @ Override
40
- public void configure (AuthenticationManagerBuilder authenticationManagerBuilder ) throws Exception {
41
- authenticationManagerBuilder .userDetailsService (userDetailsService ).passwordEncoder (passwordEncoder ());
41
+ // @Override
42
+ // public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
43
+ // authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
44
+ // }
45
+
46
+ @ Bean
47
+ public DaoAuthenticationProvider authenticationProvider () {
48
+ DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider ();
49
+
50
+ authProvider .setUserDetailsService (userDetailsService );
51
+ authProvider .setPasswordEncoder (passwordEncoder ());
52
+
53
+ return authProvider ;
42
54
}
43
55
56
+ // @Bean
57
+ // @Override
58
+ // public AuthenticationManager authenticationManagerBean() throws Exception {
59
+ // return super.authenticationManagerBean();
60
+ // }
61
+
44
62
@ Bean
45
- @ Override
46
- public AuthenticationManager authenticationManagerBean () throws Exception {
47
- return super .authenticationManagerBean ();
63
+ public AuthenticationManager authenticationManager (AuthenticationConfiguration authConfig ) throws Exception {
64
+ return authConfig .getAuthenticationManager ();
48
65
}
49
66
50
67
@ Bean
51
68
public PasswordEncoder passwordEncoder () {
52
69
return new BCryptPasswordEncoder ();
53
70
}
54
71
55
- @ Override
56
- protected void configure (HttpSecurity http ) throws Exception {
72
+ // @Override
73
+ // protected void configure(HttpSecurity http) throws Exception {
74
+ // http.cors().and().csrf().disable()
75
+ // .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
76
+ // .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
77
+ // .authorizeRequests().antMatchers("/api/auth/**").permitAll()
78
+ // .antMatchers("/api/test/**").permitAll()
79
+ // .anyRequest().authenticated();
80
+ //
81
+ // http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
82
+ // }
83
+
84
+ @ Bean
85
+ public SecurityFilterChain filterChain (HttpSecurity http ) throws Exception {
57
86
http .cors ().and ().csrf ().disable ()
58
- .exceptionHandling ().authenticationEntryPoint (unauthorizedHandler ).and ()
59
- .sessionManagement ().sessionCreationPolicy (SessionCreationPolicy .STATELESS ).and ()
60
- .authorizeRequests ().antMatchers ("/api/auth/**" ).permitAll ()
61
- .antMatchers ("/api/test/**" ).permitAll ()
62
- .anyRequest ().authenticated ();
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
+
93
+ http .authenticationProvider (authenticationProvider ());
63
94
64
95
http .addFilterBefore (authenticationJwtTokenFilter (), UsernamePasswordAuthenticationFilter .class );
96
+
97
+ return http .build ();
65
98
}
66
99
}
0 commit comments