login api

This commit is contained in:
aisensiy
2017-08-14 11:09:11 +08:00
parent 6bc7b84327
commit f81a048f14
6 changed files with 146 additions and 11 deletions

View File

@@ -3,6 +3,7 @@ package io.spring.api;
import com.fasterxml.jackson.annotation.JsonRootName;
import io.spring.api.exception.InvalidRequestException;
import io.spring.application.user.UserQueryService;
import io.spring.core.user.EncryptService;
import io.spring.core.user.User;
import io.spring.core.user.UserRepository;
import lombok.Getter;
@@ -12,32 +13,36 @@ import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import java.util.Optional;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
@RestController
public class UsersApi {
private UserRepository userRepository;
private UserQueryService userQueryService;
private String defaultImage;
private EncryptService encryptService;
@Autowired
public UsersApi(UserRepository userRepository,
UserQueryService userQueryService,
EncryptService encryptService,
@Value("${image.default}") String defaultImage) {
this.userRepository = userRepository;
this.userQueryService = userQueryService;
this.encryptService = encryptService;
this.defaultImage = defaultImage;
}
@RequestMapping(path = "/users", method = RequestMethod.POST)
@RequestMapping(path = "/users", method = POST)
public ResponseEntity createUser(@Valid @RequestBody RegisterParam registerParam, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
throw new InvalidRequestException(bindingResult);
@@ -54,12 +59,34 @@ public class UsersApi {
User user = new User(
registerParam.getEmail(),
registerParam.getUsername(),
registerParam.getPassword(),
encryptService.encrypt(registerParam.getPassword()),
"",
defaultImage);
userRepository.save(user);
return ResponseEntity.status(201).body(userQueryService.fetchCreatedUser(user.getUsername()));
return ResponseEntity.status(201).body(userQueryService.fetchNewAuthenticatedUser(user.getUsername()));
}
@RequestMapping(path = "/users/login", method = POST)
public ResponseEntity userLogin(@Valid @RequestBody LoginParam loginParam, BindingResult bindingResult) {
Optional<User> optional = userRepository.findByEmail(loginParam.getEmail());
if (optional.isPresent() && encryptService.check(loginParam.getPassword(), optional.get().getPassword())) {
return ResponseEntity.ok(userQueryService.fetchNewAuthenticatedUser(optional.get().getUsername()));
} else {
bindingResult.rejectValue("password", "INVALID", "invalid email or password");
throw new InvalidRequestException(bindingResult);
}
}
}
@Getter
@JsonRootName("user")
@NoArgsConstructor
class LoginParam {
@NotBlank(message = "can't be empty")
@Email(message = "should be an email")
private String email;
@NotBlank(message = "can't be empty")
private String password;
}
@Getter

View File

@@ -15,7 +15,7 @@ public class UserQueryService {
this.jwtService = jwtService;
}
public UserWithToken fetchCreatedUser(String username) {
public UserWithToken fetchNewAuthenticatedUser(String username) {
UserData userData = userReadService.findOne(username);
return new UserWithToken(userData, jwtService.toToken(userData));
}

View File

@@ -0,0 +1,6 @@
package io.spring.core.user;
public interface EncryptService {
String encrypt(String password);
boolean check(String checkPassword, String realPassword);
}

View File

@@ -0,0 +1,17 @@
package io.spring.infrastructure.user;
import io.spring.core.user.EncryptService;
import org.springframework.stereotype.Service;
@Service
public class NaiveEncryptService implements EncryptService {
@Override
public String encrypt(String password) {
return password;
}
@Override
public boolean check(String checkPassword, String realPassword) {
return checkPassword.equals(realPassword);
}
}