스프링08_종합(+암호화), 모듈화

charl hi·2022년 1월 24일
0

Spring

목록 보기
9/25

홈화면 보여주기

HomeController

@Controller
public class HomeController {
	//홈화면 보여주기
	@RequestMapping("/")
	public String home() {
		return "home";
	}
}

http://127.0.0.1:8888/app15/


home.jsp

<body>
	<h1>홈 화면</h1>
	<%	if(session.getAttribute("loginUser") != null){	%>
		<h1>${loginUser.id}님 환영합니다!</h1>
		<h1>${loginUser.nick}님 환영합니다!</h1>
		<h1>나이 : ${loginUser.age}</h1>
		<a href="member/logout">로그아웃</a>
	<%	}else{ %>
			로그인 해주세요!
			<a href="member/login">로그인페이지</a>
	<%	} %>		
</body>




회원

✨join(암호화), login, logout

MemberController


@Controller
@RequestMapping("member")
public class MemberController {
	
	@Autowired
	private SqlSession sqlSession;

	@Autowired
	private PasswordEncoder pe;
	
	// ***회원가입***
	@GetMapping("join")
	public String join() {
		System.out.println("join~~");
		return "member/join";
	}
	
	@PostMapping("join")
	public String join(MemberDto m) {
		
		//암호화
		String newPwd = pe.encode(m.getPwd());
		m.setPwd(newPwd);
		//insert
		int result = sqlSession.insert("member.join", m);
		//view
		if(result > 0) {
			return "redirect:/";
		} else {
			return "redirect:/member/join";
		}
	}
	
	// ***로그인***
	@GetMapping("login")
	public String login() {
		return "member/login";
	}
	
	@PostMapping("login")
	public String login(MemberDto m, HttpSession session) {
		//아이디 갖고 디비가서 비번 가져오기
		MemberDto loginUser = sqlSession.selectOne("member.login", m);
		if(loginUser == null) {
			return "redirect:/member/login";
		}
		String dbPwd = loginUser.getPwd();
		
		//가져온 비번과, 사용자입력 비번이랑 일치하는지 확인
		String userPwd = m.getPwd();
		if(pe.matches(userPwd, dbPwd)) {
			//success
			//session에 담기
			session.setAttribute("loginUser", loginUser);
			return "redirect:/";
		} else {
			//fail
			return "redirect:/member/login";
		}
	}
	
	// ***로그아웃***
	@GetMapping("logout")
	public String logout(HttpSession session) {
		session.invalidate();
		return "redirect:/";
	}
}

암호화

  • 컨트롤러에서 DI받는 객체의 타입을 변경...
  • 서블릿에 등록한 객체를 SCriptPasswordEncoder?로 변경
  • 컨트롤러의 패스워드 인코더 타입을 수정

-> ✨✨상위인 PasswordEncoder로 한다!



member-mapper.xml

<mapper namespace="member">
	<!-- select 가 아닌 dml(insert, delete, update)은 resultType==int 이다!! -->
	<insert id="join" parameterType="memberDto">
		INSERT INTO MEMBER
		VALUES (#{id}, #{pwd}, #{nick}, #{addr}, #{age}, SYSDATE)
	</insert>
	
	<!-- 로그인 -->
	<select id="login" parameterType="memberDto" resultMap="mDto">
		SELECT * FROM MEMBER
		WHERE ID = #{id}
	</select>
	
	<resultMap type="memberDto" id="mDto">
		<result column="ID" property="id"/>
 		<result column="PWD" property="pwd"/>
 		<result column="NICK" property="nick"/>
 		<result column="ADDR" property="addr"/>
 		<result column="AGE" property="age"/>
 		<result column="ENROLL_DATE" property="enrollDate"/>
	</resultMap>
</mapper>



모듈화

✨MemberService는 Spring에 없다!!

1) MemberService를 Spring에 등록시키자

-> ✨✨ @Component

2) @AutoWired로 MemberService를 부르자

-> ✨✨ @AutoWired private MemberService memberService
-> memberService로 다루자

MemberService

@Component
public class MemberService implements MemberServiceInterface{
	
	@Autowired
	private SqlSession sqlSession;
	@Autowired
	private PasswordEncoder pe;
	@Autowired
	private MemberRepositoryInterface memberRepositoryInterface;
	

	public int join(MemberDto m) {
		//암호화
		String newPwd = pe.encode(m.getPwd());
		m.setPwd(newPwd);
		//DB에 insert
		return memberRepositoryInterface.join(m);
	}

}

MemberServiceImpl

@Component
public class MemberServiceImpl implements MemberServiceInterface{

	@Autowired
	private MemberRepositoryInterface memberRepositoryInterface;
	@Autowired
	private PasswordEncoder pe;
	
	@Override
	public int join(MemberDto m) {
		//암호화
//		String newPwd = pe.encode(m.getPwd());
//		m.setPwd(newPwd);
		//암호화 사용하지 않고 그냥 insert
		//DB에 insert
		return memberRepositoryInterface.join(m);
	}

}

MemberServiceInterface

public interface MemberServiceInterface {

	public int join(MemberDto m);
}



MemberRepository

@Component
public class MemberRepository implements MemberRepositoryInterface{
	
	@Autowired
	private SqlSession sqlSession;

	//회원가입
	public int join(MemberDto m) {
		return sqlSession.insert("member.join", m);
	}
}

MemberRepositoryInterface

public interface MemberRepositoryInterface {

	public int join(MemberDto m);
}



MemberController


@Controller
@RequestMapping("member")
public class MemberController {
	
	@Autowired
	private SqlSession sqlSession;

	@Autowired
	private PasswordEncoder pe;
	
	@Autowired
	private MemberServiceInterface memberServiceInterface;
	
	// ***회원가입***
	@GetMapping("join")
	public String join() {
		System.out.println("join~~");
		return "member/join";
	}
	
	@PostMapping("join")
	public String join(MemberDto m) {
		
		// ***모듈화 시작!
		//1. 값을 받기 : 파라미터(MemberDto)로 받음
		
		//2. 나머지 작업(암호화, db에 insert)은 서비스 레이어한테 맡기기
		int result = memberServiceInterface.join(m);
		
		//3. 화면 선택
		//view
		if(result > 0) {
			return "redirect:/";
		} else {
			return "redirect:/member/join";
		}
	}
	
	// ***로그인***
	@GetMapping("login")
	public String login() {
		return "member/login";
	}
	
	@PostMapping("login")
	public String login(MemberDto m, HttpSession session) {
		//아이디 갖고 디비가서 비번 가져오기
		MemberDto loginUser = sqlSession.selectOne("member.login", m);
		if(loginUser == null) {
			return "redirect:/member/login";
		}
		String dbPwd = loginUser.getPwd();
		
		//가져온 비번과, 사용자입력 비번이랑 일치하는지 확인
		String userPwd = m.getPwd();
		if(pe.matches(userPwd, dbPwd)) {
			//success
			//session에 담기
			session.setAttribute("loginUser", loginUser);
			return "redirect:/";
		} else {
			//fail
			return "redirect:/member/login";
		}
	}
	
	// ***로그아웃***
	@GetMapping("logout")
	public String logout(HttpSession session) {
		session.invalidate();
		return "redirect:/";
	}
}


0개의 댓글