您的位置:首页 > 其它

shiro授权

2016-06-28 21:22 405 查看

授权流程






三种授权方法

Shiro 支持三种方式的授权:

n 编程式:通过写if/else
授权代码块完成:

Subject subject = SecurityUtils.getSubject();

if(subject.hasRole(“admin”)) {

//有权限

} else {

//无权限

}

n 注解式:通过在执行的Java方法上放置相应的注解完成:

@RequiresRoles("admin")

public void hello() {

//有权限

}

n JSP/GSP 标签:在JSP/GSP
页面通过相应的标签完成:

<shiro:hasRole name="admin">

<!— 有权限—>

</shiro:hasRole>

shiro-permission.ini

shiro-permission.ini里边的内容相当于在数据库。

#用户
[users]
#用户zhang的密码是123,此用户具有role1和role2两个角色
zhangsan=123,role1,role2
wangwu=123,role2

#权限
[roles]
#角色role1对资源user拥有create、update权限
role1=user:create,user:update
#角色role2对资源user拥有create、delete权限
role2=user:create,user:delete
#角色role3对资源user拥有create权限
role3=user:create

权限标识符号规则:资源:操作:实例(中间使用半角:分隔)

user:create:01  表示对用户资源的01实例进行create操作。

user:create:表示对用户资源进行create操作,相当于user:create:*,对所有用户资源实例进行create操作。

user:*:01  表示对用户资源实例01进行所有操作。



4程序测试

//角色授权、资源授权测试
@Test
public void testAuthorization(){
//创建SecurityManager工厂
Factory<SecurityManager> factory =new IniSecurityManagerFactory("classpath:shiro-permission.ini");

//创建SecurityManager
SecurityManager securityManager = factory.getInstance();

//将SecurityManager设置到系统运行环境,一般单例管理
SecurityUtils.setSecurityManager(securityManager);

//创建subject
Subject subject = SecurityUtils.getSubject();

//创建token令牌
UsernamePasswordToken token = new UsernamePasswordToken("zhangsan", "123");

//执行认证
try {
subject.login(token);
} catch (Exception e) {
e.printStackTrace();
}

System.out.println("认证状态:"+subject.isAuthenticated());

//认证通过后执行授权

//基于角色的授权
//hasRole传入角色标识
boolean ishasRole1 = subject.hasRole("role1");
System.out.println("单个角色判断:"+ishasRole1);

//hasAllRoles是否拥有多个角色
boolean hasAllRoles = subject.hasAllRoles(Arrays.asList("role1","role2"));
System.out.println("多个角色判断:"+hasAllRoles);

//使用check方法进行授权,如果授权不通过会抛出异常
subject.checkRole("role1");

//基于资源的授权
boolean permitted = subject.isPermitted("user:create:1");
System.out.println("单个权限判断:"+permitted);

boolean isPermittedAll = subject.isPermittedAll("user:create:1",
"user:delete");
System.out.println("多个权限判断" + isPermittedAll);

// 使用check方法进行授权,如果授权不通过会抛出异常
subject.checkPermission("items:create:1");
}


5.自定义realm进行资源授权测试
<span style="font-size:14px;">package com.me.shiro.realm;

import java.util.ArrayList;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
* 自定义realm
* @author Administrator
*
*/
public class CustomRealm extends AuthorizingRealm{

@Override
public void setName(String name) {
// TODO Auto-generated method stub
super.setName("customRealm");
}
//用于认证
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//token是用户输入的
//第一步从token中取出身份信息
String userCode = (String) token.getPrincipal();

//第二步:根据用户输入的userCode从数据库查询
//...

//如果查询不到返回null
//数据库中用户帐号是zhangsan
if(!"zhangsan".equals(userCode)){
return null;
}

//模拟从数据库查询到密码
String password = "111111";

//如果查询到返回认证信息AuthenticationInfo
SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(userCode, password, this.getName());

return authenticationInfo;
}

//用于授权
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
// TODO Auto-generated method stub
//从principals获取主身份信息
//将getPrimaryPrincipal方法返回值转为真实身份类型
//(在上边doGetAuthenticationInfo认证通过填充到SimpleAuthenticationInfo中的身份类型)
String userCode = (String) principals.getPrimaryPrincipal();

//根据身份信息获取权限信息
//连接数据库.....
//模拟从数据库获取到数据
ArrayList<String> permissions= new ArrayList<>();
permissions.add("user:create");//用户的创建
permissions.add("items:add");//商品添加权限
//...

//将查询到授权信息填充到simpleAuthorizationInfo对象中
SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
simpleAuthorizationInfo.addStringPermissions(permissions);

//返回授权信息
return simpleAuthorizationInfo;

}

}</span><span style="font-size: 16pt;">
</span>



shiro-realm.ini

在shiro-realm.ini中配置自定义的realm,将realm设置到securityManager中

[main]
#自定义 realm
customRealm=com.me.shiro.realm.CustomRealm
#将realm设置到securityManager,相当 于spring中注入
securityManager.realms=$customRealm




测试
</pre><pre name="code" class="html">// 自定义realm进行资源授权测试
@Test
public void testAuthorizationCustomRealm(){
//创建securityManager工厂,通过ini配置文件创建securityManager工厂
Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-realm.ini");

//创建SecurityManager
org.apache.shiro.mgt.SecurityManager securityManager= factory.getInstance();

//将secrityManager设置当前的运行环境中
SecurityUtils.setSecurityManager(securityManager);

//从SecurityUtils里创建一个subject
Subject subject = SecurityUtils.getSubject();

//在认证提交前准备token(令牌)
//这里的帐号和密码 将来是由用户输入进去
UsernamePasswordToken token = new UsernamePasswordToken("zhangsan", "111111");

try {
//执行认证提交
subject.login(token);
} catch (Exception e) {
e.printStackTrace();
}

//是否认证通过
boolean isAuth = subject.isAuthenticated();
System.out.println("是否认证通过:"+isAuth);

//认证通过后执行授权
//基于资源的授权,调用
boolean permitted = subject.isPermitted("user:create");
System.out.println("单个权限判断:"+permitted);

boolean permittedAll = subject.isPermittedAll("user:create","user:add");
System.out.println("多个权限判断:"+permittedAll);

//使用check方法进行授权测试,如果授权不通过会抛出异常
subject.checkPermission("items:del");
}

授权流程

 

1、对subject进行授权,调用方法isPermitted("permission串")

2、SecurityManager执行授权,通过ModularRealmAuthorizer执行授权

3、ModularRealmAuthorizer执行realm(自定义的CustomRealm)从数据库查询权限数据

调用realm的授权方法:doGetAuthorizationInfo

 

4、realm从数据库查询权限数据,返回ModularRealmAuthorizer

5、ModularRealmAuthorizer调用PermissionResolver进行权限串比对

6、如果比对后,isPermitted中"permission串"在realm查询到权限数据中,说明用户访问permission串有权限,否则
没有权限,抛出异常。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: