본문 바로가기
개발/Security

[SpringBoot] Security 로그인 인증, 인가(7) - html

by 코딩하는 흰둥이 2023. 4. 30.
반응형

이전글 https://greed-yb.tistory.com/228

 

[SpringBoot] Security 로그인 인증, 인가(6) - Service

이전글 https://greed-yb.tistory.com/227 [SpringBoot] Security 로그인 인증, 인가(5) - Vo , Mapper 이전글 https://greed-yb.tistory.com/226 [SpringBoot] Security 로그인 인증, 인가(4) - AuthProvider 이전글 https://greed-yb.tistory.com/22

greed-yb.tistory.com

 

  • Main 화면 : home
  • login 화면 : loginPage
  • 회원가입 화면 : signupPage
  • 회원정보 수정 화면 : editPage
  • 권한이 없을 때 화면 : dinied
  • 권한 테스트용 임시 화면 : BoardCreate  <- 이건 아무거나 만들어서 하면 됨 그냥 html 파일만 들어도 가능 

 

home.html (메인)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Home</title>
</head>
<body>
<h2><span th:text="${user.id}"/> 님의 회원 정보</h2>
<p>name: <span th:text="${user.name}"/></p>
<p>password: <span th:text="${user.password}"/></p>

<button type="button" th:onclick="|location.href='@{update}'|">수정하기</button>

<form th:action="@{/logout}" method="post">
    <button type="submit">로그아웃</button>
</form>
</body>
</html>

 

 

loginPage.html ( 로그인)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login</title>
</head>
<body>
<form th:action="@{/login}" method="post">
  <h2>로그인</h2>
  <div>
    <input type="text" name="id" placeholder="id"/>
  </div>
  <div>
    <input type="password" name="password" placeholder="Password"/>
  </div>

  <button type="submit">로그인</button>
  <button type="button" th:onclick="|location.href='@{/signup}'|">회원가입</button>
</form>
</body>
</html>

login form 의 idpassword 의 명칭이 

WebSecurityConfig 의 formLogin() 의 파라미터명과 일치해야한다 

개인이 만든 loginPage을 이용하지 않고 기본 login 화면을 이용한다면

Id의 기본파라미터 값은 username 이다 login form 의 id 값도 username 으로 변경해야한다 참고할 것

 

signupPage.html (회원가입)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Sign Up</title>
</head>
<body>
<h2>회원가입</h2>
<form th:action="@{/signup}" method="post">
    <div>
        <input type="text" name="id" placeholder="id"/>
    </div>
  <div>
    <input type="text" name="name" placeholder="Name"/>
  </div>
  <div>
    <input type="password" name="password" placeholder="Password"/>
  </div>
  <button type="submit">회원가입</button>
</form>
</body>
</html>

 

 

editPage.html (회원 정보 수정)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Modify Information</title>
</head>
<body>
<h2>회원 정보 수정</h2>
<form th:action="@{/update}" method="post">
  <p>Id<br>
    <input type="text" name="id" th:value="${user.id}" readonly/>
  </p>
  <p>Name<br>
    <input type="text" name="name" th:value="${user.name}"/>
  </p>
  <p>Password<br>
    <input type="password" name="password" placeholder="Password를 입력해주세요"/>
  </p>
  <button type="submit">저장하기</button>
</form>
</body>
</html>

 

 

dinied.html (권한이 없을때)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Error</title>
</head>
<body>
<h1><span th:text="${id}" class="alert alert-danger"/>님은 권한이 없습니다. 관리자에게 문의해주세요.</h1>
<h3 th:text="${exception}"></h3>
</body>
</html>

 

 

임시.html (권한이 없는 페이지를 접속 할 때 해당 페이지가 출력되는지 확인용)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Board_Create</title>
</head>
<body>
글 작성 페이지
<div>
    <form name="form" method="post" action="/board/create">
        이름 : <input name="b_name"></p>
        제목 : <input name="b_title"></p>
        내용 : <textarea name="b_content" rows="30" cols="50"></textarea>
        <div>
            <button type="submit">버튼</button>
        </div>
    </form>
</div>

</body>
</html>

현재 소스대로 진행한다면 회원가입한 유저들은 

모두 "ROLE_USER" 라는 권한을 갖고 있다

임시.html 페이지를 WebSecurityConfig 에서 "ADMIN" 의 권한을 주었을 때

해당 페이지로 접속이 되는지 확인하기 위한 테스트 화면이다

댓글