java常用的邏輯

來源:互聯網
上載者:User

標籤:role   except   style   包含   資訊   The   isp   guest   either   

/**
* Copyright (c) 2015-2017, Chill Zhuang 莊騫 ([email protected]).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 com.stylefeng.guns.core.beetl;import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.beetl.core.GroupTemplate;import com.stylefeng.guns.core.shiro.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);
} /**
* 已認證通過的使用者。不包含已記住的使用者,這是與user標籤的區別所在。與notAuthenticated搭配使用
*
* @return 通過身分識別驗證:true,否則false
*/
public boolean authenticated() {
return getSubject() != null && getSubject().isAuthenticated();
} /**
* 未認證通過使用者,與authenticated標籤相對應。與guest標籤的區別是,該標籤包含已記住使用者。。
*
* @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()); }
}

java常用的邏輯

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.