Common logic in Java

Source: Internet
Author: User

/**
* Copyright (c) 2015-2017, Chill Zhuang Zhong ([email protected]).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* You are not a use this file except in compliance with the License.
* Obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable or agreed to writing, software
* Distributed under the License is distributed on a "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 = ","; /**
* Get current Subject
*
* @return Subject
*/
protected static Subject Getsubject () {
return Securityutils.getsubject ();
} /**
* Get the encapsulated Shirouser
*
* @return Shirouser
*/
Public Shirouser GetUser () {
if (Isguest ()) {
return null;
} else {
Return (Shirouser) Getsubject (). Getprincipals (). Getprimaryprincipal ();
}
} /**
* Verify that the current user is part of the role? , used in conjunction with Lacksrole
*
* @param roleName role name
* @return belongs to this role: true, otherwise false
*/
public boolean hasrole (String roleName) {
return getsubject () = null && RoleName! = null
&& rolename.length () > 0 && getsubject (). Hasrole (RoleName);
} /**
* In contrast to the hasrole tag logic, validation passes when the user does not belong to the role.
*
* @param roleName role name
* @return does not belong to this role: true, otherwise false
*/
public boolean lacksrole (String roleName) {
Return!hasrole (RoleName);
} /**
* Verify that the current user is part of any of the following roles.
*
* @param rolenames Role List
* @return belongs to: true, otherwise 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;
} /**
* Verify that the current user is part of all of the following roles.
*
* @param rolenames Role List
* @return belongs to: true, otherwise 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;
} /**
* Verify that the current user has the specified permission and use it with lackspermission
*
* @param permission Permission name
* @return has permission: true, otherwise false
*/
public boolean haspermission (String permission) {
return getsubject () = NULL && permission! = NULL
&& permission.length () > 0
&& getsubject (). ispermitted (permission);
} /**
* In contrast to the haspermission tag logic, the current user does not have permission when the validation passes.
*
* @param permission Permission name
* @return has permission: true, otherwise false
*/
public boolean lackspermission (String permission) {
return!haspermission (permission);
} /**
* Approved by the user. Does not contain the remembered user, which is the difference from the user tag. Paired with notauthenticated
*
* @return Authenticated: true, otherwise false
*/
public Boolean authenticated () {
Return Getsubject ()! = null && getsubject (). IsAuthenticated ();
} /**
* Not certified by user, corresponds to authenticated tag. The difference from the guest tag is that the label contains the user that has been remembered.
*
* @return Not authenticated: true, otherwise false
*/
public Boolean notauthenticated () {
return!authenticated ();
} /**
* Authentication passed or remembered by the user. Used in conjunction with the Guset.
*
* @return User: true, otherwise false
*/
public Boolean isuser () {
Return Getsubject ()! = null && getsubject (). Getprincipal ()! = NULL;
} /**
* Verify that the current user is a "guest", that is, a user who is not certified (contains not remembered). Use with user
*
* @return Visitor: true, otherwise false
*/
public Boolean isguest () {
return!isuser ();
} /**
* Output Current user information, usually login account information.
*
* @return Current User Information
*/
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 ()); }
}

Common logic in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.