导包:shiro-core shiro-spring shiro-web commons-logging
官网可以找到配置文件模板,还有数据库建表,需要按要求建表(三个)
1.spring-shiro.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans.xsd"> 6 7 8 <!--代理类--> 9 <!--id取值要和web.xml中的filter-name一样,因为委派模式就是通过这个名字来关联委派给谁--> 10 <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> 11 <property name="securityManager" ref="securityManager"/> 12 <property name="loginUrl" value="login.jsp"/> 13 <property name="filterChainDefinitions"> 14 <!--anon表示不拦截,authc表示拦截--> 15 <value> 16 /login.jsp=anon 17 /login=anon 18 /**=authc 19 </value> 20 </property> 21 </bean> 22 23 <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> 24 <property name="realm" ref="jdbcRealm"/> 25 </bean> 26 27 <bean id="jdbcRealm" class="org.apache.shiro.realm.jdbc.JdbcRealm"> 28 <property name="dataSource" ref="dataSources"/> 29 </bean> 30 31 32 <bean id="dataSources" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 33 <property name="driverClass" value="com.mysql.jdbc.Driver"/> 34 <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/db1806"/> 35 <property name="user" value="root"/> 36 <property name="password" value="123456"/> 37 </bean> 38 39 40 </beans>
2.web.xml
1 <!DOCTYPE web-app PUBLIC 2 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 3 "http://java.sun.com/dtd/web-app_2_3.dtd" > 4 5 <web-app> 6 <display-name>Archetype Created Web Application</display-name> 7 <!--采用代理模式,将过滤操作交给SPring来完成--> 8 <!-- The filter-name matches name of a 'shiroFilter' bean inside applicationContext.xml --> 9 <filter> 10 <filter-name>shiroFilter</filter-name> 11 <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 12 <init-param> 13 <param-name>targetFilterLifecycle</param-name> 14 <param-value>true</param-value> 15 </init-param> 16 </filter> 17 18 19 <!-- Make sure any request you want accessible to Shiro is filtered. /* catches all --> 20 <!-- requests. Usually this filter mapping is defined first (before all others) to --> 21 <!-- ensure that Shiro works in subsequent filters in the filter chain: --> 22 <filter-mapping> 23 <filter-name>shiroFilter</filter-name> 24 <url-pattern>/*</url-pattern> 25 </filter-mapping> 26 27 <servlet> 28 <servlet-name>spring</servlet-name> 29 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 30 <init-param> 31 <param-name>contextConfigLocation</param-name> 32 <param-value>classpath:spring.xml</param-value> 33 </init-param> 34 <load-on-startup>1</load-on-startup> 35 </servlet> 36 37 <servlet-mapping> 38 <servlet-name>spring</servlet-name> 39 <url-pattern>/</url-pattern> 40 </servlet-mapping> 41 </web-app>