- Directory
1. 회원 등록 관련 메소드 추가
MemberService.java
package org.edwith.webbe.securityexam.service;
import org.edwith.webbe.securityexam.dto.Member;
import org.edwith.webbe.securityexam.service.security.UserDbService;
public interface MemberService extends UserDbService {
void addMember(Member member, boolean admin);
Member getMemberByEmail(String loginId);
}
MemberServiceImpl.java
package org.edwith.webbe.securityexam.service;
import org.edwith.webbe.securityexam.dao.MemberDao;
import org.edwith.webbe.securityexam.dao.MemberRoleDao;
import org.edwith.webbe.securityexam.dto.Member;
import org.edwith.webbe.securityexam.dto.MemberRole;
import org.edwith.webbe.securityexam.service.security.UserEntity;
import org.edwith.webbe.securityexam.service.security.UserRoleEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
@Service
public class MemberServiceImpl implements MemberService {
private final MemberDao memberDao;
private final MemberRoleDao memberRoleDao;
public MemberServiceImpl(MemberDao memberDao, MemberRoleDao memberRoleDao) {
this.memberDao = memberDao;
this.memberRoleDao = memberRoleDao;
}
@Override
@Transactional
public UserEntity getUser(String loginUserId) {
Member member = memberDao.getMemberByEmail(loginUserId);
return new UserEntity(member.getEmail(), member.getPassword());
}
@Override
@Transactional
public List<UserRoleEntity> getUserRoles(String loginUserId) {
List<MemberRole> memberRoles = memberRoleDao.getRolesByEmail(loginUserId);
List<UserRoleEntity> list = new ArrayList<>();
for(MemberRole memberRole : memberRoles) {
list.add(new UserRoleEntity(loginUserId, memberRole.getRoleName()));
}
return list;
}
@Override
@Transactional(readOnly = false)
public void addMember(Member member, boolean admin) {
memberDao.addMember(member);
Member selectedMember = memberDao.getMemberByEmail(member.getEmail());
Long memberId = selectedMember.getId();
if(admin) {
memberRoleDao.addAdminRole(memberId);
}
memberRoleDao.addUserRole(memberId);
}
@Override
public Member getMemberByEmail(String email) {
return memberDao.getMemberByEmail(email);
}
}
2. 회원 가입을 위한 컨트롤러와 뷰 작성
MemberController.java
package org.edwith.webbe.securityexam.controller;
import java.security.Principal;
import org.edwith.webbe.securityexam.dto.Member;
import org.edwith.webbe.securityexam.service.MemberService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping(path = "/members")
public class MemberController {
private final MemberService memberService;
private final PasswordEncoder passwordEncoder;
public MemberController(MemberService memberService, PasswordEncoder passwordEncoder){
this.memberService = memberService;
this.passwordEncoder = passwordEncoder;
}
@GetMapping("/loginform")
public String loginform(){
return "members/loginform";
}
@RequestMapping("/loginerror")
public String loginerror(@RequestParam("login_error")String loginError){
return "members/loginerror";
}
@GetMapping("/joinform")
public String joinform(){
return "members/joinform";
}
@PostMapping("/join")
public String join(@ModelAttribute Member member){
member.setPassword(passwordEncoder.encode(member.getPassword()));
memberService.addMember(member, false);
return "redirect:/members/welcome";
}
@GetMapping("/welcome")
public String welcome(){
return "members/welcome";
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>회원 가입</title>
</head>
<body>
<form method="post" action="/securityexam/members/join">
<div>
<label>이름</label>
<input type="text" name="name">
</div>
<div>
<label>비밀번호</label>
<input type="password" name="password">
</div>
<div>
<label>이메일</label>
<input type="text" name="email">
</div>
<div>
<label></label>
<input type="submit" value="회원가입">
</div>
</form>
</body>
</html>
3. DB 등록을 위한 DAO
MemberDaoSqls.java
package org.edwith.webbe.securityexam.dao;
public class MemberDaoSqls {
public static final String SELECT_ALL_BY_EMAIL = "SELECT id, name, password, email, create_date, modify_date FROM member WHERE email = :email";
public static final String INSERT_MEMBER = "INSERT INTO member(name, password, email, create_date, modify_date) "
+ "VALUES (:name, :password, :email, :createDate, :modifyDate);";
}
MemberDao.java
package org.edwith.webbe.securityexam.dao;
import org.edwith.webbe.securityexam.dto.Member;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Repository;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
@Repository
public class MemberDao {
private NamedParameterJdbcTemplate jdbc;
private RowMapper<Member> rowMapper = BeanPropertyRowMapper.newInstance(Member.class);
public MemberDao(DataSource dataSource){
this.jdbc = new NamedParameterJdbcTemplate(dataSource);
}
public Member getMemberByEmail(String email){
Map<String, Object> map = new HashMap<>();
map.put("email", email);
return jdbc.queryForObject(MemberDaoSqls.SELECT_ALL_BY_EMAIL, map, rowMapper);
}
public void addMember(Member member) {
Map<String, Object> params = new HashMap<>();
params.put("name", member.getName());
params.put("password", member.getPassword());
params.put("email", member.getEmail());
params.put("createDate", member.getCreateDate());
params.put("modifyDate", member.getModifyDate());
jdbc.update(MemberDaoSqls.INSERT_MEMBER, params);
}
}
MemberRoleDaoSqls.java
package org.edwith.webbe.securityexam.dao;
public class MemberRoleDaoSqls {
public static final String SELECT_ALL_BY_EMAIL = "SELECT mr.id, mr.member_id, mr.role_name FROM member_role mr JOIN member m ON mr.member_id = m.id WHERE m.email = :email";
public static final String INSERT_ADMIN_ROLE =
"INSERT INTO member_role(member_id, role_name) "
+ "VALUES (:memberId, \"ROLE_ADMIN\");";
public static final String INSERT_USER_ROLE =
"INSERT INTO member_role(member_id, role_name) "
+ "VALUES (:memberId, \"ROLE_USER\");";
}
MemberRoleDao.java
package org.edwith.webbe.securityexam.dao;
import org.edwith.webbe.securityexam.dto.MemberRole;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Repository;
import javax.sql.DataSource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Repository
public class MemberRoleDao {
private NamedParameterJdbcTemplate jdbc;
private RowMapper<MemberRole> rowMapper = BeanPropertyRowMapper.newInstance(MemberRole.class);
public MemberRoleDao(DataSource dataSource){
this.jdbc = new NamedParameterJdbcTemplate(dataSource);
}
public List<MemberRole> getRolesByEmail(String email){
Map<String, Object> map = new HashMap<>();
map.put("email", email);
return jdbc.query(MemberRoleDaoSqls.SELECT_ALL_BY_EMAIL, map, rowMapper);
}
public void addAdminRole(Long memberId) {
Map<String, Object> params = Collections.singletonMap("memberId", memberId);
jdbc.update(MemberRoleDaoSqls.INSERT_ADMIN_ROLE, params);
}
public void addUserRole(Long memberId) {
Map<String, Object> params = Collections.singletonMap("memberId", memberId);
jdbc.update(MemberRoleDaoSqls.INSERT_USER_ROLE, params);
}
}
결과