Skip to content

Commit e037bf6

Browse files
committed
update to spring 2.6.1
1 parent 3c425d7 commit e037bf6

21 files changed

+580
-538
lines changed

README.md

+24-3
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,19 @@ For instruction: [Spring Boot Refresh Token with JWT example](https://bezkoder.c
3131
3232
> [Spring Boot + Angular 11 JWT Authentication](https://bezkoder.com/angular-11-spring-boot-jwt-auth/)
3333
34+
> [Spring Boot + Angular 12 JWT Authentication](https://www.bezkoder.com/angular-12-spring-boot-jwt-auth/)
35+
3436
> [Spring Boot + React JWT Authentication](https://bezkoder.com/spring-boot-react-jwt-auth/)
3537
3638
## Fullstack CRUD App
3739

38-
> [Vue.js + Spring Boot + MySQL/PostgreSQL example](https://bezkoder.com/spring-boot-vue-js-crud-example/)
40+
> [Vue.js + Spring Boot + H2 Embedded database example](https://www.bezkoder.com/spring-boot-vue-js-crud-example/)
41+
42+
> [Vue.js + Spring Boot + MySQL example](https://www.bezkoder.com/spring-boot-vue-js-mysql/)
43+
44+
> [Vue.js + Spring Boot + PostgreSQL example](https://www.bezkoder.com/spring-boot-vue-js-postgresql/)
45+
46+
> [Angular 8 + Spring Boot + Embedded database example](https://www.bezkoder.com/angular-spring-boot-crud/)
3947
4048
> [Angular 8 + Spring Boot + MySQL example](https://bezkoder.com/angular-spring-boot-crud/)
4149
@@ -49,6 +57,18 @@ For instruction: [Spring Boot Refresh Token with JWT example](https://bezkoder.c
4957
5058
> [Angular 11 + Spring Boot + PostgreSQL example](https://bezkoder.com/angular-11-spring-boot-postgresql/)
5159
60+
> [Angular 12 + Spring Boot + Embedded database example](https://www.bezkoder.com/angular-12-spring-boot-crud/)
61+
62+
> [Angular 12 + Spring Boot + MySQL example](https://www.bezkoder.com/angular-12-spring-boot-mysql/)
63+
64+
> [Angular 12 + Spring Boot + PostgreSQL example](https://www.bezkoder.com/angular-12-spring-boot-postgresql/)
65+
66+
> [Angular 13 + Spring Boot + H2 Embedded Database example](https://www.bezkoder.com/spring-boot-angular-13-crud/)
67+
68+
> [Angular 13 + Spring Boot + MySQL example](https://www.bezkoder.com/spring-boot-angular-13-mysql/)
69+
70+
> [Angular 13 + Spring Boot + PostgreSQL example](https://www.bezkoder.com/spring-boot-angular-13-postgresql/)
71+
5272
> [React + Spring Boot + MySQL example](https://bezkoder.com/react-spring-boot-crud/)
5373
5474
> [React + Spring Boot + PostgreSQL example](https://bezkoder.com/spring-boot-react-postgresql/)
@@ -69,9 +89,10 @@ More Practice:
6989
7090
> [Spring Boot Repository Unit Test with @DataJpaTest](https://bezkoder.com/spring-boot-unit-test-jpa-repo-datajpatest/)
7191
72-
> [Deploy Spring Boot App on AWS – Elastic Beanstalk](https://bezkoder.com/deploy-spring-boot-aws-eb/)
92+
Deployment:
93+
> [Deploy Spring Boot App on AWS – Elastic Beanstalk](https://www.bezkoder.com/deploy-spring-boot-aws-eb/)
7394
74-
> [Secure Spring Boot App with Spring Security & JWT Authentication](https://bezkoder.com/spring-boot-jwt-authentication/)
95+
> [Docker Compose Spring Boot and MySQL example](https://www.bezkoder.com/docker-compose-spring-boot-mysql/)
7596
7697
## Dependency
7798
– If you want to use PostgreSQL:

pom.xml

+6-1
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.1.8.RELEASE</version>
9+
<version>2.6.1</version>
1010
<relativePath /> <!-- lookup parent from repository -->
1111
</parent>
1212
<groupId>com.bezkoder</groupId>
@@ -29,6 +29,11 @@
2929
<groupId>org.springframework.boot</groupId>
3030
<artifactId>spring-boot-starter-security</artifactId>
3131
</dependency>
32+
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-validation</artifactId>
36+
</dependency>
3237

3338
<dependency>
3439
<groupId>org.springframework.boot</groupId>

src/main/java/com/bezkoder/springjwt/SpringBootSecurityJwtApplication.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
public class SpringBootSecurityJwtApplication {
88

99
public static void main(String[] args) {
10-
SpringApplication.run(SpringBootSecurityJwtApplication.class, args);
10+
SpringApplication.run(SpringBootSecurityJwtApplication.class, args);
1111
}
1212

1313
}

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

+90-90
Original file line numberDiff line numberDiff line change
@@ -36,94 +36,94 @@
3636
@RestController
3737
@RequestMapping("/api/auth")
3838
public class AuthController {
39-
@Autowired
40-
AuthenticationManager authenticationManager;
41-
42-
@Autowired
43-
UserRepository userRepository;
44-
45-
@Autowired
46-
RoleRepository roleRepository;
47-
48-
@Autowired
49-
PasswordEncoder encoder;
50-
51-
@Autowired
52-
JwtUtils jwtUtils;
53-
54-
@PostMapping("/signin")
55-
public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginRequest loginRequest) {
56-
57-
Authentication authentication = authenticationManager.authenticate(
58-
new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword()));
59-
60-
SecurityContextHolder.getContext().setAuthentication(authentication);
61-
String jwt = jwtUtils.generateJwtToken(authentication);
62-
63-
UserDetailsImpl userDetails = (UserDetailsImpl) authentication.getPrincipal();
64-
List<String> roles = userDetails.getAuthorities().stream()
65-
.map(item -> item.getAuthority())
66-
.collect(Collectors.toList());
67-
68-
return ResponseEntity.ok(new JwtResponse(jwt,
69-
userDetails.getId(),
70-
userDetails.getUsername(),
71-
userDetails.getEmail(),
72-
roles));
73-
}
74-
75-
@PostMapping("/signup")
76-
public ResponseEntity<?> registerUser(@Valid @RequestBody SignupRequest signUpRequest) {
77-
if (userRepository.existsByUsername(signUpRequest.getUsername())) {
78-
return ResponseEntity
79-
.badRequest()
80-
.body(new MessageResponse("Error: Username is already taken!"));
81-
}
82-
83-
if (userRepository.existsByEmail(signUpRequest.getEmail())) {
84-
return ResponseEntity
85-
.badRequest()
86-
.body(new MessageResponse("Error: Email is already in use!"));
87-
}
88-
89-
// Create new user's account
90-
User user = new User(signUpRequest.getUsername(),
91-
signUpRequest.getEmail(),
92-
encoder.encode(signUpRequest.getPassword()));
93-
94-
Set<String> strRoles = signUpRequest.getRole();
95-
Set<Role> roles = new HashSet<>();
96-
97-
if (strRoles == null) {
98-
Role userRole = roleRepository.findByName(ERole.ROLE_USER)
99-
.orElseThrow(() -> new RuntimeException("Error: Role is not found."));
100-
roles.add(userRole);
101-
} else {
102-
strRoles.forEach(role -> {
103-
switch (role) {
104-
case "admin":
105-
Role adminRole = roleRepository.findByName(ERole.ROLE_ADMIN)
106-
.orElseThrow(() -> new RuntimeException("Error: Role is not found."));
107-
roles.add(adminRole);
108-
109-
break;
110-
case "mod":
111-
Role modRole = roleRepository.findByName(ERole.ROLE_MODERATOR)
112-
.orElseThrow(() -> new RuntimeException("Error: Role is not found."));
113-
roles.add(modRole);
114-
115-
break;
116-
default:
117-
Role userRole = roleRepository.findByName(ERole.ROLE_USER)
118-
.orElseThrow(() -> new RuntimeException("Error: Role is not found."));
119-
roles.add(userRole);
120-
}
121-
});
122-
}
123-
124-
user.setRoles(roles);
125-
userRepository.save(user);
126-
127-
return ResponseEntity.ok(new MessageResponse("User registered successfully!"));
128-
}
39+
@Autowired
40+
AuthenticationManager authenticationManager;
41+
42+
@Autowired
43+
UserRepository userRepository;
44+
45+
@Autowired
46+
RoleRepository roleRepository;
47+
48+
@Autowired
49+
PasswordEncoder encoder;
50+
51+
@Autowired
52+
JwtUtils jwtUtils;
53+
54+
@PostMapping("/signin")
55+
public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginRequest loginRequest) {
56+
57+
Authentication authentication = authenticationManager.authenticate(
58+
new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword()));
59+
60+
SecurityContextHolder.getContext().setAuthentication(authentication);
61+
String jwt = jwtUtils.generateJwtToken(authentication);
62+
63+
UserDetailsImpl userDetails = (UserDetailsImpl) authentication.getPrincipal();
64+
List<String> roles = userDetails.getAuthorities().stream()
65+
.map(item -> item.getAuthority())
66+
.collect(Collectors.toList());
67+
68+
return ResponseEntity.ok(new JwtResponse(jwt,
69+
userDetails.getId(),
70+
userDetails.getUsername(),
71+
userDetails.getEmail(),
72+
roles));
73+
}
74+
75+
@PostMapping("/signup")
76+
public ResponseEntity<?> registerUser(@Valid @RequestBody SignupRequest signUpRequest) {
77+
if (userRepository.existsByUsername(signUpRequest.getUsername())) {
78+
return ResponseEntity
79+
.badRequest()
80+
.body(new MessageResponse("Error: Username is already taken!"));
81+
}
82+
83+
if (userRepository.existsByEmail(signUpRequest.getEmail())) {
84+
return ResponseEntity
85+
.badRequest()
86+
.body(new MessageResponse("Error: Email is already in use!"));
87+
}
88+
89+
// Create new user's account
90+
User user = new User(signUpRequest.getUsername(),
91+
signUpRequest.getEmail(),
92+
encoder.encode(signUpRequest.getPassword()));
93+
94+
Set<String> strRoles = signUpRequest.getRole();
95+
Set<Role> roles = new HashSet<>();
96+
97+
if (strRoles == null) {
98+
Role userRole = roleRepository.findByName(ERole.ROLE_USER)
99+
.orElseThrow(() -> new RuntimeException("Error: Role is not found."));
100+
roles.add(userRole);
101+
} else {
102+
strRoles.forEach(role -> {
103+
switch (role) {
104+
case "admin":
105+
Role adminRole = roleRepository.findByName(ERole.ROLE_ADMIN)
106+
.orElseThrow(() -> new RuntimeException("Error: Role is not found."));
107+
roles.add(adminRole);
108+
109+
break;
110+
case "mod":
111+
Role modRole = roleRepository.findByName(ERole.ROLE_MODERATOR)
112+
.orElseThrow(() -> new RuntimeException("Error: Role is not found."));
113+
roles.add(modRole);
114+
115+
break;
116+
default:
117+
Role userRole = roleRepository.findByName(ERole.ROLE_USER)
118+
.orElseThrow(() -> new RuntimeException("Error: Role is not found."));
119+
roles.add(userRole);
120+
}
121+
});
122+
}
123+
124+
user.setRoles(roles);
125+
userRepository.save(user);
126+
127+
return ResponseEntity.ok(new MessageResponse("User registered successfully!"));
128+
}
129129
}

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

+20-20
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,26 @@
1010
@RestController
1111
@RequestMapping("/api/test")
1212
public class TestController {
13-
@GetMapping("/all")
14-
public String allAccess() {
15-
return "Public Content.";
16-
}
17-
18-
@GetMapping("/user")
19-
@PreAuthorize("hasRole('USER') or hasRole('MODERATOR') or hasRole('ADMIN')")
20-
public String userAccess() {
21-
return "User Content.";
22-
}
13+
@GetMapping("/all")
14+
public String allAccess() {
15+
return "Public Content.";
16+
}
2317

24-
@GetMapping("/mod")
25-
@PreAuthorize("hasRole('MODERATOR')")
26-
public String moderatorAccess() {
27-
return "Moderator Board.";
28-
}
18+
@GetMapping("/user")
19+
@PreAuthorize("hasRole('USER') or hasRole('MODERATOR') or hasRole('ADMIN')")
20+
public String userAccess() {
21+
return "User Content.";
22+
}
2923

30-
@GetMapping("/admin")
31-
@PreAuthorize("hasRole('ADMIN')")
32-
public String adminAccess() {
33-
return "Admin Board.";
34-
}
24+
@GetMapping("/mod")
25+
@PreAuthorize("hasRole('MODERATOR')")
26+
public String moderatorAccess() {
27+
return "Moderator Board.";
28+
}
29+
30+
@GetMapping("/admin")
31+
@PreAuthorize("hasRole('ADMIN')")
32+
public String adminAccess() {
33+
return "Admin Board.";
34+
}
3535
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.bezkoder.springjwt.models;
22

33
public enum ERole {
4-
ROLE_USER,
5-
ROLE_MODERATOR,
6-
ROLE_ADMIN
4+
ROLE_USER,
5+
ROLE_MODERATOR,
6+
ROLE_ADMIN
77
}

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

+23-23
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,35 @@
55
@Entity
66
@Table(name = "roles")
77
public class Role {
8-
@Id
9-
@GeneratedValue(strategy = GenerationType.IDENTITY)
10-
private Integer id;
8+
@Id
9+
@GeneratedValue(strategy = GenerationType.IDENTITY)
10+
private Integer id;
1111

12-
@Enumerated(EnumType.STRING)
13-
@Column(length = 20)
14-
private ERole name;
12+
@Enumerated(EnumType.STRING)
13+
@Column(length = 20)
14+
private ERole name;
1515

16-
public Role() {
16+
public Role() {
1717

18-
}
18+
}
1919

20-
public Role(ERole name) {
21-
this.name = name;
22-
}
20+
public Role(ERole name) {
21+
this.name = name;
22+
}
2323

24-
public Integer getId() {
25-
return id;
26-
}
24+
public Integer getId() {
25+
return id;
26+
}
2727

28-
public void setId(Integer id) {
29-
this.id = id;
30-
}
28+
public void setId(Integer id) {
29+
this.id = id;
30+
}
3131

32-
public ERole getName() {
33-
return name;
34-
}
32+
public ERole getName() {
33+
return name;
34+
}
3535

36-
public void setName(ERole name) {
37-
this.name = name;
38-
}
36+
public void setName(ERole name) {
37+
this.name = name;
38+
}
3939
}

0 commit comments

Comments
 (0)