forked from kookmin-sw/cap-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from hightuv/회원가입-기능추가
로그인, 회원가입 기능추가
- Loading branch information
Showing
13 changed files
with
369 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 37 additions & 14 deletions
51
src/main/java/com/example/WebOrder/controller/LoginController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,85 @@ | ||
package com.example.WebOrder.controller; | ||
|
||
import com.example.WebOrder.dto.LoginDto; | ||
import com.example.WebOrder.dto.RegisterDto; | ||
import com.example.WebOrder.dto.LoginFormDto; | ||
import com.example.WebOrder.dto.UserFormDto; | ||
import com.example.WebOrder.entity.User; | ||
import com.example.WebOrder.service.LoginService; | ||
import jakarta.validation.Valid; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.validation.BindingResult; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.ModelAttribute; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
|
||
@Slf4j | ||
@Controller | ||
public class LoginController { | ||
private final LoginService loginService; | ||
private final PasswordEncoder passwordEncoder; | ||
|
||
public LoginController(LoginService loginService) { | ||
public LoginController(LoginService loginService, PasswordEncoder passwordEncoder) { | ||
this.loginService = loginService; | ||
this.passwordEncoder = passwordEncoder; | ||
} | ||
|
||
@GetMapping("/index") | ||
public String getIndex(){ | ||
return "/html/index"; | ||
} | ||
@GetMapping("/login") | ||
public String getLoginForm(){ | ||
public String getLoginForm(@ModelAttribute("loginFormDto") LoginFormDto dto){ | ||
log.info("로그인 폼 소환"); | ||
return "/html/login"; | ||
return "html/loginForm"; | ||
} | ||
|
||
@PostMapping("/login") | ||
public String login(LoginDto dto){ | ||
public String login(@Valid @ModelAttribute ("loginFormDto") LoginFormDto dto, BindingResult bindingResult){ | ||
log.info("로그인 시도"); | ||
if (loginService.isLoginAttemptValid(dto)){ | ||
log.info("로그인 성공"); | ||
return "/html/index"; | ||
return "html/index"; | ||
} | ||
else { | ||
log.info("로그인 실패"); | ||
return "redirect:/login?error=true"; | ||
bindingResult.reject("loginFail", "아이디 또는 비밀번호가 맞지 않습니다."); | ||
return "html/loginForm"; | ||
} | ||
} | ||
|
||
@GetMapping("/register") | ||
public String getRegisterForm(){ | ||
public String registerForm(@ModelAttribute("userFormDto") UserFormDto dto) { | ||
log.info("회원가입 폼 소환"); | ||
return "/html/register"; | ||
return "html/registerForm"; | ||
} | ||
|
||
@PostMapping("/register") | ||
public String register(RegisterDto dto){ | ||
public String register(@Valid UserFormDto dto, BindingResult bindingResult){ | ||
log.info("회원가입 시도"); | ||
if (loginService.usernameExists(dto.getUsername())){ | ||
if (bindingResult.hasErrors()) { | ||
log.info("회원가입 실패"); | ||
return "html/registerForm"; | ||
} | ||
|
||
if (loginService.findByUsername(dto.getUsername()).isPresent()){ | ||
log.info("username 중복"); | ||
return "redirect:/register?error=true"; | ||
} | ||
|
||
if (!dto.getPassword().equals(dto.getPasswordCheck())) { | ||
bindingResult.rejectValue("passwordCheck", "passwordNotSame", "비밀번호를 다시 확인해주세요."); | ||
return "html/registerForm"; | ||
} | ||
else { | ||
log.info("회원가입 성공"); | ||
loginService.createUser(dto); | ||
return "redirect:/login"; | ||
User user = new User(); | ||
user.setUsername(dto.getUsername()); | ||
user.setPassword(passwordEncoder.encode(dto.getPassword())); | ||
user.setName(dto.getName()); | ||
loginService.join(user); | ||
return "redirect:/"; | ||
} | ||
} | ||
} |
5 changes: 4 additions & 1 deletion
5
...va/com/example/WebOrder/dto/LoginDto.java → ...om/example/WebOrder/dto/LoginFormDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
package com.example.WebOrder.dto; | ||
|
||
import jakarta.validation.constraints.NotEmpty; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
|
||
@Getter | ||
@Setter | ||
@ToString | ||
public class LoginDto { | ||
public class LoginFormDto { | ||
@NotEmpty | ||
private String username; | ||
@NotEmpty | ||
private String password; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.example.WebOrder.dto; | ||
|
||
import jakarta.validation.constraints.NotEmpty; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.hibernate.validator.constraints.Length; | ||
|
||
@Getter | ||
@Setter | ||
public class UserFormDto { | ||
@NotEmpty(message = "아이디는 필수 입력 값입니다.") | ||
private String username; | ||
|
||
@NotEmpty(message = "비밀번호는 필수 입력 값입니다.") | ||
@Length(min = 8, max = 16, message = "비밀번호는 8자 이상, 16자 이하로 입력해주세요.") | ||
private String password; | ||
|
||
@NotEmpty(message = "비밀번호를 한 번 더 입력해주세요.") | ||
private String passwordCheck; | ||
|
||
@NotEmpty(message = "이름은 필수 입력 값입니다.") | ||
private String name; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
.body { | ||
overflow:hidden; | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: center; | ||
align-items: center; | ||
} | ||
.container { | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: center; | ||
align-items: center; | ||
width:100vw; | ||
height:100vh; | ||
} | ||
.logo { | ||
background-color:rgba(157.25000202655792, 157.25000202655792, 157.25000202655792, 1); | ||
width:130px; | ||
height:130px; | ||
margin-bottom: 20px; | ||
} | ||
.login-form { | ||
width:320px; | ||
height:auto; | ||
position:relative; | ||
margin-top: 20px; | ||
} | ||
.form-group { | ||
width:320px; | ||
height:79px; | ||
position:relative; | ||
margin-top:15px; | ||
} | ||
.form-label { | ||
color:rgba(64.00000378489494, 64.00000378489494, 64.00000378489494, 1); | ||
position:relative; | ||
font-family:Noto Sans KR, sans-serif; | ||
text-align:left; | ||
font-size:16px; | ||
letter-spacing:0; | ||
} | ||
.form-input-text { | ||
background-color: rgba(255, 255, 255, 1); | ||
width: 320px; | ||
height: 50px; | ||
position: relative; | ||
font-family: Noto Sans KR, sans-serif; | ||
text-align: left; | ||
font-size: 14px; | ||
border-radius: 5px; | ||
border: 1px solid rgba(174.00000482797623, 174.00000482797623, 174.00000482797623, 1); | ||
} | ||
|
||
.login-button { | ||
background-color:rgba(91.46796986460686, 96.86175003647804, 226.31249696016312, 1); | ||
width:320px; | ||
height:55px; | ||
color:rgba(255, 255, 255, 1); | ||
font-family:Noto Sans KR, sans-serif; | ||
text-align:center; | ||
font-size:16px; | ||
letter-spacing:0; | ||
position:relative; | ||
margin-top: 20%; | ||
} | ||
.global-error { | ||
border-color: #dc3545; | ||
color: #dc3545; | ||
font-family:Noto Sans KR, sans-serif; | ||
font-size:12px; | ||
letter-spacing:0; | ||
} | ||
.ask-register { | ||
color:rgba(102.00000151991844, 102.00000151991844, 102.00000151991844, 1); | ||
font-family:Noto Sans KR, sans-serif; | ||
font-size:16px; | ||
letter-spacing:0; | ||
display: flex; | ||
align-items: center; | ||
position:relative; | ||
margin-top: 10%; | ||
} |
Oops, something went wrong.