zoukankan      html  css  js  c++  java
  • 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.lingerqi.service;
    
    import com.lingerqi.mapper.ShiroUserMapper;
    import com.lingerqi.model.ShiroUser;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.Set;
    
    /**
     * @author xyls
     * @blog name & blog address 027@0030
     * @create  2019-11-03 22:14
     */
    @Service("shiroUserService")
    public class ShiroUserServiceImpl implements ShiroUserService {
        @Autowired
        private ShiroUserMapper shiroUserMapper;
    
        @Override
        public Set<String> getRolesByUserId(Integer userId) {
            return shiroUserMapper.getRolesByUserId(userId);
        }
    
        @Override
        public Set<String> getPersByUserId(Integer userId) {
            return shiroUserMapper.getPersByUserId(userId);}
    
        @Override
        public ShiroUser queryByName(String uname) {
    
            return shiroUserMapper.queryByName(uname);
        }
    }

    重写自定义realm中的授权方法

     

      /**
         * 授权
         *
         * @param principalCollection
         * @return
         */
        @Override
        protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
            String uname = principalCollection.getPrimaryPrincipal().toString();
            ShiroUser shiroUser = this.shiroUserService.queryByName(uname);
            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需要角色adminuser

      @RequiresPermissions(value = {"user:delete","user:b"},logical = Logical.OR):表示当前Subject需要权限user:delete或者user:b

     

    注解的使用

     

    Controller

     

    @RequiresUser
    @RequestMapping("/passUser")
    public String passUser(HttpServletRequest request){
        return "admin/addUser";
    }
    
    @RequiresRoles(value = {"1","4"},logical = Logical.AND)
    @RequestMapping("/passRole")
    public String passRole(HttpServletRequest request){
        return "admin/listUser";
    }
    
    @RequiresPermissions(value = {"user:update","user:view"},logical = Logical.OR)
    @RequestMapping("/passPer")
    public String passPer(HttpServletRequest request){
        return "admin/resetPwd";
    }
    
    @RequestMapping("/unauthorized")
    public String unauthorized(){
        return "unauthorized";
    }
    

     

      

    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>
    

      

    <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>
    

     

     

     

     

  • 相关阅读:
    windows下常用linux对应工具
    常用工具备忘
    spring使用@Value标签读取.properties文件的中文乱码问题的解决
    一致性哈希算法(适用于分库分表、RPC负载均衡)转
    使用事件和消息队列实现分布式事务(转+补充)
    关于架构优化和设计,架构师必须知道的事情(转)
    做了 3 年企业级 SaaS,我收获的 10 点心得(转)
    这五件事,二次SaaS创业的老炮儿都在做(转)
    阿里云端口失效导致tomcat无法对外提供服务
    SaaS公司融资的「22条军规 」(转)
  • 原文地址:https://www.cnblogs.com/omji0030/p/11797242.html
Copyright © 2011-2022 走看看