为什么我在我的导航栏上使用sec:authorize ="isAnonymous()"获得两个我的帐户


<ul class="nav navbar-nav navbar-right">
<li><a href="#">SHOPPING CART</a></li>
<li><a sec:authorize="isAnonymous()" th:href="@{/login}">MY ACCOUNT</a></li>
<li><a sec:authorize="isAuthenticated()" th:href="@{/myProfile}">MY ACCOUNT</a></li>
<li><a sec:authorize="isAuthenticated()" th:href="@{/logout}">LOGOUT</a></li>
</ul>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml" xmlns:sec="http://www.w3.org/1999/xhtml">
<head th:fragment="common-header">
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Le's Bookstore</title>
<!-- Bootstrap core CSS -->
<link href="/css/bootstrap.min.css" rel="stylesheet" />
<link href="/css/non-responsive.css" rel="stylesheet" />
<!-- Custom styles for this template -->
<link href="/css/style.css" rel="stylesheet" />
<link rel="icon" href="/image/applie-touch-icon.png" />
</head>
<body>
<div th:fragment="navbar">
<div class="page-top"
style="width: 100%; height: 20px; background-color: #f46b42;"></div>
<!-- Static navbar -->
<nav class="navbar navbar-default navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">LE'S BOOKSTORE</a>
</div>
<div id="navbar">
<ul class="nav navbar-nav navbar-left">
<li class="dropdown"><a href="#" class="dropdown-toggle"
data-toggle="dropdown" role="button" aria-haspopup="true"
aria-expanded="false">BOOKS <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Browse the bookshelf</a></li>
<li><a href="#">Store hours &#38; Directions</a></li>
<li><a href="#">FAQ</a></li>
</ul></li>
<form class="navbar-form">
<div class="form-group">
<input type="text" name="keyword" class="form-control"
placeholder="Book title" />
</div>
<button type="submit" class="btn btn-default">Search</button>
</form>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="#">SHOPPING CART</a></li>
<li><a sec:authorize="isAnonymous()" th:href="@{/login}">MY ACCOUNT</a></li>
<li><a sec:authorize="isAuthenticated()" th:href="@{/myProfile}">MY ACCOUNT</a></li>
<li><a sec:authorize="isAuthenticated()" th:href="@{/logout}">LOGOUT</a></li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
<!--/.container-fluid -->
</nav>
</div>

<div th:fragment="body-bottom-scripts">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
</div>
</body>
</html>

您的命名空间不正确。您可能需要添加百里香&springsecurity5如下所示:

<!DOCTYPE HTML>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<!-- head, body, etc -->
</html>

尝试使用!isAuthenticated((。像这样编辑你的代码:

<ul class="nav navbar-nav navbar-right">
<li><a href="#">SHOPPING CART</a></li>
<li><a sec:authorize="!isAuthenticated()" th:href="@{/login}">MY ACCOUNT</a></li>
<li><a sec:authorize="isAuthenticated()" th:href="@{/myProfile}">MY ACCOUNT</a></li>
<li><a sec:authorize="isAuthenticated()" th:href="@{/logout}">LOGOUT</a></li>
</ul>

最新更新