Skip to content

Commit 20c3567

Browse files
committed
update for new Spring Boot version
1 parent 0aeeee8 commit 20c3567

File tree

5 files changed

+154
-87
lines changed

5 files changed

+154
-87
lines changed

README.md

+89-58
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,63 @@ You can have an overview of our Spring Boot Server with the diagram below:
1919

2020
![spring-boot-jwt-authentication-spring-security-architecture](spring-boot-jwt-authentication-spring-security-architecture.png)
2121

22+
## Configure Spring Datasource, JPA, App properties
23+
Open `src/main/resources/application.properties`
24+
25+
```properties
26+
spring.datasource.url= jdbc:mysql://localhost:3306/testdb?useSSL=false
27+
spring.datasource.username= root
28+
spring.datasource.password= 123456
29+
30+
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5InnoDBDialect
31+
spring.jpa.hibernate.ddl-auto= update
32+
33+
# App Properties
34+
bezkoder.app.jwtSecret= bezKoderSecretKey
35+
bezkoder.app.jwtExpirationMs= 3600000
36+
bezkoder.app.jwtRefreshExpirationMs= 86400000
37+
```
38+
39+
## Run Spring Boot application
40+
```
41+
mvn spring-boot:run
42+
```
43+
44+
## Run following SQL insert statements
45+
```
46+
INSERT INTO roles(name) VALUES('ROLE_USER');
47+
INSERT INTO roles(name) VALUES('ROLE_MODERATOR');
48+
INSERT INTO roles(name) VALUES('ROLE_ADMIN');
49+
```
50+
2251
Related Posts:
2352
> [Spring Boot, Spring Security, MySQL: JWT Authentication & Authorization example](https://bezkoder.com/spring-boot-jwt-authentication/)
2453
2554
> [For PostgreSQL](https://bezkoder.com/spring-boot-security-postgresql-jwt-authentication/)
2655
2756
> [For MongoDB](https://bezkoder.com/spring-boot-jwt-auth-mongodb/)
2857
58+
## More Practice:
59+
> [Spring Boot File upload example with Multipart File](https://bezkoder.com/spring-boot-file-upload/)
60+
61+
> [Exception handling: @RestControllerAdvice example in Spring Boot](https://bezkoder.com/spring-boot-restcontrolleradvice/)
62+
63+
> [Spring Boot Repository Unit Test with @DataJpaTest](https://bezkoder.com/spring-boot-unit-test-jpa-repo-datajpatest/)
64+
65+
> [Spring Boot Pagination & Sorting example](https://www.bezkoder.com/spring-boot-pagination-sorting-example/)
66+
67+
Associations:
68+
> [Spring Boot One To Many example with Spring JPA, Hibernate](https://www.bezkoder.com/jpa-one-to-many/)
69+
70+
> [Spring Boot Many To Many example with Spring JPA, Hibernate](https://www.bezkoder.com/jpa-many-to-many/)
71+
72+
> [JPA One To One example with Spring Boot](https://www.bezkoder.com/jpa-one-to-one/)
73+
74+
Deployment:
75+
> [Deploy Spring Boot App on AWS – Elastic Beanstalk](https://www.bezkoder.com/deploy-spring-boot-aws-eb/)
76+
77+
> [Docker Compose Spring Boot and MySQL example](https://www.bezkoder.com/docker-compose-spring-boot-mysql/)
78+
2979
## Fullstack Authentication
3080

3181
> [Spring Boot + Vue.js JWT Authentication](https://bezkoder.com/spring-boot-vue-js-authentication-jwt-spring-security/)
@@ -36,11 +86,23 @@ Related Posts:
3686
3787
> [Spring Boot + Angular 11 JWT Authentication](https://bezkoder.com/angular-11-spring-boot-jwt-auth/)
3888
89+
> [Spring Boot + Angular 12 JWT Authentication](https://www.bezkoder.com/angular-12-spring-boot-jwt-auth/)
90+
91+
> [Spring Boot + Angular 13 JWT Authentication](https://www.bezkoder.com/angular-13-spring-boot-jwt-auth/)
92+
93+
> [Spring Boot + Angular 14 JWT Authentication](https://www.bezkoder.com/angular-14-spring-boot-jwt-auth/)
94+
3995
> [Spring Boot + React JWT Authentication](https://bezkoder.com/spring-boot-react-jwt-auth/)
4096
4197
## Fullstack CRUD App
4298

43-
> [Vue.js + Spring Boot + MySQL/PostgreSQL example](https://bezkoder.com/spring-boot-vue-js-crud-example/)
99+
> [Vue.js + Spring Boot + H2 Embedded database example](https://www.bezkoder.com/spring-boot-vue-js-crud-example/)
100+
101+
> [Vue.js + Spring Boot + MySQL example](https://www.bezkoder.com/spring-boot-vue-js-mysql/)
102+
103+
> [Vue.js + Spring Boot + PostgreSQL example](https://www.bezkoder.com/spring-boot-vue-js-postgresql/)
104+
105+
> [Angular 8 + Spring Boot + Embedded database example](https://www.bezkoder.com/angular-spring-boot-crud/)
44106
45107
> [Angular 8 + Spring Boot + MySQL example](https://bezkoder.com/angular-spring-boot-crud/)
46108
@@ -54,6 +116,24 @@ Related Posts:
54116
55117
> [Angular 11 + Spring Boot + PostgreSQL example](https://bezkoder.com/angular-11-spring-boot-postgresql/)
56118
119+
> [Angular 12 + Spring Boot + Embedded database example](https://www.bezkoder.com/angular-12-spring-boot-crud/)
120+
121+
> [Angular 12 + Spring Boot + MySQL example](https://www.bezkoder.com/angular-12-spring-boot-mysql/)
122+
123+
> [Angular 12 + Spring Boot + PostgreSQL example](https://www.bezkoder.com/angular-12-spring-boot-postgresql/)
124+
125+
> [Angular 13 + Spring Boot + H2 Embedded Database example](https://www.bezkoder.com/spring-boot-angular-13-crud/)
126+
127+
> [Angular 13 + Spring Boot + MySQL example](https://www.bezkoder.com/spring-boot-angular-13-mysql/)
128+
129+
> [Angular 13 + Spring Boot + PostgreSQL example](https://www.bezkoder.com/spring-boot-angular-13-postgresql/)
130+
131+
> [Angular 14 + Spring Boot + H2 Embedded Database example](https://www.bezkoder.com/spring-boot-angular-14-crud/)
132+
133+
> [Angular 14 + Spring Boot + MySQL example](https://www.bezkoder.com/spring-boot-angular-14-mysql/)
134+
135+
> [Angular 14 + Spring Boot + PostgreSQL example](https://www.bezkoder.com/spring-boot-angular-14-postgresql/)
136+
57137
> [React + Spring Boot + MySQL example](https://bezkoder.com/react-spring-boot-crud/)
58138
59139
> [React + Spring Boot + PostgreSQL example](https://bezkoder.com/spring-boot-react-postgresql/)
@@ -74,65 +154,16 @@ Run both Back-end & Front-end in one place:
74154
75155
> [Spring Boot Repository Unit Test with @DataJpaTest](https://bezkoder.com/spring-boot-unit-test-jpa-repo-datajpatest/)
76156
77-
> [Deploy Spring Boot App on AWS – Elastic Beanstalk](https://bezkoder.com/deploy-spring-boot-aws-eb/)
78-
79-
> [Secure Spring Boot App with Spring Security & JWT Authentication](https://bezkoder.com/spring-boot-jwt-authentication/)
80-
81-
## Dependency
82-
```xml
83-
<dependency>
84-
<groupId>org.springframework.boot</groupId>
85-
<artifactId>spring-boot-starter-data-jpa</artifactId>
86-
</dependency>
87-
88-
<dependency>
89-
<groupId>org.springframework.boot</groupId>
90-
<artifactId>spring-boot-starter-security</artifactId>
91-
</dependency>
92-
93-
<dependency>
94-
<groupId>org.springframework.boot</groupId>
95-
<artifactId>spring-boot-starter-web</artifactId>
96-
</dependency>
97-
98-
<dependency>
99-
<groupId>mysql</groupId>
100-
<artifactId>mysql-connector-java</artifactId>
101-
<scope>runtime</scope>
102-
</dependency>
103-
104-
<dependency>
105-
<groupId>io.jsonwebtoken</groupId>
106-
<artifactId>jjwt</artifactId>
107-
<version>0.9.1</version>
108-
</dependency>
109-
```
157+
> [Spring Boot Pagination & Sorting example](https://www.bezkoder.com/spring-boot-pagination-sorting-example/)
110158
111-
## Configure Spring Datasource, JPA, App properties
112-
Open `src/main/resources/application.properties`
159+
Associations:
160+
> [Spring Boot One To Many example with Spring JPA, Hibernate](https://www.bezkoder.com/jpa-one-to-many/)
113161
114-
```properties
115-
spring.datasource.url= jdbc:mysql://localhost:3306/testdb?useSSL=false
116-
spring.datasource.username= root
117-
spring.datasource.password= 123456
118-
119-
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5InnoDBDialect
120-
spring.jpa.hibernate.ddl-auto= update
162+
> [Spring Boot Many To Many example with Spring JPA, Hibernate](https://www.bezkoder.com/jpa-many-to-many/)
121163
122-
# App Properties
123-
bezkoder.app.jwtSecret= bezKoderSecretKey
124-
bezkoder.app.jwtExpirationMs= 3600000
125-
bezkoder.app.jwtRefreshExpirationMs= 86400000
126-
```
164+
> [JPA One To One example with Spring Boot](https://www.bezkoder.com/jpa-one-to-one/)
127165
128-
## Run Spring Boot application
129-
```
130-
mvn spring-boot:run
131-
```
166+
Deployment:
167+
> [Deploy Spring Boot App on AWS – Elastic Beanstalk](https://www.bezkoder.com/deploy-spring-boot-aws-eb/)
132168
133-
## Run following SQL insert statements
134-
```
135-
INSERT INTO roles(name) VALUES('ROLE_USER');
136-
INSERT INTO roles(name) VALUES('ROLE_MODERATOR');
137-
INSERT INTO roles(name) VALUES('ROLE_ADMIN');
138-
```
169+
> [Docker Compose Spring Boot and MySQL example](https://www.bezkoder.com/docker-compose-spring-boot-mysql/)

pom.xml

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.springframework.boot</groupId>
77
<artifactId>spring-boot-starter-parent</artifactId>
8-
<version>2.2.11.RELEASE</version>
8+
<version>2.7.3</version>
99
<relativePath/> <!-- lookup parent from repository -->
1010
</parent>
1111
<groupId>com.bezkoder</groupId>
@@ -33,6 +33,11 @@
3333
<groupId>org.springframework.boot</groupId>
3434
<artifactId>spring-boot-starter-web</artifactId>
3535
</dependency>
36+
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-validation</artifactId>
40+
</dependency>
3641

3742
<dependency>
3843
<groupId>mysql</groupId>

src/main/java/com/bezkoder/spring/security/jwt/security/WebSecurityConfig.java

+57-23
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,30 @@
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.spring.security.jwt.security.jwt.AuthEntryPointJwt;
1821
import com.bezkoder.spring.security.jwt.security.jwt.AuthTokenFilter;
1922
import com.bezkoder.spring.security.jwt.security.services.UserDetailsServiceImpl;
2023

2124
@Configuration
22-
@EnableWebSecurity
25+
//@EnableWebSecurity
2326
@EnableGlobalMethodSecurity(
2427
// securedEnabled = true,
2528
// jsr250Enabled = true,
2629
prePostEnabled = true)
27-
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
30+
public class WebSecurityConfig { // extends WebSecurityConfigurerAdapter {
2831
@Autowired
2932
UserDetailsServiceImpl userDetailsService;
3033

@@ -36,31 +39,62 @@ public AuthTokenFilter authenticationJwtTokenFilter() {
3639
return new AuthTokenFilter();
3740
}
3841

39-
@Override
40-
public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
41-
authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
42-
}
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+
}
4356

57+
// @Bean
58+
// @Override
59+
// public AuthenticationManager authenticationManagerBean() throws Exception {
60+
// return super.authenticationManagerBean();
61+
// }
62+
4463
@Bean
45-
@Override
46-
public AuthenticationManager authenticationManagerBean() throws Exception {
47-
return super.authenticationManagerBean();
48-
}
64+
public AuthenticationManager authenticationManager(AuthenticationConfiguration authConfig) throws Exception {
65+
return authConfig.getAuthenticationManager();
66+
}
4967

5068
@Bean
5169
public PasswordEncoder passwordEncoder() {
5270
return new BCryptPasswordEncoder();
5371
}
5472

55-
@Override
56-
protected void configure(HttpSecurity http) throws Exception {
57-
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();
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());
6395

64-
http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
65-
}
96+
http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
97+
98+
return http.build();
99+
}
66100
}

src/main/resources/application.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
spring.datasource.url= jdbc:mysql://localhost:3306/testdb?useSSL=false
1+
spring.datasource.url= jdbc:mysql://localhost:3306/testdb_spring?useSSL=false
22
spring.datasource.username= root
33
spring.datasource.password= 123456
44

src/test/java/com/bezkoder/spring/security/jwt/SpringBootSecurityJwtApplicationTests.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package com.bezkoder.spring.security.jwt;
22

3-
import org.junit.Test;
4-
import org.junit.runner.RunWith;
3+
import org.junit.jupiter.api.Test;
54
import org.springframework.boot.test.context.SpringBootTest;
6-
import org.springframework.test.context.junit4.SpringRunner;
75

8-
@RunWith(SpringRunner.class)
96
@SpringBootTest
107
public class SpringBootSecurityJwtApplicationTests {
118

0 commit comments

Comments
 (0)