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