Apache Shiro Simple Example

Source: Internet
Author: User

1. Create a new MAVEN War project and add the following dependencies to the Pom.xml.

<dependency> <groupid>org.springframework</groupid> <artifactid>spring-web </artifactId> <version>4.0.9.RELEASE</version></dependency><dependency>  <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId>  <version>1.2.3</version></dependency><dependency> <groupId> Javax.servlet</groupid> <artifactid>javax.servlet-api</artifactid> <version> 3.1.0</version></dependency><dependency> <groupid>org.apache.shiro</groupid > <artifactid>shiro-quartz</artifactid> <version>1.2.3</version></ Dependency><dependency> <groupid>commons-collections</groupid> <artifactid >commons-collections</artifactId> <version>3.2.1</version></dependency>< Dependency> <groupid>javax. servlet.jsp</groupid> <artifactid>jsp-api</artifactid> <version>2.2</ Version></dependency>

         2, add the following in Web. Xml.

<context-param> <param-name> Contextconfiglocation</param-name> <param-value>classpath:spring-core.xml</param-value> </context-param><listener> <listener-class> Org.springframework.web.context.contextloaderlistener</listener-class></listener><filter>  <filter-name>shiroFilter</filter-name> <filter-class> org.springframework.web.filter.delegatingfilterproxy</filter-class> <async-supported>true< /async-supported> <init-param>  <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> 

3, added two pages, respectively, login.jsp and index.jsp, mainly used to display the login interface and the main interface.

login.jsp

<% @page language= "java" pageencoding= "UTF-8"%>

index.jsp

<% @page language= "java" pageencoding= "UTF-8"%><% @taglib prefix= "Shiro" uri= "Http://shiro.apache.org/tags" %>

4, added a Userrealm class, for the user login operation to achieve the relevant check. In order to simplify, the user name, password, and salt values are written dead. User name: admin, Password: 123456

package org.cloud.shiro.realm;import org.apache.shiro.authc.authenticationexception;import  org.apache.shiro.authc.authenticationinfo;import org.apache.shiro.authc.authenticationtoken;import  Org.apache.shiro.authc.simpleauthenticationinfo;import org.apache.shiro.authz.authorizationinfo;import  org.apache.shiro.realm.authorizingrealm;import org.apache.shiro.subject.principalcollection;import  org.apache.shiro.util.bytesource;public class userrealm extends authorizingrealm{  protected authorizationinfo dogetauthorizationinfo (principalcollection principals)  {   return null; } protected authenticationinfo dogetauthenticationinfo ( Authenticationtoken token)  throws AuthenticationException {  String  Username= (String)  token.getprincipal ();   simpleauthenticationinfo authenticationinfo=new  simpleauthenticationinfo (UsernAme, "123456", ByteSource.Util.bytes (username+ "8d78869f470951332959580424d4bf4f"), GetName ());   return  authenticationinfo; }}

        5, new Spring-core.xml file, mainly used for Shiro configuration. Note that you need to configure the Userrealm class written above.

<?xml version= "1.0"  encoding= "UTF-8"? ><beans xmlns= "http://www.springframework.org/ Schema/beans " xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance " xsi:schemalocation="/HTTP/ Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd " > <!-- realm Implementation  --> <bean id= "Userrealm"  class= " Org.cloud.shiro.realm.UserRealm " /> <!--  session ID Generator  --> <bean id=" Sessionidgenerator "  class=" Org.apache.shiro.session.mgt.eis.JavaUuidSessionIdGenerator " />  <!--  Session cookie Template  --> <bean id= "Sessionidcookie"  class= " Org.apache.shiro.web.servlet.SimpleCookie ">  <constructor-arg value=" Sid " />   <property name= "HttpOnly"  value= "true"  />  <property name= " MaxAge " value=" 180000 " /> </bean> <!--  session dao --> <bean id= "Sessiondao"   class= " Org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO ">  <property name=" Activesessionscachename " value=" Shiro-activesessioncache " />  <property name=" Sessionidgenerator " ref=" Sessionidgenerator " /> </bean> <!--  Session Validation Scheduler  --> <bean id= "Sessionvalidationscheduler"   class= " Org.apache.shiro.session.mgt.quartz.QuartzSessionValidationScheduler ">  <property name=" Sessionvalidationinterval " value=" 1800000 " />  <property name=" SessionManager "  ref= "SessionManager"  /> </bean> <!--  Session Manager  --> <bean  id= "SessionManager"   class= "Org.apache.shiro.web.session.mgt.DefaultWebSessionManager" >   <property name= "Globalsessiontimeout"  value= "1800000" &NBSP;/&GT;&NBSP;&NBsp;<property name= "Deleteinvalidsessions"  value= "true"  />  <property  Name= "sessionvalidationschedulerenabled"  value= "true"  />  <property name= " Sessionvalidationscheduler " ref=" Sessionvalidationscheduler " />  <property name = "Sessiondao"  ref= "Sessiondao"  />  <property name= "sessionIdCookieEnabled"  value= "true"  />  <property name= "Sessionidcookie"  ref= "SessionIdCookie"  /> </bean> <!--  Security Manager  --> <bean id= "SecurityManager"  class= "Org.apache.shiro.web.mgt.DefaultWebSecurityManager" >  <property name= "Realm"  ref= "Userrealm"  />  <property name= "SessionManager"  ref= "SessionManager"  /> </bean> <bean id= "Formauthenticationfilter"   class= " Org.apache.shiro.web.filter.authc.FormAuThenticationfilter ">  <property name=" Usernameparam " value=" username " />   <property name= "Passwordparam"  value= "password"  /> </bean>  <bean id= "Shirofilter"  class= "Org.apache.shiro.spring.web.ShiroFilterFactoryBean" >   <property name= "SecurityManager"  ref= "SecurityManager"  />  <property  Name= "loginurl"  value= "/login.jsp"  />  <property name= "SuccessUrl"  value= " /index.jsp " />  <property name=" Filters ">   <map>     <entry key= "authc"  value-ref= "Formauthenticationfilter" ></entry>    </map>  </property>  <property name= " Filterchaindefinitions ">   <value>    /login.jsp = authc     /logout = Logout    /** = user   </value>  </property > </bean> <!-- shiro life cycle Processor  --> <bean id= " Lifecyclebeanpostprocessor " class=" Org.apache.shiro.spring.LifecycleBeanPostProcessor " /></ Beans>

6, after the start of the project, after accessing login.jsp, the login screen will be displayed, enter the user name and password, you can enter the Index.jsp page. Of course, click the Exit button and return to the login page. If the system is not logged in, direct access to the index.jsp in the address bar is returned to the login page.

In this case, a simple Shiro example is built, and you can extend the content according to the actual needs of the project. This article is a brief list of Shiro examples, mainly for Shiro first learner to do an opening example, in order to grasp the Shiro, but also need to learn more, more practice.

Apache Shiro Simple Example

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.