Implement User Role
This commit is contained in:
@@ -1,26 +0,0 @@
|
|||||||
package com.api;
|
|
||||||
|
|
||||||
//@Configuration
|
|
||||||
//public class AppConfig {
|
|
||||||
// private final UserRepository userRepository;
|
|
||||||
// private final PasswordEncoder bCryptPasswordEncoder;
|
|
||||||
//
|
|
||||||
// public AppConfig(UserRepository userRepository, PasswordEncoder bCryptPasswordEncoder) {
|
|
||||||
// System.out.println("AppConfig");
|
|
||||||
// System.out.println("userRepository = " + userRepository);
|
|
||||||
// this.userRepository = userRepository;
|
|
||||||
// this.bCryptPasswordEncoder = bCryptPasswordEncoder;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Bean
|
|
||||||
// public UserService userService() {
|
|
||||||
// System.out.println("userService");
|
|
||||||
// return new UserServiceImpl(userRepository, bCryptPasswordEncoder);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//// @Bean
|
|
||||||
//// public BCryptPasswordEncoder passwordEncoder() {
|
|
||||||
//// System.out.println("passwordEncoder");
|
|
||||||
//// return new BCryptPasswordEncoder();
|
|
||||||
//// }
|
|
||||||
//}
|
|
||||||
@@ -45,7 +45,9 @@ public class AuthServiceImpl implements AuthService {
|
|||||||
if(userRepository.existsByEmail(signUpReq.getEmail())) {
|
if(userRepository.existsByEmail(signUpReq.getEmail())) {
|
||||||
return new SignUpRes(false, "Your Mail already Exist.");
|
return new SignUpRes(false, "Your Mail already Exist.");
|
||||||
}
|
}
|
||||||
|
|
||||||
Users newUser = signUpReq.toUserEntity();
|
Users newUser = signUpReq.toUserEntity();
|
||||||
|
|
||||||
newUser.hashPassword(bCryptPasswordEncoder);
|
newUser.hashPassword(bCryptPasswordEncoder);
|
||||||
|
|
||||||
Users user = userRepository.save(newUser);
|
Users user = userRepository.save(newUser);
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.api.auth.dtos;
|
package com.api.auth.dtos;
|
||||||
|
|
||||||
|
import com.api.user.domain.UserRole;
|
||||||
import com.api.user.domain.Users;
|
import com.api.user.domain.Users;
|
||||||
import javax.validation.constraints.Email;
|
import javax.validation.constraints.Email;
|
||||||
import javax.validation.constraints.NotEmpty;
|
import javax.validation.constraints.NotEmpty;
|
||||||
@@ -9,6 +10,7 @@ import lombok.ToString;
|
|||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@ToString
|
@ToString
|
||||||
|
@Builder
|
||||||
public class SignUpReq {
|
public class SignUpReq {
|
||||||
@NotEmpty(message = "Please enter your Email")
|
@NotEmpty(message = "Please enter your Email")
|
||||||
@Email
|
@Email
|
||||||
@@ -17,23 +19,27 @@ public class SignUpReq {
|
|||||||
private String password;
|
private String password;
|
||||||
@NotEmpty(message = "Please enter your Name")
|
@NotEmpty(message = "Please enter your Name")
|
||||||
private String name;
|
private String name;
|
||||||
|
private UserRole role;
|
||||||
@Builder
|
|
||||||
public SignUpReq(String email, String password, String name) {
|
|
||||||
this.email = email;
|
|
||||||
this.password = password;
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transform to User Entity
|
* Transform to User Entity
|
||||||
* @return User Entity
|
* @return User Entity
|
||||||
*/
|
*/
|
||||||
public Users toUserEntity() {
|
public Users toUserEntity() {
|
||||||
|
if(this.getRole() != null) {
|
||||||
|
return Users.builder()
|
||||||
|
.email(this.getEmail())
|
||||||
|
.password(this.getPassword())
|
||||||
|
.name(this.getName())
|
||||||
|
.role(this.getRole())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
else {
|
||||||
return Users.builder()
|
return Users.builder()
|
||||||
.email(this.getEmail())
|
.email(this.getEmail())
|
||||||
.password(this.getPassword())
|
.password(this.getPassword())
|
||||||
.name(this.getName())
|
.name(this.getName())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package com.api.config;
|
|||||||
import com.api.user.domain.Users;
|
import com.api.user.domain.Users;
|
||||||
import com.api.exception.UserNotFoundException;
|
import com.api.exception.UserNotFoundException;
|
||||||
import com.api.user.repository.UserRepository;
|
import com.api.user.repository.UserRepository;
|
||||||
import java.util.HashSet;
|
import java.util.Collections;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.security.core.GrantedAuthority;
|
import org.springframework.security.core.GrantedAuthority;
|
||||||
@@ -21,7 +21,8 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
|||||||
System.out.println("email in loadUserByUsername = " + email);
|
System.out.println("email in loadUserByUsername = " + email);
|
||||||
Users user = userRepository.findByEmail(email)
|
Users user = userRepository.findByEmail(email)
|
||||||
.orElseThrow(UserNotFoundException::new);
|
.orElseThrow(UserNotFoundException::new);
|
||||||
Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
|
|
||||||
|
Set<GrantedAuthority> grantedAuthorities = Collections.singleton(user.getRole());
|
||||||
|
|
||||||
return new org
|
return new org
|
||||||
.springframework
|
.springframework
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ public class UserController {
|
|||||||
Users userDetail = userService.findByEmail(userDetails.getUsername())
|
Users userDetail = userService.findByEmail(userDetails.getUsername())
|
||||||
.orElseThrow(() -> new UserNotFoundException());
|
.orElseThrow(() -> new UserNotFoundException());
|
||||||
|
|
||||||
|
System.out.println("userDetails.getAuthorities " + userDetails.getAuthorities());
|
||||||
return ProfileRes.builder()
|
return ProfileRes.builder()
|
||||||
.email(userDetail.getEmail())
|
.email(userDetail.getEmail())
|
||||||
.name(userDetail.getName())
|
.name(userDetail.getName())
|
||||||
|
|||||||
@@ -1,8 +1,15 @@
|
|||||||
package com.api.user.domain;
|
package com.api.user.domain;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import org.springframework.security.core.GrantedAuthority;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
public enum UserRole {
|
public enum UserRole implements GrantedAuthority {
|
||||||
ROLE_USER // Spring Security의 role 네이밍 규칙 : ROLE_권한이름
|
ROLE_CLIENT, // Spring Security의 role 네이밍 규칙 : ROLE_권한이름
|
||||||
|
ROLE_ADMIN;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAuthority() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,9 +3,12 @@ package com.api.user.domain;
|
|||||||
import com.api.common.domain.CoreEntity;
|
import com.api.common.domain.CoreEntity;
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.EnumType;
|
||||||
|
import javax.persistence.Enumerated;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
@@ -15,6 +18,7 @@ import org.springframework.security.crypto.password.PasswordEncoder;
|
|||||||
@Getter @Setter
|
@Getter @Setter
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@ToString
|
@ToString
|
||||||
|
@Builder
|
||||||
public class Users extends CoreEntity {
|
public class Users extends CoreEntity {
|
||||||
@Column(nullable = false, unique = true)
|
@Column(nullable = false, unique = true)
|
||||||
private String email;
|
private String email;
|
||||||
@@ -22,16 +26,17 @@ public class Users extends CoreEntity {
|
|||||||
private String password;
|
private String password;
|
||||||
@Column(length = 10, nullable = false, unique = true)
|
@Column(length = 10, nullable = false, unique = true)
|
||||||
private String name;
|
private String name;
|
||||||
|
@Enumerated(EnumType.STRING)
|
||||||
|
@Column(nullable = false)
|
||||||
|
@Builder.Default
|
||||||
|
private UserRole role = UserRole.ROLE_CLIENT;
|
||||||
|
|
||||||
// @Enumerated(EnumType.STRING)
|
|
||||||
// private UserRole role;
|
|
||||||
|
|
||||||
@Builder
|
public Users(String email, String password, String name, UserRole role) {
|
||||||
public Users(String email, String password, String name /*UserRole role*/) {
|
|
||||||
this.email = email;
|
this.email = email;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
// this.role = role;
|
this.role = role;
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://reflectoring.io/spring-security-password-handling/
|
// https://reflectoring.io/spring-security-password-handling/
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.api.user.repository;
|
|||||||
import com.api.user.domain.Users;
|
import com.api.user.domain.Users;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
|
|||||||
Reference in New Issue
Block a user