天天看點

Apache Shiro學習筆記(八)Shiro的JSTL标簽

魯春利的工作筆記,好記性不如爛筆頭

Shiro 提供了JSTL标簽用于在JSP/GSP 頁面進行權限控制,如根據登入使用者顯示相應的頁面按鈕。

導入标簽庫

<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>      

标簽庫定義在shiro-web.jar包下的META-INF/shiro.tld 中定義。

Apache Shiro學習筆記(八)Shiro的JSTL标簽

自定義标簽tag-class定義

package org.apache.shiro.web.tags;

public class GuestTag extends SecureTag {

    //TODO - complete JavaDoc

    private static final Logger log = LoggerFactory.getLogger(GuestTag.class);

    public int onDoStartTag() throws JspException {
        if (getSubject() == null || getSubject().getPrincipal() == null) {
            if (log.isTraceEnabled()) {
                log.trace("Subject does not exist or does not have a known identity (aka 'principal').  " +
                        "Tag body will be evaluated.");
            }
            return TagSupport.EVAL_BODY_INCLUDE;
        } else {
            if (log.isTraceEnabled()) {
                log.trace("Subject exists or has a known identity (aka 'principal').  " +
                        "Tag body will not be evaluated.");
            }
            return TagSupport.SKIP_BODY;
        }
    }

}


package org.apache.shiro.web.tags;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

/**
 * @since 0.1
 */
public abstract class SecureTag extends TagSupport {

    //TODO - complete JavaDoc

    private static final Logger log = LoggerFactory.getLogger(SecureTag.class);

    public SecureTag() {
    }

    protected Subject getSubject() {
        return SecurityUtils.getSubject();
    }

    protected void verifyAttributes() throws JspException {
    }

    public int doStartTag() throws JspException {

        verifyAttributes();

        return onDoStartTag();
    }

    public abstract int onDoStartTag() throws JspException;
}      
Apache Shiro學習筆記(八)Shiro的JSTL标簽
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>未授權</title>
    </head>
    <body>
        <shiro:guest>
        歡迎遊客通路,<a href="${pageContext.request.contextPath}/login">登入</a>
        </shiro:guest>
        <shiro:user>
        歡迎[<shiro:principal/>]登入,<a href="${pageContext.request.contextPath}/logout">退出</a>
        </shiro:user>
    </body>
</html>      

guest标簽

<shiro:guest>
歡迎遊客通路,<a href="${pageContext.request.contextPath}/login.jsp">登入</a>
</shiro:guest>      

使用者沒有身份驗證時顯示相應資訊,即遊客通路資訊。

Apache Shiro學習筆記(八)Shiro的JSTL标簽

user标簽

<shiro:user>
歡迎[<shiro:principal/>]登入,<a href="${pageContext.request.contextPath}/logout">退出</a>
</shiro:user>      

使用者已經身份驗證/記住我登入後顯示相應的資訊。

Apache Shiro學習筆記(八)Shiro的JSTL标簽

authenticated标簽

<shiro:authenticated>
使用者[<shiro:principal/>]已身份驗證通過
</shiro:authenticated>      

使用者已經身份驗證通過,即Subject.login登入成功,不是記住我登入的。

notAuthenticated标簽

<shiro:notAuthenticated>
未身份驗證(包括記住我)
</shiro:notAuthenticated>      

使用者已經身份驗證通過,即沒有調用Subject.login進行登入,包括記住我自動登入的也屬于未進行身份驗證。

principal标簽

<shiro: principal/>      

顯示使用者身份資訊,預設調用Subject.getPrincipal()擷取,即Primary Principal。

<shiro:principal type="java.lang.String"/>      

相當于Subject.getPrincipals().oneByType(String.class)。

<shiro:principal type="java.lang.String"/>      
<shiro:principal property="username"/>      

相當于((User)Subject.getPrincipals()).getUsername()。

hasRole标簽

<shiro:hasRole name="admin">
使用者[<shiro:principal/>]擁有角色admin<br/>
</shiro:hasRole>      

如果目前Subject有admin角色。

hasAnyRoles标簽

<shiro:hasAnyRoles name="admin,user">
使用者[<shiro:principal/>]擁有角色admin 或user<br/>
</shiro:hasAnyRoles>      

如果目前Subject有任意一個角色(或的關系)。

lacksRole标簽

<shiro:lacksRole name="abc">
使用者[<shiro:principal/>]沒有角色abc<br/>
</shiro:lacksRole>      

沒有角色abc将執行的代碼。

hasPermission标簽

<shiro:hasPermission name="user:create">
使用者[<shiro:principal/>]擁有權限user:create<br/>
</shiro:hasPermission>      

判斷是否有指定的權限(有權限才能。。。)。

lacksPermission标簽

<shiro:lacksPermission name="org:create">
使用者[<shiro:principal/>]沒有權限org:create<br/>
</shiro:lacksPermission>      

判斷是否有指定的權限(無權限才能。。。)。

導入自定義标簽庫

<%@taglib prefix="lucl" tagdir="/WEB-INF/tags" %>      
<lucl:hasAnyPermissions name="user:create,abc:update">
使用者[<shiro:principal/>]擁有權限user:create或abc:update<br/>
</lucl:hasAnyPermissions>