|
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; |
6 |
| -import lombok.RequiredArgsConstructor; |
| 3 | +import org.springframework.beans.factory.annotation.Autowired; |
7 | 4 | import org.springframework.context.annotation.Bean;
|
8 | 5 | import org.springframework.context.annotation.Configuration;
|
9 | 6 | import org.springframework.security.authentication.AuthenticationManager;
|
10 |
| -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; |
11 | 10 | import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
12 | 11 | 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; |
| 12 | +//import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 13 | +//import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
15 | 14 | import org.springframework.security.config.http.SessionCreationPolicy;
|
16 | 15 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
17 | 16 | import org.springframework.security.crypto.password.PasswordEncoder;
|
| 17 | +import org.springframework.security.web.SecurityFilterChain; |
18 | 18 | import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
19 | 19 |
|
20 |
| -@RequiredArgsConstructor |
| 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 | + |
21 | 24 | @Configuration
|
22 |
| -@EnableWebSecurity |
23 | 25 | @EnableGlobalMethodSecurity(
|
24 |
| - // securedEnabled = true, |
25 |
| - // jsr250Enabled = true, |
26 |
| - prePostEnabled = true) |
27 |
| -public class WebSecurityConfig extends WebSecurityConfigurerAdapter { |
28 |
| - |
29 |
| - private final UserDetailsServiceImpl userDetailsService; |
| 26 | + // securedEnabled = true, |
| 27 | + // jsr250Enabled = true, |
| 28 | + prePostEnabled = true) |
| 29 | +public class WebSecurityConfig { // extends WebSecurityConfigurerAdapter { |
| 30 | + @Autowired |
| 31 | + UserDetailsServiceImpl userDetailsService; |
30 | 32 |
|
31 |
| - private final AuthEntryPointJwt unauthorizedHandler; |
| 33 | + @Autowired |
| 34 | + private AuthEntryPointJwt unauthorizedHandler; |
32 | 35 |
|
33 |
| - @Bean |
34 |
| - public AuthTokenFilter authenticationJwtTokenFilter() { |
35 |
| - return new AuthTokenFilter(); |
36 |
| - } |
| 36 | + @Bean |
| 37 | + public AuthTokenFilter authenticationJwtTokenFilter() { |
| 38 | + return new AuthTokenFilter(); |
| 39 | + } |
37 | 40 |
|
38 |
| - @Override |
39 |
| - public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception { |
40 |
| - authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); |
41 |
| - } |
| 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; |
| 54 | + } |
42 | 55 |
|
43 |
| - @Bean |
44 |
| - @Override |
45 |
| - public AuthenticationManager authenticationManagerBean() throws Exception { |
46 |
| - return super.authenticationManagerBean(); |
47 |
| - } |
| 56 | +// @Bean |
| 57 | +// @Override |
| 58 | +// public AuthenticationManager authenticationManagerBean() throws Exception { |
| 59 | +// return super.authenticationManagerBean(); |
| 60 | +// } |
| 61 | + |
| 62 | + @Bean |
| 63 | + public AuthenticationManager authenticationManager(AuthenticationConfiguration authConfig) throws Exception { |
| 64 | + return authConfig.getAuthenticationManager(); |
| 65 | + } |
48 | 66 |
|
49 |
| - @Bean |
50 |
| - public PasswordEncoder passwordEncoder() { |
51 |
| - return new BCryptPasswordEncoder(); |
52 |
| - } |
| 67 | + @Bean |
| 68 | + public PasswordEncoder passwordEncoder() { |
| 69 | + return new BCryptPasswordEncoder(); |
| 70 | + } |
53 | 71 |
|
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(); |
| 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 { |
| 86 | + 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 | + |
| 93 | + http.authenticationProvider(authenticationProvider()); |
62 | 94 |
|
63 |
| - http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class); |
64 |
| - } |
| 95 | + http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class); |
| 96 | + |
| 97 | + return http.build(); |
| 98 | + } |
65 | 99 | }
|
0 commit comments