第一种:URL 级别粗粒度权限控制
配置 web.xml 的 shiroFilter 拦截 /*
在 spring 的 applicationContext*.xml 配置文件中配置同名 bean,配置
filterChainDefinitions 拦截控制规则
xxx.html* = anon (未登录可以访问) xxx.html* =authc (必须登录才能访问 ) xxx.html* = perms[权限] (需要特定权限才能访问) xxx.html* = roles[角色] (需要特定角色才能访问 )
第二种: 方法级别细粒度权限控制
在 spring 的 applicationContext*.xml 配置 spring aop 对 spring 管理 bean 对象开启 shiro 注解支持
@RequiresPermissions(权限) 需要特定权限才能访问
@RequiresRoles(角色) 需要特定角色才能访问
@RequiresAuthentication 需要认证才能访问
第三种:通过 shiro 自定义标签,实现页面元素显示控制
shiro:authenticated 登录后才能访问
<shiro:hasPermission name=“abc”> 需要特定权限才能访问
<shiro:hasRole name=“abc”> 需要特定角色才能访问
第四种:在程序中通过代码 判断用户是否具有指定权限( 不太常用 ,有代码侵入 )
作者:llx_1001
来源:CSDN
原文:https://blog.csdn.net/llx_1001/article/details/79764441
版权声明:本文为博主原创文章,转载请附上博文链接!
<div class="htmledit_views" id="content_views">
**<p>我们使用shiro进行权限控制 有以下四种方式<br>**
1. URL拦截权限控制:基于filter过滤器实现
我们在spring配置文件中配置shiroFilter时配置
<!–指定URL级别拦截策略 -->
<property name=“filterChainDefinitions”>
<value>
/css/ = anon
/js/ = anon
/images/ = anon
/validatecode.jsp = anon
/login.jsp = anon
/userActionlogin.action = anon
/pagebasestaff.action = perms[“staff-list”]
/ = authc
</value>
</property>
2. 方法注解权限控制:基于代理技术实现
我们在代码方法上用注解声明调用该方法需要什么权限。
首先要在spring配置文件中进行声明开启shiro注解:
<!-- 开启shiro框架注解支持 -->*
<bean id=“defaultAdvisorAutoProxyCreator”
class=“org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator”>
<!-- 必须使用cglib方式为Action对象创建代理对象 -->
<property name=“proxyTargetClass” value=“true”/>
</bean>
<!-- 配置shiro框架提供的切面类,用于创建代理对象 -->
<bean class=“org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor”/>
然后在方法上声明:
@RequiresPermissions(“staff-delete”)
//执行这个方法,需要当前用户具有staff-delete这个权限
public String deleteBatch(){
staffService.deleteBatch(ids);
return LIST;
}
*
3. 页面标签权限控制:页面表签技术实现
首先要在jsp页面进入表签:
<%@ taglib uri=“http://shiro.apache.org/tags” prefix=“shiro” %>
然后包裹权限控制的内容
<shiro:hasPermission name=“Permission”>
xxxxxxxxxxxxxxxx
</shiro:hasPermission>
4.代码级别权限控制:
public String edit(){
Subject subject = SecurityUtils.getSubject();
subject.checkPermission(“staff-edit”);
Staff staff = staffService.findById(model.getId());
staff.setName(model.getName());
staff.setTelephone(model.getTelephone());
staff.setHaspda(model.getHaspda());
staff.setStandard(model.getStandard());
staff.setStation(model.getStation());
staffService.update(staff);
return LIST;
}
总结:
使用shiro进行权限控制时 这四种方法并不是进行单一的使用,是相互结合的使用从而完整的进行权限控制。
一.过滤器
在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>