标签:encoding ida org 注入 java intercept override type return
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>
标签:encoding ida org 注入 java intercept override type return
原文地址:http://www.cnblogs.com/miye/p/7909733.html