Shiro授权

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>

ShiroUserMapper.java

    
    //查询用户对应角色id集合
    Set<String> getRolesByUserId(@Param("userid")Integer userid);

    //查询用户对应的权限名称集合
    Set<String> getPersByUserId(@Param("userid")Integer userid);

ShiroUserService.java

   
    Set<String> getRolesByUserId(Integer userId);

    Set<String> getPersByUserId(Integer userId);

ShiroUserServiceImpl.java

   @Override
    public Set<String> getRolesByUserId(Integer userId) {
        return shiroUserMapper.getRolesByUserId(userId);
    }

    @Override
    public Set<String> getPersByUserId(Integer userId) {
        return shiroUserMapper.getPersByUserId(userId);
    }

重写MyRealm.java中的授权方法

   
   @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("进行授权");
        ShiroUser shiroUser=this.shiroUserService.queryByName(principals.getPrimaryPrincipal().toString());
        // 当前认证过的用户对应的角色id集合
        Set<String> rolesByUserId = this.shiroUserService.getRolesByUserId(shiroUser.getUserid());
        // 当前认证过的用户对应的权限id集合
        Set<String> persByUserId = this.shiroUserService.getPersByUserId(shiroUser.getUserid());
        AuthorizationInfo info = new SimpleAuthorizationInfo();
        ((SimpleAuthorizationInfo) info).setRoles(rolesByUserId);
        ((SimpleAuthorizationInfo) info).setStringPermissions(persByUserId);
        return info;
    }

运行如下

用zmd登录点击用户新增未授权会显示如下图
在这里插入图片描述
授权显示如下图
在这里插入图片描述

Shiro的注解式开发

常用注解介绍

@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

HelloController.java

   
    @RequiresUser
    @ResponseBody
    @RequestMapping("/passUser")
    public  String  passUser(HttpServletRequest req){
        return  "pass User";
    }

    @RequiresRoles(value = {"1","4"},logical = Logical.OR)
    @ResponseBody
    @RequestMapping("/passRole")
    public  String  passRole(HttpServletRequest req){
        return  "pass Role";
    }

    @RequiresPermissions(value = {"user:load","user:export"},logical = Logical.AND)
    @ResponseBody
    @RequestMapping("/passAuth")
    public  String  passAuth(HttpServletRequest req){
        return  "pass Auth";
    }

在spring-mvc.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>

测试
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值