Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
c3f08a8
commit 5f50b67
Showing
20 changed files
with
853 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
src/main/java/com/isolaja/skillsearchapp/controller/AuthController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package com.isolaja.skillsearchapp.controller; | ||
|
||
import com.isolaja.skillsearchapp.model.ERole; | ||
import com.isolaja.skillsearchapp.model.Role; | ||
import com.isolaja.skillsearchapp.model.User; | ||
import com.isolaja.skillsearchapp.payload.request.LoginRequest; | ||
import com.isolaja.skillsearchapp.payload.request.SignupRequest; | ||
import com.isolaja.skillsearchapp.payload.response.JwtResponse; | ||
import com.isolaja.skillsearchapp.payload.response.MessageResponse; | ||
import com.isolaja.skillsearchapp.repository.RoleRepository; | ||
import com.isolaja.skillsearchapp.repository.UserRepository; | ||
import com.isolaja.skillsearchapp.security.jwt.JwtUtils; | ||
import com.isolaja.skillsearchapp.security.services.UserDetailsImpl; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import javax.validation.Valid; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
@CrossOrigin(origins = "*", maxAge = 3600) | ||
@RestController | ||
@RequestMapping("/api/auth") | ||
public class AuthController { | ||
@Autowired | ||
AuthenticationManager authenticationManager; | ||
|
||
@Autowired | ||
UserRepository userRepository; | ||
|
||
@Autowired | ||
RoleRepository roleRepository; | ||
|
||
@Autowired | ||
PasswordEncoder encoder; | ||
|
||
@Autowired | ||
JwtUtils jwtUtils; | ||
|
||
@PostMapping("/signin") | ||
public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginRequest loginRequest) { | ||
|
||
Authentication authentication = authenticationManager.authenticate( | ||
new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword())); | ||
|
||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
String jwt = jwtUtils.generateJwtToken(authentication); | ||
|
||
UserDetailsImpl userDetails = (UserDetailsImpl) authentication.getPrincipal(); | ||
List<String> roles = userDetails.getAuthorities().stream() | ||
.map(GrantedAuthority::getAuthority) | ||
.collect(Collectors.toList()); | ||
|
||
return ResponseEntity.ok(new JwtResponse(jwt, | ||
userDetails.getId(), | ||
userDetails.getUsername(), | ||
userDetails.getEmail(), | ||
roles)); | ||
} | ||
|
||
@PostMapping("/signup") | ||
public ResponseEntity<?> registerUser(@Valid @RequestBody SignupRequest signUpRequest) { | ||
if (userRepository.existsByUsername(signUpRequest.getUsername())) { | ||
return ResponseEntity | ||
.badRequest() | ||
.body(new MessageResponse("Error: Username is already taken!")); | ||
} | ||
|
||
if (userRepository.existsByEmail(signUpRequest.getEmail())) { | ||
return ResponseEntity | ||
.badRequest() | ||
.body(new MessageResponse("Error: Email is already in use!")); | ||
} | ||
|
||
// Create new user's account | ||
User user = new User(signUpRequest.getUsername(), | ||
signUpRequest.getEmail(), | ||
encoder.encode(signUpRequest.getPassword())); | ||
|
||
Set<String> strRoles = signUpRequest.getRole(); | ||
Set<Role> roles = new HashSet<>(); | ||
|
||
if (strRoles == null) { | ||
Role userRole = roleRepository.findByName(ERole.ROLE_USER) | ||
.orElseThrow(() -> new RuntimeException("Error: Role is not found.")); | ||
roles.add(userRole); | ||
} else { | ||
strRoles.forEach(role -> { | ||
switch (role) { | ||
case "admin": | ||
Role adminRole = roleRepository.findByName(ERole.ROLE_ADMIN) | ||
.orElseThrow(() -> new RuntimeException("Error: Role is not found.")); | ||
roles.add(adminRole); | ||
|
||
break; | ||
case "mod": | ||
Role modRole = roleRepository.findByName(ERole.ROLE_MODERATOR) | ||
.orElseThrow(() -> new RuntimeException("Error: Role is not found.")); | ||
roles.add(modRole); | ||
|
||
break; | ||
default: | ||
Role userRole = roleRepository.findByName(ERole.ROLE_USER) | ||
.orElseThrow(() -> new RuntimeException("Error: Role is not found.")); | ||
roles.add(userRole); | ||
} | ||
}); | ||
} | ||
|
||
user.setRoles(roles); | ||
userRepository.save(user); | ||
|
||
return ResponseEntity.ok(new MessageResponse("User registered successfully!")); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/isolaja/skillsearchapp/controller/TestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.isolaja.skillsearchapp.controller; | ||
|
||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.web.bind.annotation.CrossOrigin; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@CrossOrigin(origins = "*", maxAge = 3600) | ||
@RestController | ||
@RequestMapping("/api/test") | ||
public class TestController { | ||
@GetMapping("/all") | ||
public String allAccess() { | ||
return "Public Content."; | ||
} | ||
|
||
@GetMapping("/user") | ||
@PreAuthorize("hasRole('USER') or hasRole('MODERATOR') or hasRole('ADMIN')") | ||
public String userAccess() { | ||
return "User Content."; | ||
} | ||
|
||
@GetMapping("/mod") | ||
@PreAuthorize("hasRole('MODERATOR')") | ||
public String moderatorAccess() { | ||
return "Moderator Board."; | ||
} | ||
|
||
@GetMapping("/admin") | ||
@PreAuthorize("hasRole('ADMIN')") | ||
public String adminAccess() { | ||
return "Admin Board."; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.isolaja.skillsearchapp.model; | ||
|
||
public enum ERole { | ||
ROLE_USER, | ||
ROLE_MODERATOR, | ||
ROLE_ADMIN | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.isolaja.skillsearchapp.model; | ||
|
||
import javax.persistence.*; | ||
|
||
@Entity | ||
@Table(name = "roles") | ||
public class Role { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Integer id; | ||
|
||
@Enumerated(EnumType.STRING) | ||
@Column(length = 20) | ||
private ERole name; | ||
|
||
public Role() { | ||
|
||
} | ||
|
||
public Role(ERole name) { | ||
this.name = name; | ||
} | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Integer id) { | ||
this.id = id; | ||
} | ||
|
||
public ERole getName() { | ||
return name; | ||
} | ||
|
||
public void setName(ERole name) { | ||
this.name = name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.isolaja.skillsearchapp.model; | ||
|
||
import javax.persistence.*; | ||
import javax.validation.constraints.Email; | ||
import javax.validation.constraints.NotBlank; | ||
import javax.validation.constraints.Size; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
@Entity | ||
@Table( | ||
uniqueConstraints = { | ||
@UniqueConstraint(columnNames = "username"), | ||
@UniqueConstraint(columnNames = "email") | ||
}) | ||
public class User { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@NotBlank | ||
@Size(max = 20) | ||
private String username; | ||
|
||
@NotBlank | ||
@Size(max = 50) | ||
private String email; | ||
|
||
@NotBlank | ||
@Size(max = 120) | ||
private String password; | ||
|
||
@ManyToMany(fetch = FetchType.LAZY) | ||
@JoinTable(name = "user_roles", | ||
joinColumns = @JoinColumn(name = "user_id"), | ||
inverseJoinColumns = @JoinColumn(name = "role_id")) | ||
private Set<Role> roles = new HashSet<>(); | ||
|
||
public User() { | ||
} | ||
|
||
public User(String username, String email, String password) { | ||
this.username = username; | ||
this.email = email; | ||
this.password = password; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public void setUsername(String username) { | ||
this.username = username; | ||
} | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
|
||
public void setEmail(String email) { | ||
this.email = email; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public void setPassword(String password) { | ||
this.password = password; | ||
} | ||
|
||
public Set<Role> getRoles() { | ||
return roles; | ||
} | ||
|
||
public void setRoles(Set<Role> roles) { | ||
this.roles = roles; | ||
} | ||
} |
Oops, something went wrong.