일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- Oracle
- 단축키
- Android
- 맥
- FastAPI
- 배열
- nodejs
- ibatis
- Error
- 반복문
- 자바
- Homebrew
- Mac
- 자바문제
- servlet
- pyqt
- API
- 이클립스
- 컬렉션프레임워크
- ddit
- 객체지향
- 대덕인재개발원
- spring
- jsp
- 생활코딩
- python
- crud
- JDBC
- Java
- html
Archives
- Today
- Total
romworld
JSP 20 - 쇼핑몰 만들기(7) [상품 등록 - (security 권한 설정 -admin)] 본문
<tomcat-users.xml>
<role rolename="admin" /> <!-- 상품관리자 -->
<user username="ssr" password="java" roles="admin"/><!-- 상품관리자 -->
권한을 줄 username , password, roles를 설정한다.
<web.xml>
<security-role>
<role-name>admin</role-name>
</security-role>
<!--
상품을 추가할 수 있는 URL인 /addProduct.jsp에 접근하려면
admin이라는 권한이 있어야함.
admin이라는 권한을 갖고 있는 user는 gdi 회원이 있음
-->
<security-constraint>
<web-resource-collection>
<web-resource-name>JSPBook</web-resource-name>
<url-pattern>/addProduct.jsp</url-pattern>
<http-method>GET</http-method>
</web-resource-collection>
<auth-constraint>
<description></description>
<role-name>admin</role-name>
</auth-constraint>
</security-constraint>
admin 권한을 가져야지만 접속할 수 있다.
<login.jsp>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/css/bootstrap.min.css"/>
<title>Login</title>
</head>
<body>
<!-- include 액션 태그 -->
<jsp:include page="menu.jsp" />
<div class="jumbotron">
<div class="container">
<h1 class="display-3">로그인</h1>
</div>
</div>
<div class="container" align="center">
<div class="clo-md-4 col-md-offset-4">
<h3 class="form-signin-heading">Please Sign In</h3>
<!-- /ch01/security01.jsp에 접근 시 통과해야 할 관문 -->
<form class="form-signin" name="loginForm" action="j_security_check" method="post">
<div class="form-group">
<label for="inputUserName" class="sr-only">User Name</label>
<input type="text" name="j_username"
class="form-control" placeholder="ID"
required autofocus />
</div>
<div class="form-group">
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" name="j_password"
class="form-control" placeholder="Password"
required />
</div>
<input type="submit" class="btn btn btn-lg btn-success btn-block"
value="로그인"/>
</form>
</div>
</div>
<!-- footer -->
<jsp:include page="footer.jsp" />
</body>
</html>
**url창에서 [localhost/addProduct.jsp] 주소를 입력
상품 등록 페이지로 이동이 된다.
로그인 실패 시
<login_failed.jsp>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
//로그인 인증 실패 시 강제 이동
//response 내장 객체.
//error=1 요청 파라미터
response.sendRedirect("login.jsp?error=1");
%>
<login.jsp> 추가
<div class="container" align="center">
<div class="clo-md-4 col-md-offset-4">
<!-- ?error=1 요청 파라미터(param) -->
<c:if test="${param.error =='1' }">
<h3 class="form-signin-heading">아이디/비밀번호를 확인해주세요</h3>
</c:if>
<c:if test="${param.error !='1' }">
<h3 class="form-signin-heading">Please Sign In</h3>
</c:if>
c:if 를 추가해서 아이디/비밀번호를 잘못 쳤을 경우
<h3 class="form-signin-heading">아이디/비밀번호를 확인해주세요</h3>
정상 입력했을 경우
<h3 class="form-signin-heading">Please Sign In</h3>
다시 로그인 페이지로 돌아가게 된다.
로그인 했을 때
헤더 상단 오른쪽에 시큐리티의 사용자명 가져오기
[jsp:include로 삽입했던 헤더 메뉴(menu.jsp) 수정]
function 사용을 위한 taglib 추가.
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<menu.jsp>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
// 시큐리티의 사용자명을 가져옴
String username = request.getRemoteUser();
%>
<!-- JAVA세계의 변수 username의 값을 JSTL세계의 변수 username에 할당 -->
<c:set var="username" value="<%=username %>"></c:set>
<nav class="navbar navbar-expand navbar-dark bg-dark">
<!-- container : 내 안에 내용 있다.. -->
<div class="container">
<div class="navbar-header" style="width:100%;">
<div style="float:left;">
<a class="navbar-brand" href="welcome.jsp">Home</a>
</div>
<div style="float:right;">
<span class="navbar-brand">
<!-- 로그인 했음 -->
<c:if test="${fn:length(username) >0}">
${username}님 환영합니다.
</c:if>
<!-- 로그인 했함 -->
<c:if test="${fn:length(username) == 0 }">
로그인해주세요.
</c:if>
</span>
</div>
</div>
</div>
</nav>
JSTL을 이용해서 값을 꺼내준다.
결과
로그아웃
헤더 상단에 로그아웃 버튼을 만들어 준다.
<menu.jsp>
<a href="/logout.jsp" class="btn btn-sm btn-success pull-right">logout</a>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
// 시큐리티의 사용자명을 가져옴
String username = request.getRemoteUser();
%>
<!-- JAVA세계의 변수 username의 값을 JSTL세계의 변수 username에 할당 -->
<c:set var="username" value="<%=username %>"></c:set>
<nav class="navbar navbar-expand navbar-dark bg-dark">
<!-- container : 내 안에 내용 있다.. -->
<div class="container">
<div class="navbar-header" style="width:100%;">
<div style="float:left;">
<a class="navbar-brand" href="welcome.jsp">Home</a>
</div>
<div style="float:right;">
<span class="navbar-brand">
<!-- 로그인 했음 -->
<c:if test="${fn:length(username) >0}">
${username}님 환영합니다. |
<a href="/logout.jsp" class="btn btn-sm btn-success pull-right">logout</a>
</c:if>
<!-- 로그인 했함 -->
<c:if test="${fn:length(username) == 0}">
로그인해주세요.
</c:if>
</span>
</div>
</div>
</div>
</nav>
<logout.jsp>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
//로그인 인증을 할 때 웹 브라우저에 저장된 모든 사용자를 삭제
session.invalidate();
//로그인 페이지로 강제 이동
// login.jsp로 바로 보내지 않는 이유는 login.jsp의 action은
// check용도이므로.
response.sendRedirect("/addProduct.jsp");
%>
'WEB > JSP' 카테고리의 다른 글
JSP 22 - 쇼핑몰 만들기(8) (예외 처리 하기) (0) | 2023.01.13 |
---|---|
JSP 21 - 예외 처리(Exception) (1) | 2023.01.13 |
JSP 19 - 시큐리티(security) (0) | 2023.01.11 |
JSP 18 - 쇼핑몰 사이트 만들기(6) [상품 등록 - (Bundle 사용)] (0) | 2023.01.11 |
JSP 17 - Properties File Editor (다국어 처리) ,JSTL fmt 활용 (0) | 2023.01.11 |
Comments