shiro实现授权的三种操作
2016-08-09 12:24
302 查看
授权的例子就是是否可以访问某个页面,可以操作某个按钮,是否可以编缉对应的数据等。
如何在shiro中使用授权
if (currentUser.hasRole("admin")) {
判断用户是否有打印的权限
Permission printPermission =new PrinterPermission(“laserjet3000n”,“print”);
If (currentUser.isPermitted(printPermission)) {
//do one thing (show the print button?)
} else {
//don’t show the button?
}
也可以使用字符串的方式验证
String perm = “printer:print:laserjet4400n”;
if(currentUser.isPermitted(perm)){
//show the print button?
}else {
//don’t show the button?
}
//Will throw an AuthorizationException if none
//of the caller’s roles imply the Account
//'create' permission\u000B
@RequiresPermissions(“account:create”)
public void openAccount( Account acct ) {
//create the account
}
判断用户角色,如果符合角色,可以使用对应方法
//Throws an AuthorizationException if the caller
//doesn’t have the ‘teller’ role:
@RequiresRoles( “teller” )
public void openAccount( Account acct ) {
//do something in here that only a teller
//should do
}
<%@ taglib prefix=“shiro” uri=http://shiro.apache.org/tags %>
<html>
<body>
<shiro:hasPermission name=“users:manage”>
<a href=“manageUsers.jsp”>
Click here to manage users
</a>
</shiro:hasPermission>
<shiro:lacksPermission name=“users:manage”>
No user managementfor you!
</shiro:lacksPermission>
</body>
</html>
如何在shiro中使用授权
1,使用编程方式
判断是否有管理员角色if (currentUser.hasRole("admin")) {
判断用户是否有打印的权限
Permission printPermission =new PrinterPermission(“laserjet3000n”,“print”);
If (currentUser.isPermitted(printPermission)) {
//do one thing (show the print button?)
} else {
//don’t show the button?
}
也可以使用字符串的方式验证
String perm = “printer:print:laserjet4400n”;
if(currentUser.isPermitted(perm)){
//show the print button?
}else {
//don’t show the button?
}
2,使用注释方式
判断用户是否有 创建账户权限//Will throw an AuthorizationException if none
//of the caller’s roles imply the Account
//'create' permission\u000B
@RequiresPermissions(“account:create”)
public void openAccount( Account acct ) {
//create the account
}
判断用户角色,如果符合角色,可以使用对应方法
//Throws an AuthorizationException if the caller
//doesn’t have the ‘teller’ role:
@RequiresRoles( “teller” )
public void openAccount( Account acct ) {
//do something in here that only a teller
//should do
}
3,使用jsp taglib
判断用户是否有管理权限<%@ taglib prefix=“shiro” uri=http://shiro.apache.org/tags %>
<html>
<body>
<shiro:hasPermission name=“users:manage”>
<a href=“manageUsers.jsp”>
Click here to manage users
</a>
</shiro:hasPermission>
<shiro:lacksPermission name=“users:manage”>
No user managementfor you!
</shiro:lacksPermission>
</body>
</html>
相关文章推荐
- shiro实现授权的三种操作
- Shiro实现授权的三种操作方式
- 如何实现共享软件网络授权认证,包括注册新用户、登录、修改密码等操作
- 角色权限etmvc+jQuery EasyUI+combobox多值操作实现角色授权
- Android绘图机制(三)——自定义View的三种实现方式以及实战项目操作
- 【C++】实现双向链表的所有操作,包括逆置双链表(三种方法)
- Shiro实现用户授权(Authorization)
- 基于操作+角色的授权方案(具体实现篇)
- Shiro授权的三种方式
- Shiro系列之Shiro+Mysql实现用户授权(Authorization)
- 如何实现共享软件网络授权认证,包括注册新用户、登录、修改密码等操作
- Apache shiro集群实现 (四)shiro授权(Authentication)--访问控制
- 将 Shiro 作为应用的权限基础 三:基于注解实现的授权认证过程
- [shiro学习笔记]第二节 shiro与web融合实现一个简单的授权认证
- [shiro学习笔记]第二节 shiro与web融合实现一个简单的授权认证
- etmvc+jQuery EasyUI+combobox多值操作实现角色授权
- Zend Framework之Zend_Auth,Zend_Acl实现用户的授权和操作
- Spring Shiro配置实现用户认证和授权
- 【C++】实现双向链表的所有操作,包括逆置双链表(三种方法)
- BOS项目练习(权限/角色/用户管理(CRUD),基于数据库实现动态授权,ehcache缓存权限,shiro标签,菜单权限展示)