在 jsp 页面中引入 Shiro 的标签配置
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
Shiro JSP标签也不多:
一、Shiro JSP标签详解
1、guest标签 :验证当前用户是否为“访客”,即未认证(包含未记住)的用户,游客时才显示,登录用户不显示。
<shiro:guest>
游客你好: <a href = "login.jsp">请登录</a>
</shiro:guest>
2、user标签 :已认证通过或已记住的用户,登录后显示响应的内容。
<shiro:user>
欢迎[<shiro:principal/>]登录 <a href = "logout">退出</a>
</shiro:user>
3、authenticated标签 :已认证通过的用户,即 Subject.login 登录成功。不包含已记住的用户,这是与user标签的区别所在。
<shiro:authenticated>
用户[<shiro:principal/>] 已身份验证通过
</shiro:authenticated>
4、notAuthenticated标签 :未认证通过用户,即没有调用Subject.login进行登录,包括"记住我"也属于未进行身份验证。与authenticated标签相对应。与guest标签的区别是,该标签包含已记住用户。
<shiro:notAuthenticated>
<a href = "login.jsp">请登录</a>
</shiro:notAuthenticated>
5、principal 标签 :显示当前用户信息,通常为登录帐号信息。默认调用Subjec.getPrincipal()获取,即Primary Principal 。
你好,<shiro:principal property = "username"/>
6、hasRole标签 :验证当前用户是否属于该角色,如果当前Subject有角色将显示body体内的内容。
<shiro:hasRole name = "admin">
用户[<shiro:principal/>]拥有角色admin
</shiro:hasRole>
7、lacksRole标签 :与hasRole标签逻辑相反,当用户不属于该角色时验证通过,如果当前 Subjec没有角色将显示body体内的内容。
<shiro:lacksRole name = "admin">
用户[<shiro:principal/>]没有角色admin
</shiro:lacksRole>
8、hasAnyRole标签 :验证当前用户是否属于以下任意一个角色,如果Subject有任意一个角色(或的关系)将显示body体里的内容。
<shiro:hasAnyRoles name = "admin,user">
用户[<shiro:principal/>]拥有角色admin 或者 user
</shiro:hasAnyRoles>
9、hasPermission标签 :验证当前用户是否拥有指定权限,如果当前Subject有权限将显示body体内容。
<shiro:hasPermission name = "user:create">
用户[<shiro:principal/>] 拥有权限user:create
</shiro:hasPermission>
10、lacksPermission标签 :与hasPermission标签逻辑相反,当前用户没有制定权限时验证通过,如果当前Subject没有权限将显示body体内容。
<shiro:lacksPermission name = "org:create">
用户[<shiro:principal/>] 没有权限org:create
</shiro:lacksPermission>
二、简单使用:
1、登录页面:
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ssm_</title>
</head>
<body>
<h4>登录页面</h4>
<input type="hidden" id="uuidSalt" value="${uuidSalt }"><br>
<br>
<form action="${pageContext.request.contextPath }/login" method="post" id="loginForm" >
用户名:<input type="text" name="username"><br>
密 码:<input type="password" name="pazzword" id="pazzword"><br>
<button type="button" onclick="checkForm()">提交</button>
</form>
<shiro:guest>
游客你好: <a href = "${pageContext.request.contextPath }/login">请登录</a>
</shiro:guest>
<script src="${pageContext.request.contextPath }/static/jquery/jquery-1.12.4.js"></script>
<script src="${pageContext.request.contextPath }/static/md5/md5.js"></script>
<script src="${pageContext.request.contextPath }/static/aes/aes.js"></script>
<script src="${pageContext.request.contextPath }/static/aes/pad-zeropadding-min.js"></script>
<script type="text/javascript">
function checkForm(){
var uuidSalt = $("#uuidSalt").val();
var pazzword = $("#pazzword").val();
pazzword = hex_md5(pazzword);
//加密后的密码进行第二次加密(可解密)
pazzword = encrypt(pazzword,uuidSalt,uuidSalt);
$("#pazzword").val(pazzword);
//alert(pazzword.length);
if(pazzword.length == 44){
$("#loginForm").submit();
}
}
function encrypt(data,key,iv) { //key,iv:16位的字符串
var key1 = CryptoJS.enc.Latin1.parse(key);
var iv1 = CryptoJS.enc.Latin1.parse(iv);
return CryptoJS.AES.encrypt(data, key1, {iv:iv1,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.ZeroPadding}).toString();
}
</script>
</body>
</html>
2、登录成功后页面
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ssm_</title>
</head>
<body>
<h4>admin页面</h4><br>
<a href="${pageContext.request.contextPath }/logout">注销</a><br>
<a href="${pageContext.request.contextPath }/admin/userlist">用户列表</a><br>
<a href="${pageContext.request.contextPath }/admin/addUser">添加用户</a><br>
<shiro:guest>
游客你好: <a href = "${pageContext.request.contextPath }/login">请登录</a>
</shiro:guest>
<br>
<shiro:user>
欢迎[<shiro:principal/>]登录 <a href = "${pageContext.request.contextPath }/logout">退出</a>
</shiro:user>
<br>
<shiro:authenticated>
用户[<shiro:principal/>] 已身份验证通过
</shiro:authenticated>
<br>
你好,<shiro:principal property="username"/>
<br>
<shiro:hasRole name = "admin">
用户[<shiro:principal/>]拥有角色admin
</shiro:hasRole>
<br>
<shiro:hasAnyRoles name = "admin,user">
用户[<shiro:principal/>]拥有角色admin 或者 user
</shiro:hasAnyRoles>
<br>
<shiro:hasPermission name = "userlist">
用户[<shiro:principal/>] 拥有权限/admin/userlist
</shiro:hasPermission>
<br>
<shiro:lacksPermission name = "addUser">
用户[<shiro:principal/>] 没有权限/admin/addUser
</shiro:lacksPermission>
</body>
</html>
end ~