[SPRING] ch04. 로그인

2017. 9. 21. 10:16·SKILL/Security
728x90
반응형

PwController.java

 

package secure.ch04.ex01.controller;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/ch04/ex01/pw")
public class PwController {
    @RequestMapping(method=RequestMethod.GET)
    public void main(){}
    
    @RequestMapping(method=RequestMethod.POST)
    @ResponseBody
    public String validate(String pw){
        String result = "BAD.";
        String pwPolicy = "((?=.*[a-zA-Z])(?=.*[!@#])(?=.*[0-9]).{3,5})";
        Pattern pattern = Pattern.compile(pwPolicy);
        Matcher matcher = pattern.matcher(pw);
        if(matcher.matches()) result = "GOOD.";
        return result;
    }
}
 
 
/WEB-INF/views/ch04/ex01/pw.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<script src="//code.jquery.com/jquery-3.1.1.min.js"></script>
<script>
$(function(){
    $("button").bind("click", function(){
        $("#msg").empty();        
        var pw = $("input[name=pw]").val();
        
        if(isValidPw(pw)){
            $.ajax({
                method: "post",
                data: {"pw":pw},
                success: function(result){
                    $("#msg").text("SERVER: " + result);
                },
                error: function(a, b, errMsg){
                    $("#msg").text("SERVER: " + errMsg);
                }
            });
        }else{
            $("#msg").text("CLIENT: bad.");
        }
    });
});

var isValidPw = function(pw){
    var pattern = /^(?=.*[a-zA-Z])(?=.*[!@#])(?=.*[0-9]).{3,5}$/;
    return pattern.test(pw);
};
</script>
<form>
    <input type="text" name="pw"/><br><br>
    <button type="button">검증</button>
</form>
<p id="msg"></p>
 
 
* 나쁜 암호
123456
password
12345678
qwerty
12345
123456789
football
1234
1234567
baseball
welcome
1234567890
abc123
11111111
1qaz2wsx
dragon
master
monkey
letmein
login
princess
qwertyuiop
solo
passw0rd
starwars
--
 
TimeoutController.java
 
package secure.ch04.ex02.controller;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/ch04/ex02")
public class TimeoutController {
    @RequestMapping("/main")
    public void main(){}
    
    @RequestMapping(value="/login", method=RequestMethod.GET)
    public void login(){}
    
    @RequestMapping(value="/login", method=RequestMethod.POST)
    public String login(@ModelAttribute("userId") String userId, HttpSession session){
        session.setAttribute("userId", userId);
        session.setMaxInactiveInterval(10); //10초
        return "redirect:main";
    }
    
    @RequestMapping(value="/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:main";
    }
    
    @RequestMapping(value="/article")
    public String article(HttpSession session){
        String view = "";
        String userId = (String)session.getAttribute("userId");
        if(userId != null) view = "ch04/ex02/article";
        else view = "redirect:login";
        return view;
    }
}
 
/WEB-INF/views/ch04/ex02/main.jsp
 
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<p>
    <c:choose>
        <c:when test="${empty userId}">
            <a href="login">로그인</a>
        </c:when>
        <c:otherwise>
            ${userId}님, 환영합니다.  
            <a href="logout">로그아웃</a>
        </c:otherwise>
    </c:choose>
</p>
<a href="article">기사 보기</a>
 
/WEB-INF/views/ch04/ex02/login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<form method="post">
    <input type="text" name="userId"><br> 
    <input type="password" name="userPw"><br><br>
    <button type="submit">제출</button>
</form>
 
 
/WEB-INF/views/ch04/ex02/article.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<a href="logout">로그아웃</a>
<p>기사</p>
<a href="main">메인으로</a>
 
--
 
Tomcat의 web.xml 에 타임아웃 설정
 
<session-config>
    <session-timeout>30</session-timeout>
</session-config>
 

 

 

LoginCntController.java

package secure.ch04.ex03.controller;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/ch04/ex03")
public class LoginCntController {
    @RequestMapping(value="/login", method=RequestMethod.GET)
    public void login(){}
    
    @RequestMapping(value="/login", method=RequestMethod.POST)
    public String login(String userId, HttpSession session, Model model){
        String result = "";
        if(userId.equals("id")) {
            session.setAttribute("userId", userId);
            result = "ch04/ex03/loginAfter";
        }else {
            int loginCnt = 1;
            Object obj = session.getAttribute("loginCnt");
            if(obj != null) loginCnt += (int)obj;
            if(loginCnt > 3) result = "ch04/ex03/loginDeny";
            else{
                session.setAttribute("loginCnt", loginCnt);            
                result = "redirect:login";
            }
        }
        return result;
    }
    
    @RequestMapping(value="/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:login";
    }
}
 
/WEB-INF/views/ch04/ex03/login.jsp
 
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<form method="post">
    <input type="text" name="userId"><br>
    <input type="password" name="userPw"><br><br>
    <button type="submit">제출</button>
</form>
<c:if test="${!(empty sessionScope.loginCnt)}">
    ${sessionScope.loginCnt}회, 로그인 실패.
</c:if>
 
/WEB-INF/views/ch04/ex03/loginAfter.jsp
 
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
${userId}님, 환영합니다. <br>
<a href="logout">로그아웃</a>
 
 
/WEB-INF/views/ch04/ex03/loginDeny.jsp
 
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
계정 사용 차단
 

728x90
반응형

'SKILL > Security' 카테고리의 다른 글

LETSENCRYPT 에서 SSL 인증서를 무료로 발급 받아 웹 서버에 적용하기  (0) 2018.05.17
SSL 보안 인증서 발급 - CSR 발급  (0) 2018.05.17
[SPRING] ch03. OS 명령어  (0) 2017.09.21
[SPRING] ch02. SQL Injection - 방어  (0) 2017.09.21
[SPRING] ch01.SQL Injection - 공격  (0) 2017.09.21
'SKILL/Security' 카테고리의 다른 글
  • LETSENCRYPT 에서 SSL 인증서를 무료로 발급 받아 웹 서버에 적용하기
  • SSL 보안 인증서 발급 - CSR 발급
  • [SPRING] ch03. OS 명령어
  • [SPRING] ch02. SQL Injection - 방어
밍글링글링
밍글링글링
mingling - 밍글링, 밍글밍글링. 코드와 어우러지다. IT/ 프로그래밍/소스
    반응형
    250x250
  • 밍글링글링
    mingling
    밍글링글링
  • 전체
    오늘
    어제
    • 밍글링글링 (407) N
      • Flutter (2)
      • 일상생활 (8)
        • 리뷰 (1)
        • 생활정보 (4)
        • 맛집 (0)
        • 여행 (0)
        • 모든정보 (3)
      • JAVA (126)
        • 개념 (6)
        • 예제 (115)
        • Exception (2)
      • C (1)
        • C (1)
        • C++ (0)
        • C# (0)
      • JS (29)
        • JavaScript (18)
        • JQuery (5)
        • AJax (0)
        • NODE.JS (6)
        • Angular.JS 2.0 (0)
      • WEB (87)
        • HTML (6)
        • CSS (61)
        • JSP (20)
        • JSTL (0)
      • FrameWork (8)
        • Spring (8)
        • BootStrap (0)
        • MyBATIS (0)
        • JUnit (0)
      • 외부 라이브러리 (5)
      • 공유 소스 관리 (5)
        • Git (5)
        • SVN (0)
      • 빅데이터 프로그래밍 (37)
        • Python (37)
        • R Programming (0)
      • DB (7)
        • ORACLE (0)
        • MySql (6)
      • Development Tools (7)
        • StarUML (0)
        • eXERD (0)
        • Eclipse (4)
      • SKILL (6)
        • Migration (0)
        • Security (6)
      • MicroSoft (0)
        • Excel (0)
        • Word (0)
      • Android (0)
      • Server (21)
        • Ubuntu (5)
        • Linux (15)
      • IOS (0)
      • XML (0)
      • 미디어 (0)
      • 공지사항 (3)
      • NETWORK (1)
      • 게임 (4)
        • 피파 (1)
        • 리니지M (0)
        • 배틀그라운드 (1)
        • 듀랑고 (2)
      • 세상 이슈 (6)
      • 일렉트론 (0)
      • 대회 소식 (4)
      • 업무 (2)
      • Express, Vue (6)
      • docker (11)
      • svelte (3)
      • 블록체인 (1)
      • IT (10) N
      • Rust (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 미디어로그
    • 위치로그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    API 설계
    jsp parameter
    에디터 팁
    vscode
    Node
    Extension Bisect
    ssl 인증서 발급
    자바 배열
    spring java
    vue cli
    mysql db
    lang rust
    자바 for문
    React Compiler
    docker
    자바 생성자
    gitlab 설치
    nginx ssl 설정
    vue 설치
    css list
    오류
    러스트
    클론코딩
    java casting
    티스토리 자동화
    브라우저 자동화
    nginx
    css perspective
    ubuntu
    자바 클래스
    nginx ssl 적용
    servlet class
    Java Array
    SSL 인증서
    css transition
    css float
    VS Code 팁
    jsp include
    proxy pass
    svelte
    css block
    자바 객체 지향
    Rust lang
    css table
    프런트엔드
    AI 코딩 에이전트
    리눅스 설치
    rust linux
    자바 exception
    css tb
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.6
밍글링글링
[SPRING] ch04. 로그인
상단으로

티스토리툴바