Spring Cloud - 10. Users Microservice 사용자 추가

목차

Users Microservice 사용자 추가

@RestController
@RequestMapping("/")
public class UserController {
private Environment env;
private UserService userService;
private Greeting greeting;

public UserController(Environment env, UserService userService){
this.env = env;
this.userService = userService;
}

@GetMapping("/health_check")
public String status(){
return "It's Working in User Service";
}

@GetMapping("/welcome")
public String welcome(){
return env.getProperty("greeting.message");
}

@GetMapping("/welcome_value")
public String welcomeValue(){
return greeting.getMessage();
}

@PostMapping("/users")
public ResponseEntity createUser(@RequestBody RequestUser user){
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
UserDto userDto = modelMapper.map(user, UserDto.class);
userService.createUser(userDto);

ResponseUser responseUser = modelMapper.map(userDto, ResponseUser.class);
return ResponseEntity.status(HttpStatus.CREATED).body(responseUser);
}
}
@Data
public class ResponseUser {
private String email;
private String name;
private String userId;
}
@Data
public class RequestUser {
@NotNull(message = "Email cannot be null")
@Size(min = 2, message = "Email not be less than two characters")
private String email;

@NotNull(message = "Name cannot be null")
@Size(min = 2, message = "Name not be less than two characters")
private String pwd;

@NotNull(message = "Password cannot be null")
@Size(min = 8, message = "Password not be less than two characters")
private String name;
}
@Data
public class UserDto {
private String email;
private String name;
private String pwd;
private String userId;
private Date createdAt;

private String encryptedPwd;
}
@Component
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Greeting {
@Value("${greeting.message}")
private String message;
}
@Data
@Entity
@Table("users")
public class UserEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false, length = 50)
private String email;

@Column(nullable = false, length = 50)
private String name;

@Column(nullable = false, unique = true)
private String userId;

@Column(nullable = false, unique = true)
private String encryptedPwd;
}
public interface UserRepository extends CrudRepository<UserEntity, Long> {
}
@Service
public class UserServiceImpl implements UserService{

@Autowired
UserRepository userRepository;

@Override
public UserDto createUser(UserDto userDto) {
userDto.setUserId(UUID.randomUUID().toString());

ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
UserEntity userEntity = modelMapper.map(userDto, UserEntity.class);
userEntity.setEncryptedPwd("encrypted_password");

userRepository.save(userEntity);

return null;
}
}

Spring Security 연동

@Configuration
@EnableWebSecurity
public class WebSecurity extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();

http.authorizeRequests().antMatchers("/users/**").permitAll();

http.headers().frameOptions().disable();
}

@Bean
public BCryptPasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
}
@Service
public class UserServiceImpl implements UserService{
UserRepository userRepository;
BCryptPasswordEncoder passwordEncoder;

public UserServiceImpl(UserRepository userRepository, BCryptPasswordEncoder passwordEncoder){
this.userRepository = userRepository;
this.passwordEncoder = passwordEncoder;
}

@Override
public UserDto createUser(UserDto userDto) {
userDto.setUserId(UUID.randomUUID().toString());

ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
UserEntity userEntity = modelMapper.map(userDto, UserEntity.class);
userEntity.setEncryptedPwd(passwordEncoder.encode(userDto.getPwd()));

userRepository.save(userEntity);

UserDto returnUserDto = modelMapper.map(userEntity, UserDto.class);

return returnUserDto;
}
}
Share