Java tutorial
/** * Copyright (c) 2015-2017, Chill Zhuang (smallchill@163.com). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.com.xl.core.beetl.func; import org.apache.shiro.SecurityUtils; import org.apache.shiro.subject.Subject; import org.beetl.core.GroupTemplate; import cn.com.xl.common.vo.ShiroUser; public class ShiroExt { private static final String NAMES_DELIMETER = ","; /** * ?? Subject * * @return Subject */ protected static Subject getSubject() { return SecurityUtils.getSubject(); } /** * ?? ShiroUser * * @return ShiroUser */ public ShiroUser getUser() { if (isGuest()) { return null; } else { return (ShiroUser) getSubject().getPrincipals().getPrimaryPrincipal(); } } /** * ???,lacksRole ?? * * @param roleName * ?? * @return true?false */ public boolean hasRole(String roleName) { return getSubject() != null && roleName != null && roleName.length() > 0 && getSubject().hasRole(roleName); } /** * hasRole???? * * @param roleName * ?? * @return ?true?false */ public boolean lacksRole(String roleName) { return !hasRole(roleName); } /** * ???? * * @param roleNames * * @return :true,?false */ public boolean hasAnyRoles(String roleNames) { boolean hasAnyRole = false; Subject subject = getSubject(); if (subject != null && roleNames != null && roleNames.length() > 0) { for (String role : roleNames.split(NAMES_DELIMETER)) { if (subject.hasRole(role.trim())) { hasAnyRole = true; break; } } } return hasAnyRole; } /** * ??? * * @param roleNames * * @return :true,?false */ public boolean hasAllRoles(String roleNames) { boolean hasAllRole = true; Subject subject = getSubject(); if (subject != null && roleNames != null && roleNames.length() > 0) { for (String role : roleNames.split(NAMES_DELIMETER)) { if (!subject.hasRole(role.trim())) { hasAllRole = false; break; } } } return hasAllRole; } /** * ?????,lacksPermission ?? * * @param permission * ???? * @return ??true?false */ public boolean hasPermission(String permission) { return getSubject() != null && permission != null && permission.length() > 0 && getSubject().isPermitted(permission); } /** * hasPermission?????? * * @param permission * ???? * @return ??true?false */ public boolean lacksPermission(String permission) { return !hasPermission(permission); } /** * ????usernotAuthenticated?? * * @return ?true?false */ public boolean authenticated() { return getSubject() != null && getSubject().isAuthenticated(); } /** * ?authenticatedguest?? * * @return ?true?false */ public boolean notAuthenticated() { return !authenticated(); } /** * ??guset?? * * @return true? false */ public boolean isUser() { return getSubject() != null && getSubject().getPrincipal() != null; } /** * ????????user?? * * @return true?false */ public boolean isGuest() { return !isUser(); } /** * ????? * * @return ?? */ public String principal() { if (getSubject() != null) { Object principal = getSubject().getPrincipal(); return principal.toString(); } return ""; } public static void main(String[] args) { GroupTemplate gt = new GroupTemplate(); gt.registerFunctionPackage("shiro", new ShiroExt()); } }