Skip to content

Commit 5985ea8

Browse files
committed
update to Spring Boot 3
1 parent edaf308 commit 5985ea8

File tree

12 files changed

+155
-109
lines changed

12 files changed

+155
-109
lines changed

README.md

+85-61
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,68 @@ You can have an overview of our Spring Boot Server with the diagram below:
1010

1111
![spring-boot-jwt-authentication-spring-security-architecture](spring-boot-jwt-authentication-spring-security-architecture.png)
1212

13+
## Dependency
14+
– If you want to use PostgreSQL:
15+
```xml
16+
<dependency>
17+
<groupId>org.postgresql</groupId>
18+
<artifactId>postgresql</artifactId>
19+
<scope>runtime</scope>
20+
</dependency>
21+
```
22+
– or MySQL:
23+
```xml
24+
<dependency>
25+
<groupId>com.mysql</groupId>
26+
<artifactId>mysql-connector-j</artifactId>
27+
<scope>runtime</scope>
28+
</dependency>
29+
```
30+
## Configure Spring Datasource, JPA, App properties
31+
Open `src/main/resources/application.properties`
32+
- For PostgreSQL:
33+
```
34+
spring.datasource.url= jdbc:postgresql://localhost:5432/testdb
35+
spring.datasource.username= postgres
36+
spring.datasource.password= 123
37+
38+
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation= true
39+
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.PostgreSQLDialect
40+
41+
# Hibernate ddl auto (create, create-drop, validate, update)
42+
spring.jpa.hibernate.ddl-auto= update
43+
44+
# App Properties
45+
bezkoder.app.jwtSecret= bezKoderSecretKey
46+
bezkoder.app.jwtExpirationMs= 86400000
47+
```
48+
- For MySQL
49+
```
50+
spring.datasource.url=jdbc:mysql://localhost:3306/testdb_spring?useSSL=false
51+
spring.datasource.username=root
52+
spring.datasource.password=123456
53+
54+
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
55+
spring.jpa.hibernate.ddl-auto=update
56+
57+
# App Properties
58+
bezkoder.app.jwtSecret= ======================BezKoder=Spring===========================
59+
bezkoder.app.jwtExpirationMs=86400000
60+
```
61+
## Run Spring Boot application
62+
```
63+
mvn spring-boot:run
64+
```
65+
66+
## Run following SQL insert statements
67+
```
68+
INSERT INTO roles(name) VALUES('ROLE_USER');
69+
INSERT INTO roles(name) VALUES('ROLE_MODERATOR');
70+
INSERT INTO roles(name) VALUES('ROLE_ADMIN');
71+
```
72+
1373
For more detail, please visit:
14-
> [Secure Spring Boot App with Spring Security & JWT Authentication](https://bezkoder.com/spring-boot-jwt-authentication/)
74+
> [Secure Spring Boot with Spring Security & JWT Authentication](https://bezkoder.com/spring-boot-jwt-authentication/)
1575
1676
> [For MongoDB](https://bezkoder.com/spring-boot-jwt-auth-mongodb/)
1777
@@ -22,6 +82,8 @@ For more detail, please visit:
2282
For instruction: [Spring Boot Refresh Token with JWT example](https://bezkoder.com/spring-boot-refresh-token-jwt/)
2383

2484
## More Practice:
85+
> [Spring Boot JWT Authentication example using HttpOnly Cookie](https://www.bezkoder.com/spring-boot-login-example-mysql/)
86+
2587
> [Spring Boot File upload example with Multipart File](https://bezkoder.com/spring-boot-file-upload/)
2688
2789
> [Exception handling: @RestControllerAdvice example in Spring Boot](https://bezkoder.com/spring-boot-restcontrolleradvice/)
@@ -30,6 +92,12 @@ For instruction: [Spring Boot Refresh Token with JWT example](https://bezkoder.c
3092
3193
> [Spring Boot Pagination & Sorting example](https://www.bezkoder.com/spring-boot-pagination-sorting-example/)
3294
95+
> Validation: [Spring Boot Validate Request Body](https://www.bezkoder.com/spring-boot-validate-request-body/)
96+
97+
> Documentation: [Spring Boot and Swagger 3 example](https://www.bezkoder.com/spring-boot-swagger-3/)
98+
99+
> Caching: [Spring Boot Redis Cache example](https://www.bezkoder.com/spring-boot-redis-cache-example/)
100+
33101
Associations:
34102
> [Spring Boot One To Many example with Spring JPA, Hibernate](https://www.bezkoder.com/jpa-one-to-many/)
35103
@@ -58,6 +126,10 @@ Deployment:
58126
59127
> [Spring Boot + Angular 14 JWT Authentication](https://www.bezkoder.com/angular-14-spring-boot-jwt-auth/)
60128
129+
> [Spring Boot + Angular 15 JWT Authentication](https://www.bezkoder.com/angular-15-spring-boot-jwt-auth/)
130+
131+
> [Spring Boot + Angular 16 JWT Authentication](https://www.bezkoder.com/angular-16-spring-boot-jwt-auth/)
132+
61133
> [Spring Boot + React JWT Authentication](https://bezkoder.com/spring-boot-react-jwt-auth/)
62134
63135
## Fullstack CRUD App
@@ -100,6 +172,18 @@ Deployment:
100172
101173
> [Angular 14 + Spring Boot + PostgreSQL example](https://www.bezkoder.com/spring-boot-angular-14-postgresql/)
102174
175+
> [Angular 15 + Spring Boot + H2 Embedded Database example](https://www.bezkoder.com/spring-boot-angular-15-crud/)
176+
177+
> [Angular 15 + Spring Boot + MySQL example](https://www.bezkoder.com/spring-boot-angular-15-mysql/)
178+
179+
> [Angular 15 + Spring Boot + PostgreSQL example](https://www.bezkoder.com/spring-boot-angular-15-postgresql/)
180+
181+
> [Angular 16 + Spring Boot + H2 Embedded Database example](https://www.bezkoder.com/spring-boot-angular-16-crud/)
182+
183+
> [Angular 16 + Spring Boot + MySQL example](https://www.bezkoder.com/spring-boot-angular-16-mysql/)
184+
185+
> [Angular 16 + Spring Boot + PostgreSQL example](https://www.bezkoder.com/spring-boot-angular-16-postgresql/)
186+
103187
> [React + Spring Boot + MySQL example](https://bezkoder.com/react-spring-boot-crud/)
104188
105189
> [React + Spring Boot + PostgreSQL example](https://bezkoder.com/spring-boot-react-postgresql/)
@@ -112,63 +196,3 @@ Run both Back-end & Front-end in one place:
112196
> [Integrate React.js with Spring Boot Rest API](https://bezkoder.com/integrate-reactjs-spring-boot/)
113197
114198
> [Integrate Vue.js with Spring Boot Rest API](https://bezkoder.com/integrate-vue-spring-boot/)
115-
116-
## Dependency
117-
– If you want to use PostgreSQL:
118-
```xml
119-
<dependency>
120-
<groupId>org.postgresql</groupId>
121-
<artifactId>postgresql</artifactId>
122-
<scope>runtime</scope>
123-
</dependency>
124-
```
125-
– or MySQL:
126-
```xml
127-
<dependency>
128-
<groupId>mysql</groupId>
129-
<artifactId>mysql-connector-java</artifactId>
130-
<scope>runtime</scope>
131-
</dependency>
132-
```
133-
## Configure Spring Datasource, JPA, App properties
134-
Open `src/main/resources/application.properties`
135-
- For PostgreSQL:
136-
```
137-
spring.datasource.url= jdbc:postgresql://localhost:5432/testdb
138-
spring.datasource.username= postgres
139-
spring.datasource.password= 123
140-
141-
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation= true
142-
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.PostgreSQLDialect
143-
144-
# Hibernate ddl auto (create, create-drop, validate, update)
145-
spring.jpa.hibernate.ddl-auto= update
146-
147-
# App Properties
148-
bezkoder.app.jwtSecret= bezKoderSecretKey
149-
bezkoder.app.jwtExpirationMs= 86400000
150-
```
151-
- For MySQL
152-
```
153-
spring.datasource.url= jdbc:mysql://localhost:3306/testdb?useSSL=false
154-
spring.datasource.username= root
155-
spring.datasource.password= 123456
156-
157-
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5InnoDBDialect
158-
spring.jpa.hibernate.ddl-auto= update
159-
160-
# App Properties
161-
bezkoder.app.jwtSecret= bezKoderSecretKey
162-
bezkoder.app.jwtExpirationMs= 86400000
163-
```
164-
## Run Spring Boot application
165-
```
166-
mvn spring-boot:run
167-
```
168-
169-
## Run following SQL insert statements
170-
```
171-
INSERT INTO roles(name) VALUES('ROLE_USER');
172-
INSERT INTO roles(name) VALUES('ROLE_MODERATOR');
173-
INSERT INTO roles(name) VALUES('ROLE_ADMIN');
174-
```

pom.xml

+23-9
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.7.3</version>
9+
<version>3.1.0</version>
1010
<relativePath /> <!-- lookup parent from repository -->
1111
</parent>
1212
<groupId>com.bezkoder</groupId>
@@ -16,7 +16,7 @@
1616
<description>Demo project for Spring Boot Security - JWT</description>
1717

1818
<properties>
19-
<java.version>1.8</java.version>
19+
<java.version>17</java.version>
2020
</properties>
2121

2222
<dependencies>
@@ -29,10 +29,10 @@
2929
<groupId>org.springframework.boot</groupId>
3030
<artifactId>spring-boot-starter-security</artifactId>
3131
</dependency>
32-
32+
3333
<dependency>
34-
<groupId>org.springframework.boot</groupId>
35-
<artifactId>spring-boot-starter-validation</artifactId>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-validation</artifactId>
3636
</dependency>
3737

3838
<dependency>
@@ -41,15 +41,29 @@
4141
</dependency>
4242

4343
<dependency>
44-
<groupId>mysql</groupId>
45-
<artifactId>mysql-connector-java</artifactId>
44+
<groupId>com.mysql</groupId>
45+
<artifactId>mysql-connector-j</artifactId>
46+
<scope>runtime</scope>
47+
</dependency>
48+
49+
<dependency>
50+
<groupId>io.jsonwebtoken</groupId>
51+
<artifactId>jjwt-api</artifactId>
52+
<version>0.11.5</version>
53+
</dependency>
54+
55+
<dependency>
56+
<groupId>io.jsonwebtoken</groupId>
57+
<artifactId>jjwt-impl</artifactId>
58+
<version>0.11.5</version>
4659
<scope>runtime</scope>
4760
</dependency>
4861

4962
<dependency>
5063
<groupId>io.jsonwebtoken</groupId>
51-
<artifactId>jjwt</artifactId>
52-
<version>0.9.1</version>
64+
<artifactId>jjwt-jackson</artifactId>
65+
<version>0.11.5</version>
66+
<scope>runtime</scope>
5367
</dependency>
5468

5569
<dependency>

src/main/java/com/bezkoder/springjwt/controllers/AuthController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import java.util.Set;
66
import java.util.stream.Collectors;
77

8-
import javax.validation.Valid;
8+
import jakarta.validation.Valid;
99

1010
import org.springframework.beans.factory.annotation.Autowired;
1111
import org.springframework.http.ResponseEntity;

src/main/java/com/bezkoder/springjwt/models/Role.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.bezkoder.springjwt.models;
22

3-
import javax.persistence.*;
3+
import jakarta.persistence.*;
44

55
@Entity
66
@Table(name = "roles")

src/main/java/com/bezkoder/springjwt/models/User.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import java.util.HashSet;
44
import java.util.Set;
55

6-
import javax.persistence.*;
7-
import javax.validation.constraints.Email;
8-
import javax.validation.constraints.NotBlank;
9-
import javax.validation.constraints.Size;
6+
import jakarta.persistence.*;
7+
import jakarta.validation.constraints.Email;
8+
import jakarta.validation.constraints.NotBlank;
9+
import jakarta.validation.constraints.Size;
1010

1111
@Entity
1212
@Table(name = "users",

src/main/java/com/bezkoder/springjwt/payload/request/LoginRequest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.bezkoder.springjwt.payload.request;
22

3-
import javax.validation.constraints.NotBlank;
3+
import jakarta.validation.constraints.NotBlank;
44

55
public class LoginRequest {
66
@NotBlank

src/main/java/com/bezkoder/springjwt/payload/request/SignupRequest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import java.util.Set;
44

5-
import javax.validation.constraints.*;
5+
import jakarta.validation.constraints.*;
66

77
public class SignupRequest {
88
@NotBlank

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

+13-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
88
//import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
99
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
10-
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
10+
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
1111
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
1212
//import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
1313
//import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@@ -22,10 +22,10 @@
2222
import com.bezkoder.springjwt.security.services.UserDetailsServiceImpl;
2323

2424
@Configuration
25-
@EnableGlobalMethodSecurity(
26-
// securedEnabled = true,
27-
// jsr250Enabled = true,
28-
prePostEnabled = true)
25+
@EnableMethodSecurity
26+
// (securedEnabled = true,
27+
// jsr250Enabled = true,
28+
// prePostEnabled = true) // by default
2929
public class WebSecurityConfig { // extends WebSecurityConfigurerAdapter {
3030
@Autowired
3131
UserDetailsServiceImpl userDetailsService;
@@ -83,12 +83,14 @@ public PasswordEncoder passwordEncoder() {
8383

8484
@Bean
8585
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();
86+
http.csrf(csrf -> csrf.disable())
87+
.exceptionHandling(exception -> exception.authenticationEntryPoint(unauthorizedHandler))
88+
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
89+
.authorizeHttpRequests(auth ->
90+
auth.requestMatchers("/api/auth/**").permitAll()
91+
.requestMatchers("/api/test/**").permitAll()
92+
.anyRequest().authenticated()
93+
);
9294

9395
http.authenticationProvider(authenticationProvider());
9496

src/main/java/com/bezkoder/springjwt/security/jwt/AuthEntryPointJwt.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import java.util.HashMap;
55
import java.util.Map;
66

7-
import javax.servlet.ServletException;
8-
import javax.servlet.http.HttpServletRequest;
9-
import javax.servlet.http.HttpServletResponse;
7+
import jakarta.servlet.ServletException;
8+
import jakarta.servlet.http.HttpServletRequest;
9+
import jakarta.servlet.http.HttpServletResponse;
1010

1111
import org.slf4j.Logger;
1212
import org.slf4j.LoggerFactory;

src/main/java/com/bezkoder/springjwt/security/jwt/AuthTokenFilter.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import java.io.IOException;
44

5-
import javax.servlet.FilterChain;
6-
import javax.servlet.ServletException;
7-
import javax.servlet.http.HttpServletRequest;
8-
import javax.servlet.http.HttpServletResponse;
5+
import jakarta.servlet.FilterChain;
6+
import jakarta.servlet.ServletException;
7+
import jakarta.servlet.http.HttpServletRequest;
8+
import jakarta.servlet.http.HttpServletResponse;
99

1010
import org.slf4j.Logger;
1111
import org.slf4j.LoggerFactory;
@@ -57,7 +57,7 @@ private String parseJwt(HttpServletRequest request) {
5757
String headerAuth = request.getHeader("Authorization");
5858

5959
if (StringUtils.hasText(headerAuth) && headerAuth.startsWith("Bearer ")) {
60-
return headerAuth.substring(7, headerAuth.length());
60+
return headerAuth.substring(7);
6161
}
6262

6363
return null;

0 commit comments

Comments
 (0)