shiro权限设计
都是由中间表来实现的
授权
在ShiroUserMapper.xml中新增内容
<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>
Service层
package com.liyingdong.serivce;
import com.liyingdong.model.ShiroUser;
import org.springframework.stereotype.Repository;
import java.util.Set;
/**
* @author 李瀛东
* @site www.xiaomage.com
* @company xxx公司
* @create 2020-10-28 23:19
*/
@Repository
public interface ShiroUserService {
ShiroUser queryByName(String uname);
Set<String> getRolesByUserId(Integer userid);
Set<String> getPersByUserId( Integer userid);
}
package com.liyingdong.serivce.impl;
import com.liyingdong.mapper.ShiroUserMapper;
import com.liyingdong.model.ShiroUser;
import com.liyingdong.serivce.ShiroUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Set;
/**
* @author 李瀛东
* @site www.xiaomage.com
* @company xxx公司
* @create 2020-10-28 23:21
*/
@Service("shiroUserService")
public class ShiroUserServiceImpl implements ShiroUserService {
@Autowired
private ShiroUserMapper shiroUserMapper;
@Override
public ShiroUser queryByName(String uname) {
return shiroUserMapper.queryByName(uname);
}
@Override
public Set<String> getRolesByUserId(Integer userid) {
return shiroUserMapper.getRolesByUserId(userid);
}
@Override
public Set<String> getPersByUserId(Integer userid) {
return shiroUserMapper.getPersByUserId(userid);
}
}
重写自定义realm中的授权方法
/**授权的方法
* AuthorizingRealm
* @param principalCollection
* @return
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
// 拿到需要授权的账号
String uname = principalCollection.getPrimaryPrincipal().toString();
// 通过当前账号拿到用户
ShiroUser shiroUser = this.shiroUserService.queryByName(uname);
// 拿到当前用户对应的所有权限(pid)
Set<String> perids = this.shiroUserService.getPersByUserId(shiroUser.getUserid());
// 拿到当前用户对应的所有角色
Set<String> roleIds = this.shiroUserService.getRolesByUserId(shiroUser.getUserid());
// 返回认证的符合格式
SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
// 设置角色
info.setRoles(roleIds);
// 设置权限
info.setStringPermissions(perids);
return info;
}
注解式开发
常用注解介绍
@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层
@RequiresUser
@ResponseBody
@RequestMapping("/passUser")
public String passUser(){
return "身份认证成功,能够访问!!!";
}
// 角色id为2(ls)
// Logical.AND 是指必须符合value的数组值才可以
// Logical.OR 是指必须符合value的数组值单个值才可以
@RequiresRoles(value={"2","3"},logical = Logical.OR)
@ResponseBody
@RequestMapping("/passRole")
public String passRole(){
return "角色认证成功,能够访问!!!";
}
@RequiresPermissions(value={"user:update","user:load"},logical = Logical.AND)
@ResponseBody
@RequestMapping("/passPer")
public String passPer(){
return "权限认证成功,能够访问!!!";
}
Springmvc.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>
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>
结果:
zs只能查看身份认证的按钮内容
ls、ww可以看权限认证按钮内容
zdm可以看所有按钮的内容
查看失败: