Skip to content

Commit edaf308

Browse files
committed
update WebSecurityConfig for new Spring Boot version
1 parent e037bf6 commit edaf308

File tree

3 files changed

+83
-31
lines changed

3 files changed

+83
-31
lines changed

README.md

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,27 @@ For more detail, please visit:
2121

2222
For instruction: [Spring Boot Refresh Token with JWT example](https://bezkoder.com/spring-boot-refresh-token-jwt/)
2323

24+
## More Practice:
25+
> [Spring Boot File upload example with Multipart File](https://bezkoder.com/spring-boot-file-upload/)
26+
27+
> [Exception handling: @RestControllerAdvice example in Spring Boot](https://bezkoder.com/spring-boot-restcontrolleradvice/)
28+
29+
> [Spring Boot Repository Unit Test with @DataJpaTest](https://bezkoder.com/spring-boot-unit-test-jpa-repo-datajpatest/)
30+
31+
> [Spring Boot Pagination & Sorting example](https://www.bezkoder.com/spring-boot-pagination-sorting-example/)
32+
33+
Associations:
34+
> [Spring Boot One To Many example with Spring JPA, Hibernate](https://www.bezkoder.com/jpa-one-to-many/)
35+
36+
> [Spring Boot Many To Many example with Spring JPA, Hibernate](https://www.bezkoder.com/jpa-many-to-many/)
37+
38+
> [JPA One To One example with Spring Boot](https://www.bezkoder.com/jpa-one-to-one/)
39+
40+
Deployment:
41+
> [Deploy Spring Boot App on AWS – Elastic Beanstalk](https://www.bezkoder.com/deploy-spring-boot-aws-eb/)
42+
43+
> [Docker Compose Spring Boot and MySQL example](https://www.bezkoder.com/docker-compose-spring-boot-mysql/)
44+
2445
## Fullstack Authentication
2546

2647
> [Spring Boot + Vue.js JWT Authentication](https://bezkoder.com/spring-boot-vue-js-authentication-jwt-spring-security/)
@@ -33,6 +54,10 @@ For instruction: [Spring Boot Refresh Token with JWT example](https://bezkoder.c
3354
3455
> [Spring Boot + Angular 12 JWT Authentication](https://www.bezkoder.com/angular-12-spring-boot-jwt-auth/)
3556
57+
> [Spring Boot + Angular 13 JWT Authentication](https://www.bezkoder.com/angular-13-spring-boot-jwt-auth/)
58+
59+
> [Spring Boot + Angular 14 JWT Authentication](https://www.bezkoder.com/angular-14-spring-boot-jwt-auth/)
60+
3661
> [Spring Boot + React JWT Authentication](https://bezkoder.com/spring-boot-react-jwt-auth/)
3762
3863
## Fullstack CRUD App
@@ -69,6 +94,12 @@ For instruction: [Spring Boot Refresh Token with JWT example](https://bezkoder.c
6994
7095
> [Angular 13 + Spring Boot + PostgreSQL example](https://www.bezkoder.com/spring-boot-angular-13-postgresql/)
7196
97+
> [Angular 14 + Spring Boot + H2 Embedded Database example](https://www.bezkoder.com/spring-boot-angular-14-crud/)
98+
99+
> [Angular 14 + Spring Boot + MySQL example](https://www.bezkoder.com/spring-boot-angular-14-mysql/)
100+
101+
> [Angular 14 + Spring Boot + PostgreSQL example](https://www.bezkoder.com/spring-boot-angular-14-postgresql/)
102+
72103
> [React + Spring Boot + MySQL example](https://bezkoder.com/react-spring-boot-crud/)
73104
74105
> [React + Spring Boot + PostgreSQL example](https://bezkoder.com/spring-boot-react-postgresql/)
@@ -82,18 +113,6 @@ Run both Back-end & Front-end in one place:
82113
83114
> [Integrate Vue.js with Spring Boot Rest API](https://bezkoder.com/integrate-vue-spring-boot/)
84115
85-
More Practice:
86-
> [Spring Boot File upload example with Multipart File](https://bezkoder.com/spring-boot-file-upload/)
87-
88-
> [Exception handling: @RestControllerAdvice example in Spring Boot](https://bezkoder.com/spring-boot-restcontrolleradvice/)
89-
90-
> [Spring Boot Repository Unit Test with @DataJpaTest](https://bezkoder.com/spring-boot-unit-test-jpa-repo-datajpatest/)
91-
92-
Deployment:
93-
> [Deploy Spring Boot App on AWS – Elastic Beanstalk](https://www.bezkoder.com/deploy-spring-boot-aws-eb/)
94-
95-
> [Docker Compose Spring Boot and MySQL example](https://www.bezkoder.com/docker-compose-spring-boot-mysql/)
96-
97116
## Dependency
98117
– If you want to use PostgreSQL:
99118
```xml

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>org.springframework.boot</groupId>
88
<artifactId>spring-boot-starter-parent</artifactId>
9-
<version>2.6.1</version>
9+
<version>2.7.3</version>
1010
<relativePath /> <!-- lookup parent from repository -->
1111
</parent>
1212
<groupId>com.bezkoder</groupId>

src/main/java/com/bezkoder/springjwt/security/WebSecurityConfig.java

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,29 @@
44
import org.springframework.context.annotation.Bean;
55
import org.springframework.context.annotation.Configuration;
66
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;
810
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
911
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;
1214
import org.springframework.security.config.http.SessionCreationPolicy;
1315
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
1416
import org.springframework.security.crypto.password.PasswordEncoder;
17+
import org.springframework.security.web.SecurityFilterChain;
1518
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
1619

1720
import com.bezkoder.springjwt.security.jwt.AuthEntryPointJwt;
1821
import com.bezkoder.springjwt.security.jwt.AuthTokenFilter;
1922
import com.bezkoder.springjwt.security.services.UserDetailsServiceImpl;
2023

2124
@Configuration
22-
@EnableWebSecurity
2325
@EnableGlobalMethodSecurity(
2426
// securedEnabled = true,
2527
// jsr250Enabled = true,
2628
prePostEnabled = true)
27-
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
29+
public class WebSecurityConfig { // extends WebSecurityConfigurerAdapter {
2830
@Autowired
2931
UserDetailsServiceImpl userDetailsService;
3032

@@ -36,31 +38,62 @@ public AuthTokenFilter authenticationJwtTokenFilter() {
3638
return new AuthTokenFilter();
3739
}
3840

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;
4254
}
4355

56+
// @Bean
57+
// @Override
58+
// public AuthenticationManager authenticationManagerBean() throws Exception {
59+
// return super.authenticationManagerBean();
60+
// }
61+
4462
@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();
4865
}
4966

5067
@Bean
5168
public PasswordEncoder passwordEncoder() {
5269
return new BCryptPasswordEncoder();
5370
}
5471

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 {
5786
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());
6394

6495
http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
96+
97+
return http.build();
6598
}
6699
}

0 commit comments

Comments
 (0)