login
This commit is contained in:
@@ -10,5 +10,4 @@ import org.springframework.stereotype.Repository;
|
||||
@Repository
|
||||
public interface UserRepository extends CrudRepository<UserEntity, Long> {
|
||||
Optional<UserEntity> findByUsername(String username);
|
||||
Optional<UserEntity> findByUsernameAndPassword(String username, String password);
|
||||
}
|
||||
|
||||
@@ -9,6 +9,16 @@ public class LoginDTO {
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
public void setUsername(String username){
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public void setPassword(String password){
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public LoginDTO() {}
|
||||
|
||||
public LoginDTO(String username, String password){
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
|
||||
@@ -37,16 +37,18 @@ public class UserServiceImpl implements UserService {
|
||||
|
||||
@Override
|
||||
public UserEntity login(UserEntity userEntity, HttpSession httpSession){
|
||||
UserEntity user = (UserEntity) httpSession.getAttribute("user");
|
||||
if(user == null){
|
||||
user = userRepository.findByUsernameAndPassword(
|
||||
userEntity.getUsername(),
|
||||
passwordEncoder.encode(userEntity.getPassword()))
|
||||
.orElseThrow(() -> new WrongUsernameAndPasswordException("wrong username and password"));
|
||||
UserEntity user = userRepository.findByUsername(userEntity.getUsername())
|
||||
.orElseThrow(() -> new WrongUsernameAndPasswordException("wrong username"));
|
||||
|
||||
if(passwordEncoder.matches(userEntity.getPassword(), user.getPassword())){
|
||||
log.info("login user: " + userEntity.toString());
|
||||
httpSession.setAttribute("user",user);
|
||||
log.info("session id: " + httpSession.getId());
|
||||
log.info("session value: " + httpSession.getAttribute("user"));
|
||||
} else {
|
||||
throw new WrongUsernameAndPasswordException("wrong passowrd");
|
||||
}
|
||||
httpSession.setAttribute("user",user);
|
||||
log.info("session id: " + httpSession.getId());
|
||||
log.info("session value: " + httpSession.getAttribute("user"));
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
|
||||
@@ -82,7 +82,7 @@ public class UserControllerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("login test")
|
||||
@DisplayName("success login test")
|
||||
public void loginTest() throws Exception {
|
||||
UserEntity user = createTestUser("testuser", "testpw");
|
||||
|
||||
@@ -102,4 +102,26 @@ public class UserControllerTest {
|
||||
|
||||
actions.andExpectAll(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("fail login test")
|
||||
public void failLoginTest() throws Exception{
|
||||
UserEntity user = createTestUser("testuser", "testpw");
|
||||
|
||||
Map<String, Object> requestBody = new HashMap<>();
|
||||
requestBody.put("username", user.getUsername());
|
||||
requestBody.put("password", "testw");
|
||||
String json = new ObjectMapper().registerModule(new JavaTimeModule()).writeValueAsString(requestBody);
|
||||
|
||||
final ResultActions actions = mvc.perform(
|
||||
post("/user/login/")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.session(httpSession)
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.characterEncoding("UTF-8")
|
||||
.content(json)
|
||||
);
|
||||
|
||||
actions.andExpectAll(status().isNotFound());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user