shiro 与spring集成

实际开发项目中shiro经常与spring集成使用,该文章就来介绍下sping集成shiro完成权限验证的操作。

1、引入shiro相关jar包

加入shiro的jar包,此处使用版本为1.4.0,spring的jar包就忽略了(当 大家有spring基础)

   
    
      org.apache.shiro
      shiro-web
      1.4.0
    
    
      org.apache.shiro
      shiro-spring
      1.4.0
    
    
      org.apache.shiro
      shiro-ehcache
      1.4.0
    
    
      org.apache.shiro
      shiro-quartz
      1.4.0
    

2、在web.xml中加入shiro的过滤器


        shiroFilter
        org.springframework.web.filter.DelegatingFilterProxy
        
            targetFilterLifecycle
            true
        
        
            targetBeanName
            shiroFilter
        
    
    
        shiroFilter
        /*
    

3、编写shiro的登录和权限认证类Realm

/**
     * 权限认证,验证授权信息,主要设置用户的角色和权限信息
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //1.获取登录用户信息
        HUser user = (HUser) principalCollection.getPrimaryPrincipal();
        //2.查询用户的权限信息
        List permissionList =
                permissionService.findPermissonByUser(user.getId());
        // 创建权限信息对象
        SimpleAuthorizationInfo authorizationInfo =
                new SimpleAuthorizationInfo();
        Set permissionSet=new HashSet<>();
        for (HPermission hPermission : permissionList) {
                permissionSet.add(hPermission.getPercode());
        }
        //设置权限信息(权限编码)
        authorizationInfo.setStringPermissions(permissionSet);
        List roleList = roleService.findRolesByUser(user.getId());
        SetroleSet=new HashSet<>();
        for (HRole hRole : roleList) {
            roleSet.add(hRole.getRolecode());
        }
        //设置角色信息(角色编码)
        authorizationInfo.setRoles(roleSet);
        return authorizationInfo;
    }

    /**
     * 身份认证,登录时判断用户身份信息
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //1、authenticationToken 保存了用户登录信息,获取前台登录传递的用户名
        String name = (String) authenticationToken.getPrincipal();
        //2、根据用户名查询用户信息
        HUser user = userService.findUserByName(name);
        //返回null则验证不通过
        if(user==null) {
            return null;
        }
        //身份认证信息对象
        //第一个参数为需要保存到session中的对象
        //第二个参数为数据库中存储的密码
        //第三个参数为盐值
        //第四个参数为自定义的realm的名字
        SimpleAuthenticationInfo simpleAuthenticationInfo=
          new SimpleAuthenticationInfo(user,user.getPassword()
          ,new SimpleByteSource(user.getCreatetime().getTime()+"")
          ,getName());
        return simpleAuthenticationInfo;
    }

4、与spring整合

4.1 配置applicationContext-shrio.xml配置文件,用以配置shiro的信息




    
    
        
        
        
        
    

    
    
         
         
         
         
    

    
    
        
        
    

    
    
        
        
        
        
    
    
    
        
        
        
        
        
        
        
        
            
                /js/** =anon
                /login.jsp =anon
                /plugins/** =anon
                /login =anon
                /register.jsp =anon
                /register =anon
                /** =authc
            
        
    


 @RequestMapping("/register")
    public String register(HUser user){
        //设置创建时间
        user.setCreatetime(new Date());
        //将用户密码MD5加密
        SimpleHash md5 = new SimpleHash(
                "MD5"//加密方法
                ,user.getPassword()//需要加密的数据
                ,user.getCreatetime().getTime()+""//盐值
                ,2);//加密次数
        //将加密后的密码设置进user对象
        user.setPassword(String.valueOf(md5));
        userService.insert(user);
        return "redirect:/toLogin";
    }
 @RequestMapping("login")
    public String login(HUser user){
        //获取登录用户Subject
        Subject subject = SecurityUtils.getSubject();
        //封装token
        UsernamePasswordToken token=
                new UsernamePasswordToken(user.getName(),user.getPassword());
        try {
            //执行登录验证,会调用自定义realm进行验证。
            subject.login(token);
        }catch (AuthenticationException e){
            //如果抛出异常,则认证失败,跳转到登录页面
            return "login";
        }
        //没抛异常则认证通过,跳转到首页
        return "redirect:/index";
    }

4.2 在springmvc配置文件中配置shiro




    
    
        
            
                
                
                    
                        
                            
                                
                                    
                                    
                                
                            
                        
                    
                
            
        
    
    
    
    
    
    
    
    

    
    

    
    
    

    
    

    
    
        
        
            
                
                
                    /refuse.jsp
                
            
        
    


你可能感兴趣的:(shiro 与spring集成)