Spring+shiro+cas Integrated Configuration Notes

Source: Internet
Author: User

First, preface

Apache Shiro is a security framework for Java, just like spring security. So why integrate with spring and use Shiro? Don't ask why, wayward! A joke: D In fact, I personally think spring security is too cumbersome to write too many filters. I am a person afraid of trouble, Shiro simple configuration This is the reason for my choice, not to mention the spring officials themselves recommend the use of Shiro. The combination of Shiro privilege control and CAS single sign-on is a Java security ***~ ( ̄_, ̄) ... However, this article only introduces the integration of the three of them (that is, to leave a study note for themselves), if the content of interest in this area can go to the online search study, in this recommendation Open Tao Great God:"Follow me to learn Shiro."

Again, the following is only a personal study note, not a tutorial.


Second, the configuration

Shiro is the most important authentication and authorization, and the focus of CAS is the single sign-on, in fact, CAS and Shiro integration is about the integration of certification that piece.

Let's first look at the configuration of Shiro and CAs in Web. xml:

<!--  Introduction details Configuration  -->    <context-param>         <param-name>contextConfigLocation</param-name>         <param-value>             classpath*:/cas-client-shiro.xml        </param-value>     </context-param>        <!-- shiro Configuration   (to be written before Spring mvc servlet configuration)-->    <filter>           <filter-name>shiroFilter</filter-name>           <filter-class> org.springframework.web.filter.delegatingfilterproxy</filter-class>           <init-param>      &Nbsp;       <param-name>targetfilterlifecycle</param-name>               <param-value>true </param-value>          </init-param>       </filter>      <filter-mapping>           <filter-name>shiroFilter</filter-name>           <url-pattern>/*</url-pattern>       </filter-mapping>        spring mvc  servlet Configuring ......        <!--  Exit  -->     <filter>        <filter-name>logoutfilter</ filter-name>        <filter-class>org.springframework.web.filter.delegatingfilterproxy</ filter-class>    </filter>          <filter-mapping>        <filter-name>logoutfilter</ Filter-name>        <url-pattern>/logout</url-pattern>     </filter-mapping>        <!--  This filter is used to implement the single-point logout function, optional configuration. -->    <filter>        <filter-name >CAS Single Sign Out Filter</filter-name>         <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>         <init-param>             <param-name>targetbeanname</param-name>             <param-value>singleSignOutFilter</param-value>         </init-param>    </filter>         <filter-mapping>        < filter-name>cas single sign out filter</filter-name>         <url-pattern>/*</url-pattern>    </filter-mapping &GT;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP, ............ The rest of the configuration is omitted .......

In the above configuration whether to notice the goods: Delegatingfilterproxy, it will automatically send the filter request to the corresponding name of the bean processing. For example, at startup, Spring has a filter request, which is forwarded to the Shirofilter bean for processing. so^ Next, we'll have to find out where Shirofilter is.

This is the Cas-client-shiro.xml configuration loaded above:

    <!-- shiro configuration  -->    <bean id= "Shirofilter"  class= "Org.apache.shiro.spring.web.ShiroFilterFactoryBean" >         <property name= "SecurityManager"  ref= "SecurityManager"/>         <!--  Set a login link for a role, here is a link to the CAS login page and a configurable callback address   -->         <property name= "loginurl"  value= "${cas.casserverloginurl}?service=${cas.service }login/" />        <property name=" SUCCESSURL "  Value= "/"  />        <property name= "Filters" >             <util:map>                 <!--  Add casfilter to shirofilter consolidation  -->                <entry key= " Casfilter " value-ref=" Casfilter "/>             </util:map>        </property>          <property name= "Filterchaindefinitions" >             <value>                                 /login/ = casFilter                 /logout = anon                 /** = authc             </value>        </property>     </bean>        <!--  Casfilter for custom Single sign-on fileter -->    <bean id= "Casfilter"  class= " Com.test.shiro.filter.CasFilter ">        <!--  Failure page when configuring validation errors   -->        <property name= "FAILUREURL"   Value= "${cas.casserverlogouturl}?service=${cas.logoutredirecturl}"/>         <property name= "Realm"  ref= "Casrealm"/>    </bean>         <!-- myrealm for custom realm -->     <bean id= "Casrealm"  class= "Com.test.shiro.realm.MyRealm" >         <property name= "CacheManager" &Nbsp;ref= "Shiromemcachemanager"  />        <property  Name= "AppId"  value= "${roomy.uap.client.appid}"/>        < Property name= "Casserverurlprefix"  value= "${cas.host}"/>         <!--  Client callback address settings  -->        <property  name= "Casservice"  value= "${cas.service}login/"/>    </bean>         <!--  Cache manager  -->         ..................    <!--  Session sessiondao -->         ..................    <!--  Session Manager sessionmanager -->          ..................    <!--  Session Validation Scheduler SESSIONVALIDATIONSCHEDULER&NBSP;--&GT;&NBSP;&NBSP;&NBSP;     ..................    <!--  Security Manager securitymanager -->         ..................    <!--  Shiro Life Cycle Processor lifecyclebeanpostprocessor-->        ..................             <!--  Exit  -->     <bean name= "Logoutfilter"  class= "Com.test.cas.client.filter.LogoutFilterWithShiro" >         <property name= "Casserverlogouturl"  value= "${ Cas.casserverlogouturl} "></property>        <property  name= "RedirectURL"  value= "${cas.logoutredirecturl}" ></property>     </bean>        <!--  Single Sign-out  -->     <bean name= "Singlesignoutfilter"            class= "Com.test.cas.client.session.SingleSignOutFilter" >           <property name= "SessionMappingStorage"  ref= "Sessionmappingstorage" >        </property>     </bean>          <bean id= " Sessionmappingstorage " class=" Com.test.cas.client.session.ShiroBackedSessionMappingStorage ">            <property name= "CacheManager"  ref= " Shiromemcachemanager " />        <property name=" SessionManager " ref=" SessionManager " />    </bean>

Many of the above content omitted, in fact I want to emphasize is myrealm. Well, the authentication and pre-authorization actions we want to implement are all implemented in this custom realm. Myrealm inherited the Casrealm,casrealm and inherited the Authorizingrealm. Therefore, the authorization implementation logic is specifically written in Myrealm, and authentication invokes the method in Casrealm ...


Third, conclusion

Why not write a good blog post tutorial? First of all, the content of this aspect is very much, not one or two articles can be said clearly. Second, they are just beginning to learn, not fully mastered. Finally, I am very lazy, at present just want to leave a study notes for themselves.

This article is from the "Learning and Thinking" blog, please be sure to keep this source http://linhongyu.blog.51cto.com/6373370/1587818

Spring+shiro+cas Integrated Configuration Notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.