【发布时间】:2019-06-20 10:33:51
【问题描述】:
我有一个 Spring Boot 应用程序,我在其中实现了基于角色的身份验证。
最终用户通过身份验证后,应根据角色加载数据库中的菜单。
一旦成功验证,我将从 thymeleaf 页面中提取角色并执行加载菜单。
即使当我打印像alert("#hide").text() 这样的角色值时,它仍然可以正常工作,它会提醒角色名称。
要加载菜单,我应该知道哪个用户已登录,以便可以加载正确的菜单。
这就是为什么我要提取值并像“管理员”、“用户”等进行比较。
但是比较不会发生
主页.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta http-equiv="X-UA-Compatible" content="ie=edge"/>
<title>BSNL | CC</title>
<link rel="stylesheet" type="text/css" media="screen, projection"
href="/static/css/bootstrap.css"
th:href="@{/css/bootstrap.css}" />
<link rel="stylesheet" type="text/css" media="screen, projection"
href="/static/css/bootstrap.css"
th:href="@{/css/menu.css}" />
</head>
<body>
<div id="includedContent"></div>
Logged in user: <b th:inline="text" class="user" id="hide"> [[${#httpServletRequest.remoteUser}]] </b>
<input type="hidden" class="order-entry" id="ID" placeholder="" th:value="${#httpServletRequest.remoteUser}" value="${#httpServletRequest.remoteUser}"/>
<script th:src="@{/js/jquery-3.4.1.js}"></script>
<script th:src="@{/js/bootstrap.js}"></script>
<script th:src="@{/js/custom.js}"></script>
<script th:src="@{/js/home.js}"></script>
</body>
</html>
注意:${#httpServletRequest.remoteUser}spring 代码查看谁登录了,比如管理员、用户或超级管理员。
Home.js
$(document).ready(function(){
$("#includedContent").load("http://localhost:8080/BSNLCC-BackendWIthSecurity/home/");
alert($("#hide").text()); //printing fine
if($("#hide").text() == "null"){ //comparing not happening tried with ==== also
alert(true);
}
/*
if($("#rle").text() == "admin"){
loadAdminMenu();
}else if($("#ID").text() == "superadmin"){
loadSuperAdminMenu();
}
function loadAdminMenu(){
alert($("#rle").text());
$.ajax({
url: 'home/load/menu',
type: 'GET',
contentType : 'application/json; charset=utf-8',
async: false,
data: {
role: $("#rle").text()
},
cache: false,
datatype: 'json',
success: function(data) {
alert("success");
},
error: function(err){
console.log(err);
}
});
}*/
});
【问题讨论】:
-
您的文本实际上是否打算将“null”作为字符串说?要与 null 进行比较,您不需要 null 周围的 ""。
标签: jquery ajax spring spring-security