shiro的授权,运用到的是你编写的Realm(继承了AuthorizingRealm)里的doGetAuthorizationInfo()方法。
这个方法会在每当跳转地址时调用,这也就意味着性能比较差,但相对应的安全性就会很高,会一直验证你的身份。
准备工作
认证用到的是SimpleAuthenticationInfo,
同样的授权也有一个:SimpleAuthorizationInfo
注意,这两个是不同的。
在doGetAuthorizationInfo()方法里,主要的就是这个,SimpleAuthorizationInfo在实现授权,他要的参数是权限,所以我们得写方法来获取。
getRolesByUserId:是根据ID来获取角色。
getPersByUserId:是根据ID来获取权限。
Set<String> getRolesByUserId(Integer userid);
Set<String> getPersByUserId(Integer userid);
<select id="getRolesByUserId" resultType="java.lang.String" parameterType="java.lang.Integer">
select r.roleid from t_shiro_user u,t_shiro_user_role ur,t_shiro_role r
where u.userid = ur.userid and ur.roleid = r.roleid
and u.userid = #{userid}
</select>
<select id="getPersByUserId" resultType="java.lang.String" parameterType="java.lang.Integer">
select p.permission from t_shiro_user u,t_shiro_user_role ur,t_shiro_role_permission rp,t_shiro_permission p
where u.userid = ur.userid and ur.roleid = rp.roleid and rp.perid = p.perid
and u.userid = #{userid}
</select>
然后在Realm中的doGetAuthorizationInfo()方法调用:
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//根据用户名查找所有信息 用户名从principalCollection中获取
ShiroUser shiroUser = this.shiroUserService.queryByName(principalCollection.getPrimaryPrincipal().toString());
//根据ID获取其 角色 和 权限
Set<String> roles = this.shiroUserService.getRolesByUserId(shiroUser.getUserid());
Set<String> pers = this.shiroUserService.getPersByUserId(shiroUser.getUserid());
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//加入到info
info.setRoles(roles);
info.setStringPermissions(pers);
return info;
}
然后返回info,AuthorizingRealm中会自动帮你识别。
到了这一步,即已经完成授权了。可开DEBUG验证一下。
注解式开发
常用注解介绍
@RequiresAuthenthentication:表示当前Subject已经通过login进行身份验证;即 Subject.isAuthenticated()返回 true
@RequiresUser:表示当前Subject已经身份验证或者通过记住我登录的
@RequiresGuest:表示当前Subject没有身份验证或者通过记住我登录过,即是游客身份
@RequiresRoles(value = {“admin”,“user”},logical = Logical.AND):表示当前Subject需要角色admin和user
@RequiresPermissions(value = {“user:delete”,“user:b”},logical = Logical.OR):表示当前Subject需要权限user:delete或者user:b
因为controller是交给springMVC管理的,所以需要在springmvc-servlet.xml中加入安全管理器和错误映射:
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor">
<property name="proxyTargetClass" value="true"></property>
</bean>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="org.apache.shiro.authz.UnauthorizedException">
unauthorized
</prop>
</props>
</property>
<property name="defaultErrorView" value="unauthorized"/>
</bean>
这就可以使用注解开发了。
controller:
方法:
/**
* 讲解身份验证的注解
* @param req
* @param resp
* @return
*/
@RequiresUser
@RequestMapping("/passUser")
public String passUser(HttpServletRequest req, HttpServletResponse resp){
return "admin/addUser";
}
/**
*角色认证的注解
* @param req
* @param resp
* @return
* 当前方法必须同时具备1、4的角色ID才能被访问。
*/
@RequiresRoles(value = {"1","4"},logical = Logical.AND)
@RequestMapping("/passRole")
public String passRole(HttpServletRequest req, HttpServletResponse resp){
return "admin/listUser";
}
/**
* 权限认证的注解
*/
/**
*
* @param req
* @param resp
* @return
* 拥有user:update"或者"user:view权限即可访问
*/
@RequiresPermissions(value = {"user:update","user:view"},logical = Logical.OR)
@RequestMapping("/passPer")
public String passPer(HttpServletRequest req, HttpServletResponse resp){
return "admin/resetPwd";
}
测试:
jsp:
<ul>
shiro注解
<li>
<a href="${pageContext.request.contextPath}/passUser">用户认证</a>
</li>
<li>
<a href="${pageContext.request.contextPath}/passRole">角色</a>
</li>
<li>
<a href="${pageContext.request.contextPath}/passPer">权限认证</a>
</li>
</ul>
完成。