本文整理汇总了Java中org.apache.shiro.subject.Subject.hasRole方法的典型用法代码示例。如果您正苦于以下问题:Java Subject.hasRole方法的具体用法?Java Subject.hasRole怎么用?Java Subject.hasRole使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.shiro.subject.Subject
的用法示例。
在下文中一共展示了Subject.hasRole方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: hasAnyRoles
import org.apache.shiro.subject.Subject; //导入方法依赖的package包/类
/**
* 验证用户是否具有以下任意一个角色。
* @param roleNames 以 delimeter 为分隔符的角色列表
* @param delimeter 角色列表分隔符
* @return 用户是否具有以下任意一个角色
*/
public boolean hasAnyRoles(String roleNames, String delimeter) {
Subject subject = SecurityUtils.getSubject();
if (subject != null) {
if (delimeter == null || delimeter.length() == 0) {
delimeter = ROLE_NAMES_DELIMETER;
}
for (String role : roleNames.split(delimeter)) {
if (subject.hasRole(role.trim()) == true) {
return true;
}
}
}
return false;
}
示例2: checkSubjectRolesAndPermissions
import org.apache.shiro.subject.Subject; //导入方法依赖的package包/类
/**
* Check subject roles and permissions.
*
* @param currentUser the current user
* @throws FailedLoginException the failed login exception in case roles or permissions are absent
*/
protected void checkSubjectRolesAndPermissions(final Subject currentUser) throws FailedLoginException {
if (this.requiredRoles != null) {
for (final String role : this.requiredRoles) {
if (!currentUser.hasRole(role)) {
throw new FailedLoginException("Required role " + role + " does not exist");
}
}
}
if (this.requiredPermissions != null) {
for (final String perm : this.requiredPermissions) {
if (!currentUser.isPermitted(perm)) {
throw new FailedLoginException("Required permission " + perm + " does not exist");
}
}
}
}
示例3: checkSubjectRolesAndPermissions
import org.apache.shiro.subject.Subject; //导入方法依赖的package包/类
/**
* Check subject roles and permissions.
*
* @param currentUser the current user
* @throws FailedLoginException the failed login exception in case roles or permissions are absent
*/
protected void checkSubjectRolesAndPermissions(final Subject currentUser) throws FailedLoginException {
if (this.requiredRoles != null) {
for (final String role : this.requiredRoles) {
if (!currentUser.hasRole(role)) {
throw new FailedLoginException("Required role " + role + " does not exist");
}
}
}
if (this.requiredPermissions != null) {
for (final String perm : this.requiredPermissions) {
if (!currentUser.isPermitted(perm)) {
throw new FailedLoginException("Required permission " + perm + " cannot be located");
}
}
}
}
示例4: test
import org.apache.shiro.subject.Subject; //导入方法依赖的package包/类
@Test
public void test(){
log.info("My First Apache Shiro Application");
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
// get the currently executing user:
Subject currentUser = SecurityUtils.getSubject();
// Do some stuff with a Session (no need for a web or EJB container!!!)
Session session = currentUser.getSession();
session.setAttribute("someKey", "aValue");
String value = (String) session.getAttribute("someKey");
if (value.equals("aValue")) {
log.info("Retrieved the correct value! [" + value + "]");
}
// let's login the current user so we can check against roles and permissions:
if (!currentUser.isAuthenticated()) {
UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
token.setRememberMe(true);
try {
currentUser.login(token);
} catch (UnknownAccountException uae) {
log.info("There is no user with username of " + token.getPrincipal());
} catch (IncorrectCredentialsException ice) {
log.info("Password for account " + token.getPrincipal() + " was incorrect!");
} catch (LockedAccountException lae) {
log.info("The account for username " + token.getPrincipal() + " is locked. " +
"Please contact your administrator to unlock it.");
}
// ... catch more exceptions here (maybe custom ones specific to your application?
catch (AuthenticationException ae) {
//unexpected condition? error?
}
}
//say who they are:
//print their identifying principal (in this case, a username):
log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");
//test a role:
if (currentUser.hasRole("schwartz")) {
log.info("May the Schwartz be with you!");
} else {
log.info("Hello, mere mortal.");
}
//test a typed permission (not instance-level)
if (currentUser.isPermitted("lightsaber:weild")) {
log.info("You may use a lightsaber ring. Use it wisely.");
} else {
log.info("Sorry, lightsaber rings are for schwartz masters only.");
}
//a (very powerful) Instance Level permission:
if (currentUser.isPermitted("winnebago:drive:eagle5")) {
log.info("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'. " +
"Here are the keys - have fun!");
} else {
log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");
}
//all done - log out!
currentUser.logout();
}
示例5: isAccessAllowed
import org.apache.shiro.subject.Subject; //导入方法依赖的package包/类
@Override
protected boolean isAccessAllowed(ServletRequest request,
ServletResponse response, Object mappedValue) throws Exception {
String[] arra = (String[])mappedValue;
Subject subject = getSubject(request, response);
for (String role : arra) {
if(subject.hasRole(role)){
return true;
}
}
return false;
}
示例6: hasRole
import org.apache.shiro.subject.Subject; //导入方法依赖的package包/类
/**
* 验证用户是否具备某角色。
* @param role 角色名称
* @return 用户是否具备某角色
*/
public boolean hasRole(String role) {
Subject subject = SecurityUtils.getSubject();
return subject != null && subject.hasRole(role) == true;
}