# 회원 가입
---
## 회원 가입 버튼 추가
**views/statics/login.jsp**
![Imgur](http://i.imgur.com/JxLEbO5.png)
```jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page pageEncoding="utf-8"%>
<div class="jumbotron">
<h1>로그인</h1>
</div>
<div class="container">
<c:url var="loginPath" value="/user/login" />
<c:url var="signUpPath" value="/signup" />
<form action="${ loginPath }" method="post">
<div class="form-group form-group-lg">
<div class="form-group">
<label>사용자명</label> <input type="text" name="email" class="form-control" placeholder="이메일">
</div>
<div class="form-group">
<label>비밀번호</label> <input type="password" name="password" class="form-control" placeholder="비밀번호">
</div>
<div class="form-group">
<input type="hidden" name="${ _csrf.parameterName }" value="${ _csrf.token }">
</div>
<div class="form-action">
<input type="submit" class="btn btn-primary btn-lg" value="로그인">
<a href="${ signUpPath }" class="btn btn-success btn-lg">회원 가입</a>
</div>
</div>
</form>
</div>
```
## 컨트롤러 및 메소드 등록
**SessionsController.java**
```java
@Controller
public class SessionsController {
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login() {
return "statics/login";
}
@RequestMapping(value = "/signup", method = RequestMethod.GET)
public String signup(Model model) {
User user = new User();
model.addAttribute("user", user);
return "statics/signup";
}
}
```
## User 클래스 생성
vo 패키지에 아래의 소스코드를 작성합니다.
**User.java**
```java
public class User {
int id;
String email;
String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", email=" + email + ", password=" + password + "]";
}
}
```
## 뷰 페이지 생성
![Imgur](http://i.imgur.com/OMer82X.png)
**views/statics/signup.jsp**
```jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="s" uri="http://www.springframework.org/security/tags"%>
<%@ taglib prefix="f" uri="http://www.springframework.org/tags/form"%>
<%@ page pageEncoding="utf-8"%>
<div class="jumbotron">
<h1>가입하기</h1>
</div>
<div class="container">
<c:url var="signUpPath" value="/signup" />
<f:form modelAttribute="user" action="${ signUpPath }" method="post">
<div class="form-group form-group-lg">
<div class="form-group">
<label>이메일</label>
<f:input path="email" cssClass="form-control" />
<f:errors path="email" element="div" cssClass="alert text-danger" />
</div>
<div class="form-group">
<label>비밀번호</label>
<f:password path="password" cssClass="form-control" placeholder="비밀번호" />
<f:errors path="password" element="div" cssClass="alert text-danger" />
</div>
<div class="form-group"></div>
<div class="form-action">
<s:csrfInput />
<input type="submit" class="btn btn-primary btn-lg btn-block" value="회원 가입">
</div>
</div>
</f:form>
</div>
```
## 시큐리티 설정 추가
security-context.xml
```xml
...
<sec:http auto-config='true' use-expressions="true">
<sec:intercept-url pattern="/" access="permitAll" />
<sec:intercept-url pattern="/login" access="permitAll" />
<sec:intercept-url pattern="/signup" access="permitAll" />
<sec:intercept-url pattern="/**" access="isAuthenticated()" />
...
```
## 폼 데이터 받아오기
**UsersController.java**
```java
@Controller
public class UsersController {
...
@ResponseBody
@RequestMapping(value = "/signup", method = RequestMethod.POST)
public String create(@ModelAttribute User user) {
return user.toString();
}
}
```
**회원 가입 데이터 전송**
```
Email: 1234
Password: 1234
```
**실행결과**
```
User [id=0, email=1234, password=1234]
```
## DB로 회원 정보 저장하기
**UsersController.java**
```java
@Controller
public class UsersController {
@Autowired
private UserMapper userMapper;
@ResponseBody
@RequestMapping(value = "/signup", method = RequestMethod.POST)
public String create(@ModelAttribute User user) {
userMapper.insertUser(user);
userMapper.insertAuthority(user.getEmail(), "ROLE_USER");
return "redirect:/statics/login";
}
}
```
**UserMapper.java**
```java
public interface UserMapper {
@Insert("INSERT INTO users (email, password, enabled) "
+ "VALUES (#{email}, #{password}, true)")
public boolean insertUser(User user);
@Insert("INSERT INTO authorities (email, authority) "
+ "VALUES (#{email}, #{authority})")
public boolean insertAuthority(@Param("email") String email, @Param("authority") String authority);
}
```