shiro的quick-start

来源:互联网 发布:照片批量修改大小软件 编辑:程序博客网 时间:2024/06/07 07:31

pom如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>cn.tj212</groupId>  <artifactId>shiroDemo02</artifactId>  <packaging>war</packaging>  <version>1.0-SNAPSHOT</version>  <name>shiroDemo02 Maven Webapp</name>  <url>http://maven.apache.org</url>  <dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>4.12</version>      <scope>test</scope>    </dependency>    <dependency>      <groupId>org.apache.shiro</groupId>      <artifactId>shiro-all</artifactId>      <version>1.4.0</version>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-core</artifactId>      <version>4.3.12.RELEASE</version>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-context</artifactId>      <version>4.3.12.RELEASE</version>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-beans</artifactId>      <version>4.3.12.RELEASE</version>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-web</artifactId>      <version>4.3.12.RELEASE</version>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-webmvc</artifactId>      <version>4.3.12.RELEASE</version>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-aop</artifactId>      <version>4.3.12.RELEASE</version>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-aspects</artifactId>      <version>4.3.12.RELEASE</version>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-tx</artifactId>      <version>4.3.12.RELEASE</version>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-jdbc</artifactId>      <version>4.3.12.RELEASE</version>    </dependency>    <dependency>      <groupId>net.sf.ehcache</groupId>      <artifactId>ehcache-core</artifactId>      <version>2.6.11</version>    </dependency>  </dependencies>  <build>    <finalName>shiroDemo02</finalName>  </build></project>

web.xml如下:

<!DOCTYPE web-app PUBLIC        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"        "http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"         version="3.1" metadata-complete="true">  <display-name>Archetype Created Web Application</display-name>  <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath:applicationContext.xml</param-value>  </context-param>  <servlet>    <servlet-name>DispatcherServlet</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <init-param>      <param-name>contextConfigLocation</param-name>      <param-value>classpath:spring-mvc.xml</param-value>    </init-param>  </servlet>  <servlet-mapping>    <servlet-name>DispatcherServlet</servlet-name>    <url-pattern>/</url-pattern>  </servlet-mapping>  <filter>    <filter-name>shiroFilter</filter-name>    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>    <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>  <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener></web-app>

applicationContext.xml如下:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:context="http://www.springframework.org/schema/context"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd        ">    <context:annotation-config/>    <!-- =========================================================         Shiro Core Components - Not Spring Specific         ========================================================= -->    <!-- Shiro's main business-tier object for web-enabled applications         (use DefaultSecurityManager instead when there is no web environment)-->    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">        <property name="cacheManager" ref="cacheManager"/>        <!-- Single realm app.  If you have multiple realms, use the 'realms' property instead. -->        <property name="sessionMode" value="native"/>        <property name="realm" ref="myrealm"/>    </bean>    <!-- Let's use some enterprise caching support for better performance.  You can replace this with any enterprise         caching framework implementation that you like (Terracotta+Ehcache, Coherence, GigaSpaces, etc -->    <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">        <!-- Set a net.sf.ehcache.CacheManager instance here if you already have one.  If not, a new one             will be creaed with a default config:             <property name="cacheManager" ref="ehCacheManager"/> -->        <!-- If you don't have a pre-built net.sf.ehcache.CacheManager instance to inject, but you want             a specific Ehcache configuration to be used, specify that here.  If you don't, a default             will be used.:        <property name="cacheManagerConfigFile" value="classpath:some/path/to/ehcache.xml"/> -->    </bean>    <!-- Used by the SecurityManager to access security data (users, roles, etc).         Many other realm implementations can be used too (PropertiesRealm,         LdapRealm, etc. -->    <bean id="myrealm" class="cn.tj212.shiroDemo.realm.MyRealm">    </bean>    <!-- =========================================================         Shiro Spring-specific integration         ========================================================= -->    <!-- Post processor that automatically invokes init() and destroy() methods         for Spring-configured Shiro objects so you don't have to         1) specify an init-method and destroy-method attributes for every bean            definition and         2) even know which Shiro objects require these methods to be            called. -->    <!-- 管理所有shiro相关bean的生命周期 -->    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>    <!-- Enable Shiro Annotations for Spring-configured beans.  Only run after         the lifecycleBeanProcessor has run: -->    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"          depends-on="lifecycleBeanPostProcessor"/>    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">        <property name="securityManager" ref="securityManager"/>    </bean>    <!-- Secure Spring remoting:  Ensure any Spring Remoting method invocations can be associated         with a Subject for security checks. -->    <bean id="secureRemoteInvocationExecutor" class="org.apache.shiro.spring.remoting.SecureRemoteInvocationExecutor">        <property name="securityManager" ref="securityManager"/>    </bean>    <!-- Define the Shiro Filter here (as a FactoryBean) instead of directly in web.xml -         web.xml uses the DelegatingFilterProxy to access this bean.  This allows us         to wire things with more control as well utilize nice Spring things such as         PropertiesPlaceholderConfigurer and abstract beans or anything else we might need: -->    <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="unauthorizedUrl" value="/unauthorized.jsp"/>        <!-- The 'filters' property is not necessary since any declared javax.servlet.Filter bean             defined will be automatically acquired and available via its beanName in chain             definitions, but you can perform overrides or parent/child consolidated configuration             here if you like: -->        <!-- <property name="filters">            <util:map>                <entry key="aName" value-ref="someFilterPojo"/>            </util:map>        </property> -->        <property name="filterChainDefinitions">            <value>                /favicon.ico = anon                /logo.png = anon                /shiro.css = anon                /login.jsp = anon                # allow WebStart to pull the jars for the swing app:                /*.jar = anon                # everything else requires authentication:                /** = authc            </value>        </property>    </bean></beans>

而后启动tomcat即可