From 02e5c8e479a2e9850414221dc0c78d44f2f5fd9a Mon Sep 17 00:00:00 2001 From: Calvin Date: Fri, 24 Aug 2012 12:06:22 +0800 Subject: [PATCH] =?UTF-8?q?#78=20Shiro=E4=BD=BF=E7=94=A8=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E3=80=82=203.=E6=8F=90=E4=BE=9BShiro=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E6=8F=90=E4=BE=9B=E7=9A=84HasAnyPermissionsTag?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/webapp/WEB-INF/views/index.jsp | 4 +- .../webapp/WEB-INF/views/story/security.jsp | 1 + modules/core/pom.xml | 14 ++ .../security/shiro/HasAnyPermissionsTag.java | 36 ++++ .../resources/META-INF/springside-shiro.tld | 180 ++++++++++++++++++ 5 files changed, 234 insertions(+), 1 deletion(-) create mode 100644 modules/core/src/main/java/org/springside/modules/security/shiro/HasAnyPermissionsTag.java create mode 100644 modules/core/src/main/resources/META-INF/springside-shiro.tld diff --git a/examples/showcase/src/main/webapp/WEB-INF/views/index.jsp b/examples/showcase/src/main/webapp/WEB-INF/views/index.jsp index 35ead769..5dd96e6e 100644 --- a/examples/showcase/src/main/webapp/WEB-INF/views/index.jsp +++ b/examples/showcase/src/main/webapp/WEB-INF/views/index.jsp @@ -15,7 +15,9 @@

What is new?

\ No newline at end of file diff --git a/examples/showcase/src/main/webapp/WEB-INF/views/story/security.jsp b/examples/showcase/src/main/webapp/WEB-INF/views/story/security.jsp index 410dabf5..284afdd9 100644 --- a/examples/showcase/src/main/webapp/WEB-INF/views/story/security.jsp +++ b/examples/showcase/src/main/webapp/WEB-INF/views/story/security.jsp @@ -13,6 +13,7 @@

安全演示

Shiro高级演示


diff --git a/modules/core/pom.xml b/modules/core/pom.xml index 6e299f84..90b7447e 100644 --- a/modules/core/pom.xml +++ b/modules/core/pom.xml @@ -26,6 +26,20 @@ true + + + + org.apache.shiro + shiro-web + true + + + + org.eclipse.jetty + jetty-jsp + true + + diff --git a/modules/core/src/main/java/org/springside/modules/security/shiro/HasAnyPermissionsTag.java b/modules/core/src/main/java/org/springside/modules/security/shiro/HasAnyPermissionsTag.java new file mode 100644 index 00000000..b9e32698 --- /dev/null +++ b/modules/core/src/main/java/org/springside/modules/security/shiro/HasAnyPermissionsTag.java @@ -0,0 +1,36 @@ +package org.springside.modules.security.shiro; + +import org.apache.shiro.subject.Subject; +import org.apache.shiro.web.tags.PermissionTag; + +/** + * 不知为何Shiro一直不提供HasAnyPermissions的Tag. + * + * @author calvin + */ +public class HasAnyPermissionsTag extends PermissionTag { + + private static final String PERMISSION_NAMES_DELIMETER = ","; + + @Override + protected boolean showTagBody(String permissionNames) { + boolean hasAnyPermission = false; + + Subject subject = getSubject(); + + if (subject != null) { + // Iterate through permissions and check to see if the user has one of the permissions + for (String permission : permissionNames.split(PERMISSION_NAMES_DELIMETER)) { + + if (subject.isPermitted(permission.trim())) { + hasAnyPermission = true; + break; + } + + } + } + + return hasAnyPermission; + } + +} diff --git a/modules/core/src/main/resources/META-INF/springside-shiro.tld b/modules/core/src/main/resources/META-INF/springside-shiro.tld new file mode 100644 index 00000000..ccb0cfbe --- /dev/null +++ b/modules/core/src/main/resources/META-INF/springside-shiro.tld @@ -0,0 +1,180 @@ + + + + + + + 1.1.2 + + 1.2 + + Apache Shiro + + http://www.springside.org.cn/tags/shiro + + Apache Shiro JSP Tag Library. + + + hasPermission + org.apache.shiro.web.tags.HasPermissionTag + JSP + Displays body content only if the current Subject (user) + 'has' (implies) the specified permission (i.e the user has the specified ability). + + + name + true + true + + + + + hasAnyPermissions + org.springside.modules.security.shiro.HasAnyPermissionsTag + JSP + Displays body content only if the current user has one of the specified permissions from a + comma-separated list of permission names. + + + name + true + true + + + + + lacksPermission + org.apache.shiro.web.tags.LacksPermissionTag + JSP + Displays body content only if the current Subject (user) does + NOT have (not imply) the specified permission (i.e. the user lacks the specified ability) + + + name + true + true + + + + + hasRole + org.apache.shiro.web.tags.HasRoleTag + JSP + Displays body content only if the current user has the specified role. + + name + true + true + + + + + + hasAnyRoles + org.apache.shiro.web.tags.HasAnyRolesTag + JSP + Displays body content only if the current user has one of the specified roles from a + comma-separated list of role names. + + + name + true + true + + + + + lacksRole + org.apache.shiro.web.tags.LacksRoleTag + JSP + Displays body content only if the current user does NOT have the specified role + (i.e. they explicitly lack the specified role) + + + name + true + true + + + + + authenticated + org.apache.shiro.web.tags.AuthenticatedTag + JSP + Displays body content only if the current user has successfully authenticated + _during their current session_. It is more restrictive than the 'user' tag. + It is logically opposite to the 'notAuthenticated' tag. + + + + + notAuthenticated + org.apache.shiro.web.tags.NotAuthenticatedTag + JSP + Displays body content only if the current user has NOT succesfully authenticated + _during their current session_. It is logically opposite to the 'authenticated' tag. + + + + + user + org.apache.shiro.web.tags.UserTag + JSP + Displays body content only if the current Subject has a known identity, either + from a previous login or from 'RememberMe' services. Note that this is semantically different + from the 'authenticated' tag, which is more restrictive. It is logically + opposite to the 'guest' tag. + + + + + guest + org.apache.shiro.web.tags.GuestTag + JSP + Displays body content only if the current Subject IS NOT known to the system, either + because they have not logged in or they have no corresponding 'RememberMe' identity. It is logically + opposite to the 'user' tag. + + + + + principal + org.apache.shiro.web.tags.PrincipalTag + JSP + Displays the user's principal or a property of the user's principal. + + type + false + true + + + property + false + true + + + defaultValue + false + true + + + + \ No newline at end of file