ApacheShrio安全框架实现原理及实例详解-成都快上网建站

ApacheShrio安全框架实现原理及实例详解

一、Shiro整体概述

创新互联公司专注于根河企业网站建设,成都响应式网站建设公司,商城网站建设。根河网站建设公司,为根河等地区提供建站服务。全流程按需制作,专业设计,全程项目跟踪,创新互联公司专业和态度为您提供的服务

1.简介

Apache Shiro是Java的一个安全框架,功能强大,使用简单,Shiro为开发人员提供了一个直观而全面的认证(登录),授权(判断是否含有权限),加密(密码加密)及会话管理(Shiro内置Session)的解决方案.

2.Shiro组件

Apache Shrio安全框架实现原理及实例详解

3.Shiro架构

3.1 外部架构(以应用程序角度)

Apache Shrio安全框架实现原理及实例详解

3.2 内部架构

Apache Shrio安全框架实现原理及实例详解

4. Shiro的过滤器

过滤器简称

对应的java类

anon

org.apache.shiro.web.filter.authc.AnonymousFilter

authc

org.apache.shiro.web.filter.authc.FormAuthenticationFilter

authcBasic

org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter

perms

org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter

port

org.apache.shiro.web.filter.authz.PortFilter

rest

org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter

roles

org.apache.shiro.web.filter.authz.RolesAuthorizationFilter

ssl

org.apache.shiro.web.filter.authz.SslFilter

user

org.apache.shiro.web.filter.authc.UserFilter

logout

org.apache.shiro.web.filter.authc.LogoutFilter

挑几个重要的说明一下:

anon:匿名过滤器,不登录也可以访问的资源使用,比如首页,一些静态资源等

authc:认证过滤器,登录成功后才能访问的资源使用

perms:授权过滤器,必须具备某种权限才能访问

roles:角色过滤器,必须具备某种角色才能访问

注意:这么多过滤器,使用起来肯定不方便,Shiro框架也考虑到了这一点,所以有一个过滤器,一个顶十个,即DelegatingFilterProxy.

5. Shiro与Spring整合

5.1 pom.xml


    
      org.apache.shiro
      shiro-spring
      1.3.2
    

5.2 web.xml


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

5.3applicationContext-shiro.xml

<?xml version="1.0" encoding="UTF-8"?>

  
  
    
    
    
    
  

  
  
    
    
  

  
  

  
  
  

  
  
    
    
    
    
    

    
      
      
        /system/module/list.do = perms["模块管理"]
        /index.jsp* = anon
        /login.jsp* = anon
        /login* = anon
        /logout* = anon
        /css/** = anon
        /img/** = anon
        /plugins/** = anon
        /make/** = anon
        /** = authc
      
    
  

  
  

  
  
    
  

  
  
    
  

  
  

5.4 如果想看具体的实现代码(含sql脚本),可以点击页面右上角Fork me on github,到我的github仓库中拉取

仓库地址:  https://github.com/AdilCao/Shiro.git

代码部分只需要关注三个类:

1.LoginController(登录,在这里获取Subject主体,调用subject.login()方法后直接调用认证方法)

2.AuthRealm(认证和授权在这个类中定义,认证成功后调用密码比较器进行比较;授权即查找登录用户所具有的权限模块集合)

3.CustomCredentialsMatcher(密码比较器,将浏览器输入明文密码加密后,与数据库中的安全密码进行比较)

注意:整个过程中如果登录不成功,就会抛出异常


分享标题:ApacheShrio安全框架实现原理及实例详解
链接地址:http://kswjz.com/article/igechg.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流