shiro框架的四中权限控制方式
2024-09-30 00:55:03
https://www.cnblogs.com/cocosili/p/7103025.html
一.在自定义的realm中进行权限控制
在applicationContext.xml文件中添加 /areaAction_pageQuery.action = perms["area"]
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 注入shiro框架核心对象,安全管理器 -->
<property name="securityManager" ref="securityManager"/>
<!--private String loginUrl;登录页面
private String successUrl;登录成功后跳转页面
private String unauthorizedUrl;权限不足时的提示页面-->
<property name="loginUrl" value="/login.html"/>
<property name="successUrl" value="/index.html"/>
<property name="unauthorizedUrl" value="/unauthorized.html"/>
<!-- 指定URL拦截规则 -->
<property name="filterChainDefinitions">
<!--authc:代表shiro框架提供的一个过滤器,这个过滤器用于判断当前用户是否已经完成认证,
如果当前用户已经认证,就放行,如果当前用户没有认证,跳转到登录页面
anon:代表shiro框架提供的一个过滤器,允许匿名访问-->
<value>
/css/* = anon
/images/* = anon
/js/** = anon
/validatecode.jsp* = anon
/userAction_login.action = anon
/areaAction_pageQuery.action = perms["area"]
/** = authc
</value>
</property>
</bean>
此时访问areaAction_pageQuery.action是页面不会查询到数据,须要为用户授权
在自定义realm中为用户授权
@Override
/**
* 授权方法
*/
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//为用户授权,只需将用户的权限添加info中即可
info.addStringPermission("area");
return info;
}
二.使用shiro的方法注解为用户授权
1.在spring配置文件applicationContext.xml中配置开启shiro注解支持
<!-- 基于spring自动代理方式为service创建代理对象,实现权限控制 -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
<!-- 强制使用cglibdaili -->
<property name="proxyTargetClass" value="true"></property>
</bean>
<!-- 配置切面 -->
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"></property>
</bean>
2.配置事物注解,强制使用cglib代理
<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" />
3.在service上配置注解
1 @RequiresPermissions("courier:delete")
2 public void deleteBatch(String ids) {
3 //判断是否为空
4 if(StringUtils.isNoneBlank(ids)){
5 String[] idsArrays = ids.split(",");
6 for (String id : idsArrays) {
7 Integer courierid = Integer.parseInt(id);
8 dao.deleteCourier(courierid);
9 }
10 }
11 }
三.使用shiro标签进行权限控制
1.在jsp页面中引入shiro标签库
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
2.在页面中使用标签
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<!-- 判断当前用户是否已经认证,已认证就可以看到标签中的内容 -->
<shiro:authenticated>
看到内容就说明你已经认证成功了!
</shiro:authenticated> <br> <!-- 判断当前用户是否拥有指定的权限 -->
<shiro:hasPermission name="area">
<input value="这是判断权限的按钮">
</shiro:hasPermission> <br> <!-- 判断当前用户是否拥有指定的角色 -->
<shiro:hasRole name="admin">
<input value="这是判断角色的按钮">
</shiro:hasRole>
</body>
</html>
最新文章
- 计算机程序的思维逻辑 (47) - 堆和PriorityQueue的应用
- Spring in action - 会话管理
- 黑马程序员——OC语言 三大特性之多态
- 【Matplotlib】 增加图例
- 安装Debian的正确方法
- WP7 MD5加密
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(45)-工作流设计-设计步骤
- nginx,wsgi,flask之间的关系
- C#共享内存实例 附源码
- [妙味DOM]第五课:事件深入应用
- [Codeforces]813F Bipartite Checking
- Hessian探究(一)Hessian与Spring结合
- redis的特点
- 《Linux就该这么学》第十六天课程
- ASP.NET MVC项目中App_Code目录在程序应用
- 洛谷 P1451 求细胞数量
- js对input框的可编辑属性设置
- 关于MVC的感悟
- SQL数据库中临时表、临时变量和WITH AS关键词创建“临时表”的区别
- HTML5新特性postMessage解决跨域