본문 바로가기
스프링과 JPA 기반 웹 어플리케이션 개발/1부 (개발환경, 회원가입, 로그인, 계정설정)

03. 회원 가입: 컨트롤러

by Backchus 2020. 4. 17.

목표

  • GET "sign-up" 요청을 받아서 account/sign-up.html 페이지를 보여준다.
  • 회원 가입 폼에서 입력받을 수 있는 정보를 "닉네임", "이메일", "패스워드" 폼 객체로 제공한다.

Account 컨트롤러 등록

1
2
3
4
5
6
7
8
9
@RequiredArgsConstructor
@Controller
public class AccountController {
 
   @GetMapping("/sign-up")
   public String signUpForm(Model model){
       model.addAtrribute(new SignUpForm());
       return "account/sign-up";
   }
 
cs

 

화면 동작 확인(index.html)

1
2
3
4
5
6
7
8
9
10
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
   <meta charset="UTF-8">
   <title>Title</title>
</head>
<body>
Home
</body>
</html>
 
 

 

회원 가입 폼(/account/sign-up.html)

1
2
3
4
5
6
7
8
9
10
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
Sign-up page
</body>
</html>
 
s

 

참고: 하지만 SpringSecurity를 이미 등록해두었기 때문에 화면을 띄우기 위해 스프링부트를 실행하면 모든 요청을 인증을 필요로 한다고 가정하기 때문에 로그인 창만 뜨고 화면으로 이동할 수가 없다. 따라서 Security설정을 해야 한다.

SpringSecurity 설정

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .mvcMatchers("/""/login""/sign-up""/check-email""/check-email-token",
                        "/email-login""/check-email-login""/login-link").permitAll()
                .mvcMatchers(HttpMethod.GET, "/profile/*").permitAll()
                .anyRequest().authenticated();
        ;
    }
 
    // static 관련 파일들은 스프링 시큐리티 적용 x
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring()
                .requestMatchers(PathRequest.toStaticResources().atCommonLocations());
    }
}
 
 

 

sign-up 요청 화면 테스트

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package me.weekbelt.studyolle.account;
 
@AutoConfigureMockMvc
@SpringBootTest
class AccountControllerTest {
 
    @Autowired
    private MockMvc mockMvc;
 
    @DisplayName("회원 가입 화면 보이는지 테스트")
    @Test
    public void signUpForm() throws Exception {
        mockMvc.perform(get("/sign-up"))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(view().name("account/sign-up"))
                .andExpect(model().attributeExists("signUpForm"));
    }
}
 
 

 

참고: https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-JPA-%EC%9B%B9%EC%95%B1#

 

스프링과 JPA 기반 웹 애플리케이션 개발 - 인프런

이 강좌에서 여러분은 실제로 운영 중인 서비스를 스프링, JPA 그리고 타임리프를 비롯한 여러 자바 기반의 여러 오픈 소스 기술을 사용하여 웹 애플리케이션을 개발하는 과정을 학습할 수 있습니다. 이 강좌를 충분히 학습한다면 여러분 만의 웹 서비스를 만들거나 취직에 도움이 될만한 포트폴리오를 만들 수 있을 겁니다. 활용 웹 개발 프레임워크 및 라이브러리 Java Spring Spring Boot Spring Data JPA Thymeleaf 온라인 강의 스

www.inflearn.com