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