From ef4f753e0350c03b70b840cfd1a9f60fec5f1162 Mon Sep 17 00:00:00 2001 From: SanoranX Date: Fri, 24 Sep 2021 01:22:15 +0300 Subject: [PATCH] User is now AppUser because of Postgres reservation --- src/main/java/murraco/JwtAuthServiceApp.java | 13 +++++---- .../murraco/controller/UserController.java | 5 ++-- src/main/java/murraco/dto/UserDataDTO.java | 4 +-- .../java/murraco/dto/UserResponseDTO.java | 4 +-- .../murraco/model/{User.java => AppUser.java} | 4 +-- .../model/{Role.java => AppUserRole.java} | 2 +- .../murraco/repository/UserRepository.java | 6 ++--- .../murraco/security/JwtTokenProvider.java | 6 ++--- .../java/murraco/security/MyUserDetails.java | 11 ++++---- .../java/murraco/service/UserService.java | 27 +++++++++---------- 10 files changed, 39 insertions(+), 43 deletions(-) rename src/main/java/murraco/model/{User.java => AppUser.java} (94%) rename src/main/java/murraco/model/{Role.java => AppUserRole.java} (76%) diff --git a/src/main/java/murraco/JwtAuthServiceApp.java b/src/main/java/murraco/JwtAuthServiceApp.java index 282b822..d2414d2 100644 --- a/src/main/java/murraco/JwtAuthServiceApp.java +++ b/src/main/java/murraco/JwtAuthServiceApp.java @@ -4,15 +4,14 @@ import java.util.Arrays; import lombok.RequiredArgsConstructor; +import murraco.model.AppUser; +import murraco.model.AppUserRole; import org.modelmapper.ModelMapper; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; -import murraco.model.Role; -import murraco.model.User; import murraco.service.UserService; @SpringBootApplication @@ -32,19 +31,19 @@ public ModelMapper modelMapper() { @Override public void run(String... params) throws Exception { - User admin = new User(); + AppUser admin = new AppUser(); admin.setUsername("admin"); admin.setPassword("admin"); admin.setEmail("admin@email.com"); - admin.setRoles(new ArrayList(Arrays.asList(Role.ROLE_ADMIN))); + admin.setAppUserRoles(new ArrayList(Arrays.asList(AppUserRole.ROLE_ADMIN))); userService.signup(admin); - User client = new User(); + AppUser client = new AppUser(); client.setUsername("client"); client.setPassword("client"); client.setEmail("client@email.com"); - client.setRoles(new ArrayList(Arrays.asList(Role.ROLE_CLIENT))); + client.setAppUserRoles(new ArrayList(Arrays.asList(AppUserRole.ROLE_CLIENT))); userService.signup(client); } diff --git a/src/main/java/murraco/controller/UserController.java b/src/main/java/murraco/controller/UserController.java index bbdd2d6..e4f60b4 100644 --- a/src/main/java/murraco/controller/UserController.java +++ b/src/main/java/murraco/controller/UserController.java @@ -3,8 +3,8 @@ import javax.servlet.http.HttpServletRequest; import lombok.RequiredArgsConstructor; +import murraco.model.AppUser; import org.modelmapper.ModelMapper; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; @@ -23,7 +23,6 @@ import io.swagger.annotations.Authorization; import murraco.dto.UserDataDTO; import murraco.dto.UserResponseDTO; -import murraco.model.User; import murraco.service.UserService; @RestController @@ -53,7 +52,7 @@ public String login(// @ApiResponse(code = 403, message = "Access denied"), // @ApiResponse(code = 422, message = "Username is already in use")}) public String signup(@ApiParam("Signup User") @RequestBody UserDataDTO user) { - return userService.signup(modelMapper.map(user, User.class)); + return userService.signup(modelMapper.map(user, AppUser.class)); } @DeleteMapping(value = "/{username}") diff --git a/src/main/java/murraco/dto/UserDataDTO.java b/src/main/java/murraco/dto/UserDataDTO.java index a89f499..e7b94f1 100644 --- a/src/main/java/murraco/dto/UserDataDTO.java +++ b/src/main/java/murraco/dto/UserDataDTO.java @@ -5,7 +5,7 @@ import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.NoArgsConstructor; -import murraco.model.Role; +import murraco.model.AppUserRole; @Data @NoArgsConstructor @@ -18,6 +18,6 @@ public class UserDataDTO { @ApiModelProperty(position = 2) private String password; @ApiModelProperty(position = 3) - List roles; + List appUserRoles; } diff --git a/src/main/java/murraco/dto/UserResponseDTO.java b/src/main/java/murraco/dto/UserResponseDTO.java index cc7a19b..d6e71b1 100644 --- a/src/main/java/murraco/dto/UserResponseDTO.java +++ b/src/main/java/murraco/dto/UserResponseDTO.java @@ -4,7 +4,7 @@ import io.swagger.annotations.ApiModelProperty; import lombok.Data; -import murraco.model.Role; +import murraco.model.AppUserRole; @Data public class UserResponseDTO { @@ -16,6 +16,6 @@ public class UserResponseDTO { @ApiModelProperty(position = 2) private String email; @ApiModelProperty(position = 3) - List roles; + List appUserRoles; } diff --git a/src/main/java/murraco/model/User.java b/src/main/java/murraco/model/AppUser.java similarity index 94% rename from src/main/java/murraco/model/User.java rename to src/main/java/murraco/model/AppUser.java index d9ecadb..2fa7b73 100644 --- a/src/main/java/murraco/model/User.java +++ b/src/main/java/murraco/model/AppUser.java @@ -17,7 +17,7 @@ @Entity @Data // Create getters and setters @NoArgsConstructor -public class User { +public class AppUser { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -34,6 +34,6 @@ public class User { private String password; @ElementCollection(fetch = FetchType.EAGER) - List roles; + List appUserRoles; } diff --git a/src/main/java/murraco/model/Role.java b/src/main/java/murraco/model/AppUserRole.java similarity index 76% rename from src/main/java/murraco/model/Role.java rename to src/main/java/murraco/model/AppUserRole.java index 4df1644..ddeed59 100644 --- a/src/main/java/murraco/model/Role.java +++ b/src/main/java/murraco/model/AppUserRole.java @@ -2,7 +2,7 @@ import org.springframework.security.core.GrantedAuthority; -public enum Role implements GrantedAuthority { +public enum AppUserRole implements GrantedAuthority { ROLE_ADMIN, ROLE_CLIENT; public String getAuthority() { diff --git a/src/main/java/murraco/repository/UserRepository.java b/src/main/java/murraco/repository/UserRepository.java index 437a500..f1ead55 100644 --- a/src/main/java/murraco/repository/UserRepository.java +++ b/src/main/java/murraco/repository/UserRepository.java @@ -4,13 +4,13 @@ import org.springframework.data.jpa.repository.JpaRepository; -import murraco.model.User; +import murraco.model.AppUser; -public interface UserRepository extends JpaRepository { +public interface UserRepository extends JpaRepository { boolean existsByUsername(String username); - User findByUsername(String username); + AppUser findByUsername(String username); @Transactional void deleteByUsername(String username); diff --git a/src/main/java/murraco/security/JwtTokenProvider.java b/src/main/java/murraco/security/JwtTokenProvider.java index 463bbdc..7b6970c 100644 --- a/src/main/java/murraco/security/JwtTokenProvider.java +++ b/src/main/java/murraco/security/JwtTokenProvider.java @@ -9,6 +9,7 @@ import javax.annotation.PostConstruct; import javax.servlet.http.HttpServletRequest; +import murraco.model.AppUserRole; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; @@ -23,7 +24,6 @@ import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; import murraco.exception.CustomException; -import murraco.model.Role; @Component public class JwtTokenProvider { @@ -46,10 +46,10 @@ protected void init() { secretKey = Base64.getEncoder().encodeToString(secretKey.getBytes()); } - public String createToken(String username, List roles) { + public String createToken(String username, List appUserRoles) { Claims claims = Jwts.claims().setSubject(username); - claims.put("auth", roles.stream().map(s -> new SimpleGrantedAuthority(s.getAuthority())).filter(Objects::nonNull).collect(Collectors.toList())); + claims.put("auth", appUserRoles.stream().map(s -> new SimpleGrantedAuthority(s.getAuthority())).filter(Objects::nonNull).collect(Collectors.toList())); Date now = new Date(); Date validity = new Date(now.getTime() + validityInMilliseconds); diff --git a/src/main/java/murraco/security/MyUserDetails.java b/src/main/java/murraco/security/MyUserDetails.java index 2f215eb..8e60901 100644 --- a/src/main/java/murraco/security/MyUserDetails.java +++ b/src/main/java/murraco/security/MyUserDetails.java @@ -1,13 +1,12 @@ package murraco.security; import lombok.RequiredArgsConstructor; -import org.springframework.beans.factory.annotation.Autowired; +import murraco.model.AppUser; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; -import murraco.model.User; import murraco.repository.UserRepository; @Service @@ -18,16 +17,16 @@ public class MyUserDetails implements UserDetailsService { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { - final User user = userRepository.findByUsername(username); + final AppUser appUser = userRepository.findByUsername(username); - if (user == null) { + if (appUser == null) { throw new UsernameNotFoundException("User '" + username + "' not found"); } return org.springframework.security.core.userdetails.User// .withUsername(username)// - .password(user.getPassword())// - .authorities(user.getRoles())// + .password(appUser.getPassword())// + .authorities(appUser.getAppUserRoles())// .accountExpired(false)// .accountLocked(false)// .credentialsExpired(false)// diff --git a/src/main/java/murraco/service/UserService.java b/src/main/java/murraco/service/UserService.java index 70a555b..e5c239f 100644 --- a/src/main/java/murraco/service/UserService.java +++ b/src/main/java/murraco/service/UserService.java @@ -3,7 +3,6 @@ import javax.servlet.http.HttpServletRequest; import lombok.RequiredArgsConstructor; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; @@ -12,7 +11,7 @@ import org.springframework.stereotype.Service; import murraco.exception.CustomException; -import murraco.model.User; +import murraco.model.AppUser; import murraco.repository.UserRepository; import murraco.security.JwtTokenProvider; @@ -28,17 +27,17 @@ public class UserService { public String signin(String username, String password) { try { authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password)); - return jwtTokenProvider.createToken(username, userRepository.findByUsername(username).getRoles()); + return jwtTokenProvider.createToken(username, userRepository.findByUsername(username).getAppUserRoles()); } catch (AuthenticationException e) { throw new CustomException("Invalid username/password supplied", HttpStatus.UNPROCESSABLE_ENTITY); } } - public String signup(User user) { - if (!userRepository.existsByUsername(user.getUsername())) { - user.setPassword(passwordEncoder.encode(user.getPassword())); - userRepository.save(user); - return jwtTokenProvider.createToken(user.getUsername(), user.getRoles()); + public String signup(AppUser appUser) { + if (!userRepository.existsByUsername(appUser.getUsername())) { + appUser.setPassword(passwordEncoder.encode(appUser.getPassword())); + userRepository.save(appUser); + return jwtTokenProvider.createToken(appUser.getUsername(), appUser.getAppUserRoles()); } else { throw new CustomException("Username is already in use", HttpStatus.UNPROCESSABLE_ENTITY); } @@ -48,20 +47,20 @@ public void delete(String username) { userRepository.deleteByUsername(username); } - public User search(String username) { - User user = userRepository.findByUsername(username); - if (user == null) { + public AppUser search(String username) { + AppUser appUser = userRepository.findByUsername(username); + if (appUser == null) { throw new CustomException("The user doesn't exist", HttpStatus.NOT_FOUND); } - return user; + return appUser; } - public User whoami(HttpServletRequest req) { + public AppUser whoami(HttpServletRequest req) { return userRepository.findByUsername(jwtTokenProvider.getUsername(jwtTokenProvider.resolveToken(req))); } public String refresh(String username) { - return jwtTokenProvider.createToken(username, userRepository.findByUsername(username).getRoles()); + return jwtTokenProvider.createToken(username, userRepository.findByUsername(username).getAppUserRoles()); } }