shiro认证源码

1、web.xml中配置:

  1. <filter>  
  2.     <filter-name>shiroFilter</filter-name>  
  3.     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
  4.     <async-supported>true</async-supported>  
  5.     <init-param>  
  6.         <param-name>targetFilterLifecycle</param-name>  
  7.         <param-value>true</param-value>  
  8.     </init-param>  
  9. </filter>  
  10. <filter-mapping>  
  11.     <filter-name>shiroFilter</filter-name>  
  12.     <url-pattern>/*</url-pattern>  
  13. </filter-mapping> 
             这只是一个代理,执行流程委托给Spring容器中名为shiroFilter(上面同名的filter-name)的过滤器


2、shiro-spring.xml:

  1. <!-- Shiro的Web过滤器 -->  
  2. <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">  
  3.     <property name="securityManager" ref="securityManager"/>  
  4.     <property name="loginUrl" value="/login.jsp"/>  
  5.     <property name="unauthorizedUrl" value="/unauthorized.jsp"/>  
  6.     <property name="filters">  
  7.         <util:map>  
  8.             <entry key="authc" value-ref="formAuthenticationFilter"/>  
  9.         </util:map>  
  10.     </property>  
  11.     <property name="filterChainDefinitions">  
  12.         <value>  
  13.             /index.jsp = anon  
  14.             /unauthorized.jsp = anon  
  15.             /login.jsp = authc  
  16.             /logout = logout  
  17.             /authenticated.jsp = authc   
  18.             /** = user  
  19.         </value>  
  20.     </property>  
  21. </bean> 
                ShiroFilterFactoryBean实现了org.springframework.beans.factory.FactoryBean接口,所以由ShiroFilterFactoryBean的getObject()方法返回一个           

                  SpringShiroFilter对  象,即Spring配置文件中的shiroFilter对象,该过滤器拥有三个重要对象:

                                  SecurityManager、

                                  PathMatchingFilterChainResolver、                               

                                  FilterChainManager

                  当匹配到过滤链中的aauthc对应的url时,会执行匹配对应<entry key="authc" value-ref="formAuthenticationFilter"/>,既然是filter,那么最

                  重要的就是doFilter方法了,经过执行父类的一系列方法后最终执行到FormAuthenticationFilter类中的onAccessDenied方法

   

  1. protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {  
  2.     // 第一次访问自然不是登录请求  
  3.     if (isLoginRequest(request, response)) {  
  4.         // 判断是否是POST请求  
  5.         if (isLoginSubmission(request, response)) {  
  6.             if (log.isTraceEnabled()) {  
  7.                 log.trace("Login submission detected.  Attempting to execute login.");  
  8.             }  
  9.             return executeLogin(request, response);  

                  

                  接着执行父类AuthenticatingFilter中的executeLogin()方法,创建token的方法createToken()可以重写放回自定义实现UsernamePasswordToken的

                  类MyUsernamePasswordToken(可以实现实际的业务逻辑)

  1. protected boolean executeLogin(ServletRequest request, ServletResponse response) throws Exception {  
  2.     // 根据表单填写的用户名密码创建AuthenticationToken  
  3.     AuthenticationToken token = createToken(request, response);  
  4.     if (token == null) {  
  5.         String msg = "createToken method implementation returned null. A valid non-null AuthenticationToken " +  
  6.                 "must be created in order to execute a login attempt.";  
  7.         throw new IllegalStateException(msg);  
  8.     }  
  9.     try {  
  10.         // 获取Subject对象  
  11.         Subject subject = getSubject(request, response);  
  12.         // 执行Subject.login方法进行登录  
  13.         subject.login(token);  
  14.         // 如果登录成功,重定向至上次访问的URL  
  15.         return onLoginSuccess(token, subject, request, response);  
               

                 接着执行subject的实现类DelegatingSubject中的login()方法

                   

                 接着执行securityManager实现类DefaultSecurityManager的login()方法

                 

                 接着执行AuthenticatingSecurityManager中的authenticate( )方法,该方法this.authenticator返回的是ModularRealmAuthenticator

                 

                 执行ModularRealmAuthenticator中的authenticaticate( )方法,因为其继承AbstractAuthenticator,所以执行AbstractAuthenticator中的authenticaticate( )

                   

                 接着执行ModularRealmAuthenticator中的doAuthenticate( )方法

                        

                 其中realm.getAuthenticationInfo( ),realm为Realm接口,实际上调用的是其实现类AuthenticatingRealm中的getAuthenticationInfo( )方法

                 

                上述代码AuthenticationInfo info = getCachedAuthenticationInfo(token);从缓存中获取认证信息,如果未获取到,则调用doGetAuthenticationInfo(token)

                方法获取认证信息,获取到认证信息info后执行assertCredentialsMatch( )方法

                     

                 getCredentialsMatcher( )返回我们自定义实现SimpleCredentialsMatcher的类MyCredentialsMatcher,接着cm.doCredentialMatch( )调用实现类方法

                        

                校验成功后则执行下面的跳转方法onLoginSuccess( )

                           

                接着执行实现FormAuthenticationFilter的方法

                         

                        认证成功执行FormAuthenticationFilter中的onLoginSuccess()方法,调用issueSuccessRedirect( ),

                        接着执行AuthenticationFilter中的issueSuccessRedirect( )

                        

                        认证失败则执行FormAuthenticationFilter中的onLoginFailure方法,接着执行FormAuthenticationFilter中的setFailureAttribute( )方法

                        getFailureKeyAttribute( )方法返回的事一个final字符窜:DEFAULT_ERROR_KEY_ATTRIBUTE_NAME

                        

            接着在LoginController中获取到key对应的值进行异常抛出

                  


参考文献:http://www.cnblogs.com/ccfdod/p/6436353.html

                http://blog.csdn.net/xtayfjpk/article/details/53729135

                http://jinnianshilongnian.iteye.com/blog/2029717

### 回答1: Shiro是一个开源的Java安全框架,用于对应用程序进行身份验证、授权和加密等安全相关功能的支持。Spring Boot是一个用于创建独立的、生产级别的Spring应用程序的框架。 Shiro Spring Boot Starter是一个与Spring Boot集成的项目,它提供了使用Shiro框架进行身份验证和授权的便捷方式。通过引入Shiro Spring Boot Starter依赖,可以轻松将Shiro集成到Spring Boot应用程序中。 Shiro Spring Boot Starter的源码包括几个主要部分: 1. 自动配置类:ShiroAutoConfiguration是Shiro Spring Boot Starter的核心配置类,它通过@EnableConfigurationProperties注解读取配置文件中的属性,并根据这些属性进行相应的自动配置。它负责创建Shiro的安全管理器、认证器、授权器等实例,并通过注入的方式将它们注入到Spring容器中。 2. 属性配置类:ShiroProperties定义了Shiro在配置文件中的属性,并提供了默认值。通过@ConfigurationProperties注解,可以将这些属性与配置文件中的对应属性进行关联。 3. 注解支持类:ShiroAnnotationProcessor是一个自定义的注解处理器,它通过处理标注了@RequiresAuthentication、@RequiresUser、@RequiresRoles和@RequiresPermissions等注解的方法,实现了对方法级别的身份验证和授权支持。 4. 过滤器类:ShiroFilterChainDefinition是Shiro的过滤器链定义类,它定义了URL与过滤器的映射关系,并负责创建ShiroFilterFactoryBean的实例,将其注入到容器中。 5. 辅助类:ShiroUtils是一个工具类,提供了一些常用的方法,如获取当前登录用户的主体、判断用户是否拥有指定角色或权限等。 总的来说,Shiro Spring Boot Starter的源码实现了对Shiro框架在Spring Boot应用程序中的集成和自动配置。通过引入该Starter依赖,可以简化Shiro框架的配置和使用,提高开发效率,同时保证应用程序的安全性。 ### 回答2: Shiro是一个基于Java的开源安全框架,用于提供身份验证、授权、会话管理和密码加密等功能。它能帮助开发人员快速构建安全可靠的应用程序。而Spring Boot是一个基于Spring框架的开源项目,它简化了Spring应用程序的开发和部署。 Shiro和Spring Boot结合使用,可以使得应用程序的安全性和性能得到更好地保障。Shiro作为一个独立的框架可以和Spring Boot集成,通过配置文件和注解的方式,实现对应用程序的安全管理。 Shiro Spring Boot源码是指将Shiro和Spring Boot集成时所使用到的相关源代码。这些源码包括了配置文件、注解、代码注入、过滤器等等。通过阅读和理解这些源码,开发人员可以深入了解Shiro Spring Boot集成的工作原理和机制。 通过阅读Shiro Spring Boot源码,我们可以了解到Shiro是如何通过自定义配置文件和注解来实现各种身份验证和授权的方式。源码中可以看到一些关键的类和方法,如Realm、Subject、AuthenticationToken等等,这些类和方法对于理解Shiro Spring Boot的工作流程非常重要。 另外,Shiro Spring Boot源码还涉及到了Spring Boot的自动配置机制。Spring Boot通过自动配置,可以减少开发人员的工作量,自动完成一些基本的配置,以适应不同的应用需求。通过阅读源码,我们可以了解到Spring Boot是如何实现自动配置功能的,以及如何自定义配置来适配特定的应用场景。 总的来说,阅读Shiro Spring Boot源码有助于我们深入理解Shiro和Spring Boot的工作原理和机制,提升对应用程序的安全性和性能的把握,进而能更好地开发和调优应用程序。 ### 回答3: Shiro是一个强大的身份认证和授权框架,而Spring Boot是一个用来简化Spring应用程序开发和部署的框架。Shiro Spring Boot是Shiro和Spring Boot的结合体,提供了在Spring Boot应用中集成Shiro的功能。 Shiro Spring Boot源码包含了一系列的类和配置文件,用于配置和启动Shiro框架。在源码中,可以找到一些核心类,比如ShiroFilterFactoryBean、DefaultWebSecurityManager等。这些类负责处理Shiro的配置和初始化。 ShiroFilterFactoryBean是Shiro的核心过滤器,是Shiro的入口点。它负责创建Shiro的安全过滤器链,并根据配置决定哪些请求应该经过Shiro认证和授权。 DefaultWebSecurityManager是Shiro的安全管理器,它负责管理和协调Shiro的各种组件,比如Realm、SessionManager等。它是Shiro框架中最重要的组件之一。 除了这些核心类,源码中还包含了一些配置类,比如ShiroConfig、ShiroProperties等,用于配置Shiro的相关参数和属性。这些配置类提供了灵活的配置选项,使用户可以根据自己的需求来定制Shiro的行为。 总的来说,Shiro Spring Boot源码提供了一个方便快捷地在Spring Boot应用中集成Shiro的方式。通过深入研究和理解源码,我们可以更好地掌握Shiro的工作原理,并根据自己的需求进行扩展和定制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值