Skip to content

Commit

Permalink
마무리
Browse files Browse the repository at this point in the history
  • Loading branch information
kgitbank authored and kgitbank committed Sep 11, 2018
1 parent daa9791 commit f8ca14a
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 9 deletions.
9 changes: 8 additions & 1 deletion WebContent/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,24 @@
<listener>
<listener-class>handlers.SessionHandler</listener-class>
</listener>
<listener>
<listener-class>handlers.RequestHandler</listener-class>
</listener>

<!-- filter -->


<!-- filter2 -->
<filter>
<filter-name>m</filter-name>
<filter-class>filters.AuthFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>m</filter-name>
<url-pattern>/account/*</url-pattern>
<url-pattern>/logout.jsp</url-pattern>
<url-pattern>/message/*</url-pattern>
<url-pattern>/board/*</url-pattern>
</filter-mapping>

</web-app>
3 changes: 3 additions & 0 deletions WebContent/board/board_detail.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//1.board.jsp 에서 글번호 파라미터주소로넘김
//2.넘김글번호 받아서 조회쿼리문 실행 하면 해당건만 조회됨
int no = Integer.parseInt(request.getParameter("no")); // 어떻게뽑지??....
BoardDao dao = new BoardDao();
Map detail = dao.getOneByNo(no);
Expand Down
36 changes: 28 additions & 8 deletions WebContent/chk_login.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
//1.로그인했던 아이디 ,pw 파라미터값 받아오기
String login_id = request.getParameter("loginId");
String login_pw = request.getParameter("loginPw");
System.out.println("login_id:"+login_id+"/ login_pw :"+login_pw);
String login_chk = request.getParameter("keep"); // 체크안하면 null
System.out.println("login_id:"+login_id+"/ login_pw :"+login_pw+"/ login_chk:"+login_chk);
//2.객체생성
AccountDao dao = new AccountDao();
//3.account 테이블에있는 전체데이터 datas에 담음
Expand All @@ -18,20 +20,38 @@
Map info = dao.getLoginData(login_id); //로그인한아이디정보 뽑아서 acc에저장
if(info==null || !info.get("pass").equals(login_pw)){//★아이디로 맵조회했으니 비밀번호만비교하면됨
response.sendRedirect(application.getContextPath()+"/login.jsp?mode=fail");//실패시 login.jsp에 넘어감
}else{//패스워드가같으면
Set<String> set = (Set)application.getAttribute("users");// 확인(다른브라우저에서도확인할수있으니)
if(set.contains(login_id)){//아이디가 application에 등록되있으면
throw new RuntimeException(login_id+" 는 이미 로그인되어있습니다.");
}else{
}else{//패스워드가같으면(로그인성공)
Set<String> set = (Set)application.getAttribute("users");// 확인(다른브라우저에서도확인할수있으니)
//if(set.contains(login_id)){//아이디가 application에 등록되있으면
// throw new RuntimeException(login_id+" 는 이미 로그인되어있습니다.");
//}else{ //처음로그인일경우
set.add(login_id); // set에 login_id추가
//Login상태유지
//----------쿠키로그인유지--------------------
if(login_chk!=null){ // 로그인유지 체크면
Cookie c = new Cookie("freepass",login_id); //쿠키생성
c.setMaxAge(60*60*2); // 2시간
c.setPath(application.getContextPath()); // 경로
response.addCookie(c);
}
//----------쿠키로그인유지--------------------
application.setAttribute("users", set);//application에 로그인id담음
session.setAttribute("login_condition", true); //로그인확인 true
session.setAttribute("login_id", login_id); //접속한 id 세션에저장
Date date = new Date(System.currentTimeMillis());//현재시간update
dao.addLoginData(login_id, date);//로그에 담음
response.sendRedirect(application.getContextPath()+"/");//로그인되면 home으로 이동
}
//response.sendRedirect(application.getContextPath()+"/");//로그인되면 home으로 이동
if(session.getAttribute("dest")==null) {
response.sendRedirect(application.getContextPath()+"/");//로그인되면 home으로 이동
}else {
String dest = (String)session.getAttribute("dest");
response.sendRedirect(dest);
}
//}
}
%>
3 changes: 3 additions & 0 deletions WebContent/index.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
if(session.getAttribute("login_condition")==null){ //1.로그인상태가아니면 login.jsp로 보냄
throw new RuntimeException();
}
String login_id = (String)session.getAttribute("login_id"); //로그인아이디
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");//포맷형식
%>
<!DOCTYPE html>
<html>
Expand Down
3 changes: 3 additions & 0 deletions WebContent/login.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
</head>
<body style="text-align: center;">
<h1> W E L C O M E</h1>

<form action="<%=application.getContextPath()%>/chk_login.jsp" method="post">
<label>I D:</label><input type="text" name="loginId" placeholder="아이디를 입력해주세요" style="width: 230px;"><br/><br/>
<label>PW:</label><input type="password" name="loginPw" placeholder="비밀번호를 입력해주세요" style="width: 230px;"><br/><br/>
<input type="checkbox" name="keep" value="on"/>인증유지하기<br/><br/>
<button type="submit">로그인하기</button><br/><br/>
</form>

<a href="create.jsp"><button>회 원 가 입</button></a>

<%if(mode !=null && mode.equals("fail")){%>
Expand Down
7 changes: 7 additions & 0 deletions WebContent/logout.jsp
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//쿠키지우는법(로그아웃시 쿠키삭제가안되면 다시접속하면 로그인되있음)
Cookie c = new Cookie("freepass","dddd");
c.setPath(application.getContextPath());
c.setMaxAge(0); // maxAge 0 초로 설정해서 같은 이름으로 설정해서 보내서
response.addCookie(c); // 기존의 쿠키를 무효화 시켜야한다.
//--------------------------------------------------------
System.out.println("접속한 세션:"+session.getId());
session.invalidate(); // 세션종료
Thread.sleep(2000);
Expand Down
44 changes: 44 additions & 0 deletions WebContent/old_chk_login.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<%@page import="java.sql.Date"%>
<%@page import="java.util.*"%>
<%@page import="beans.AccountDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page errorPage="/error/default.jsp" %>
<%
//1.로그인했던 아이디 ,pw 파라미터값 받아오기
String login_id = request.getParameter("loginId");
String login_pw = request.getParameter("loginPw");
System.out.println("login_id:"+login_id+"/ login_pw :"+login_pw);
//2.객체생성
AccountDao dao = new AccountDao();
//3.account 테이블에있는 전체데이터 datas에 담음
List<Map<String,Object>> datas = dao.getAllDatas(); //null이면 정보없음,로그인비번다름
//4.db에 account테이블의 id,pw 정보와 내가입력한 id,pw정보 비교후 로그인
Map info = dao.getLoginData(login_id); //로그인한아이디정보 뽑아서 acc에저장
if(info==null || !info.get("pass").equals(login_pw)){//★아이디로 맵조회했으니 비밀번호만비교하면됨
response.sendRedirect(application.getContextPath()+"/login.jsp?mode=fail");//실패시 login.jsp에 넘어감
}else{//패스워드가같으면(로그인성공)
Set<String> set = (Set)application.getAttribute("users");// 확인(다른브라우저에서도확인할수있으니)
if(set.contains(login_id)){//아이디가 application에 등록되있으면
throw new RuntimeException(login_id+" 는 이미 로그인되어있습니다.");
}else{ //처음로그인일경우
set.add(login_id); // set에 login_id추가
application.setAttribute("users", set);//application에 로그인id담음
session.setAttribute("login_condition", true); //로그인확인 true
session.setAttribute("login_id", login_id); //접속한 id 세션에저장
Date date = new Date(System.currentTimeMillis());//현재시간update
dao.addLoginData(login_id, date);//로그에 담음
//response.sendRedirect(application.getContextPath()+"/");//로그인되면 home으로 이동
if(session.getAttribute("dest")==null) {
response.sendRedirect(application.getContextPath()+"/");//로그인되면 home으로 이동
}else {
String dest = (String)session.getAttribute("dest");
response.sendRedirect(dest);
}
}
}
%>
1 change: 1 addition & 0 deletions src/filters/AuthFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ protected void doFilter(HttpServletRequest request, HttpServletResponse response
chain.doFilter(request, response); // 肺弊牢夌栏搁 鞘磐烹苞
}else {
response.setContentType("text/html;charset=UTF-8");
session.setAttribute("dest", request.getRequestURI());
//PrintWriter out = response.getWriter();
//out.println("accsee denied");
response.sendRedirect(request.getContextPath()+"/login.jsp");
Expand Down
61 changes: 61 additions & 0 deletions src/handlers/RequestHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package handlers;


import java.sql.Date;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.AccountDao;

public class RequestHandler implements ServletRequestListener {
@Override
public void requestInitialized(ServletRequestEvent sre) {
ServletRequest req = sre.getServletRequest();
HttpServletRequest request = (HttpServletRequest) req;
//if(request.getSession().isNew()) { // 최초요청인지

//}
//----------쿠키로그인유지--------------------
Cookie[] ar = request.getCookies();
String value = null;
if (ar != null) {
for (int i = 0; i < ar.length; i++) {
//System.out.println(ar[i].getName()+"/"+ar[i].getValue());
if (ar[i].getName().equals("freepass")) {
value = ar[i].getValue(); //쿠키벨류값뽑기
}
}

}
if (value != null) {
System.out.println("[CookieHandler] this client has a freepass cookie. value is"+ value);

//----------쿠키로그인유지--------------------
request.getSession().setAttribute("login_condition", true); // 필터에서true안해주면튕기니깐
HttpSession session = request.getSession();
session.setAttribute("login_condition", true); //로그인확인 true
session.setAttribute("login_id", value); //접속한 id 세션에저장

AccountDao dao = new AccountDao();
Map info = dao.getLoginData(value);
Date date = new Date(System.currentTimeMillis());//현재시간update
//if (log != null) {
// session.setAttribute("latest", log.get("time"));
//}
dao.addLoginData(value,date); //시간
//----------쿠키로그인유지--------------------

}else {
System.out.println("[CookieHandler] this client does not hava a freepass cookie");
}
}

}

0 comments on commit f8ca14a

Please sign in to comment.