반응형

저번에는 회원가입 처리를 해보았다.

이제는 로그인 처리를 해보자 

 

로그인을 위해 

 타임리프 엑스트라스 스프링5 디펜던시 추가를 해준다.

 

<dependency>
   <groupId>org.thymeleaf.extras</groupId>
   <artifactId>thymeleaf-extras-springsecurity5</artifactId>
   <version>3.0.4.RELEASE</version>
</dependency>

 

 

를 추가 해준다.

 

그 후 https://www.thymeleaf.org/doc/articles/springsecurity.html 

 

Thymeleaf + Spring Security integration basics - Thymeleaf

Have you switched to Thymeleaf but your login and error pages are still using JSP? In this article we will see how to configure your Spring application to use Thymeleaf for login and error pages. All the code seen here comes from a working application. You

www.thymeleaf.org

를 참고 하여 

 

<div sec:authorize="isAuthenticated()">
  This content is only shown to authenticated users.
</div>
<div sec:authorize="hasRole('ROLE_ADMIN')">
  This content is only shown to administrators.
</div>
<div sec:authorize="hasRole('ROLE_USER')">
  This content is only shown to users.
</div>

를 이용하여 로그인 처리 및 로그아웃 처리를 해준다 !

 

<!DOCTYPE>

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security"
>
<head th:fragment="head(title)">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css" integrity="sha384-zCbKRCUGaJDkqS1kPbPd7TveP5iyJE0EjAuZQTgFLD2ylzuqKfdKlfG/eSrtxUkn" crossorigin="anonymous">
    <link href="starter-template.css" th:href="@{/css/starter-template.css}" rel="stylesheet">

    <title th:text="${title}"></title>
</head>
<body>

<nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top" th:fragment="menu(menu)">
    <a class="navbar-brand" href="#">Spring Boot</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="navbarsExampleDefault">
        <ul class="navbar-nav mr-auto">
            <li class="nav-item " th:classappend="${menu} == 'home' ? 'active'">
                <a class="nav-link" href="#"  th:href="@{/}">홈 <span class="sr-only" th:if="${menu} == 'home'">(current)</span></a>
            </li>
            <li class="nav-item" th:classappend="${menu} == 'board' ? 'active'">
                <a class="nav-link" href="#" th:href="@{/board/list}">게 시 판 <span class="sr-only" th:if="${menu} == 'board'">(current)</span> </a>
            </li>
        </ul>
            <a class="btn btn-secondary my-2 my-sm-0"  sec:authorize="!isAuthenticated()"th:href="@{/account/login}">Login</a>
             <a class="btn btn-secondary my-2  mx-2 my-sm-0"  sec:authorize="!isAuthenticated()"th:href="@{/account/register}">Sign Up</a>
        <form class="form-inline my-2 my-lg-0" sec:authorize="isAuthenticated()" th:action="@{/logout}" method="post">
            <span class="text-white" sec:authentication="name" >사용자</span>
            <span class="text-white mx-2" sec:authentication="principal.authorities" >권한</span>
            <button class="btn btn-secondary my-2 my-sm-0" type="submit">Logout</button>
        </form>
    </div>
</nav>

 

반응형

+ Recent posts