shiro-helloworld(1)

来源:互联网 发布:李涛疯狂淘宝上市 编辑:程序博客网 时间:2024/06/05 20:16

最近在公司搞了jfinal快四个月了, 实习也快结束了,赶紧来补一补现在热门的技术,shiro,另外电脑也新买了一个,也赶紧熟悉用idea开发。参考尚硅谷shiro视频以及shiro官方网站进行学习。

1.pom.xml

先配置springmvc+shiro的依赖包

<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>li</groupId>  <artifactId>demo</artifactId>  <packaging>war</packaging>  <version>1.0-SNAPSHOT</version>  <name>demo 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.slf4j</groupId>      <artifactId>slf4j-log4j12</artifactId>      <version>1.7.21</version>    </dependency>    <!--J2EE-->    <dependency>      <groupId>javax.servlet</groupId>      <artifactId>javax.servlet-api</artifactId>      <version>3.1.0</version>    </dependency>    <dependency>      <groupId>javax.servlet.jsp</groupId>      <artifactId>jsp-api</artifactId>      <version>2.2</version>    </dependency>    <dependency>      <groupId>javax.servlet</groupId>      <artifactId>jstl</artifactId>      <version>1.2</version>    </dependency>    <!--mysql驱动包-->    <dependency>      <groupId>mysql</groupId>      <artifactId>mysql-connector-java</artifactId>      <version>5.1.35</version>    </dependency>    <!--springframework-->    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-web</artifactId>      <version>4.2.6.RELEASE</version>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-webmvc</artifactId>      <version>4.2.6.RELEASE</version>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-context</artifactId>      <version>4.2.6.RELEASE</version>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-test</artifactId>      <version>4.2.6.RELEASE</version>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-jdbc</artifactId>      <version>4.2.6.RELEASE</version>    </dependency>    <dependency>      <groupId>com.github.stefanbirkner</groupId>      <artifactId>system-rules</artifactId>      <version>1.16.1</version>      <scope>test</scope>    </dependency>    <dependency>      <groupId>org.aspectj</groupId>      <artifactId>aspectjweaver</artifactId>      <version>1.8.9</version>    </dependency>    <!-- shiro -->    <dependency>      <groupId>org.apache.shiro</groupId>      <artifactId>shiro-core</artifactId>      <version>1.3.2</version>    </dependency>    <dependency>      <groupId>org.apache.shiro</groupId>      <artifactId>shiro-ehcache</artifactId>      <version>1.3.2</version>    </dependency>    <dependency>      <groupId>org.apache.shiro</groupId>      <artifactId>shiro-spring</artifactId>      <version>1.3.2</version>    </dependency>    <dependency>      <groupId>org.apache.shiro</groupId>      <artifactId>shiro-web</artifactId>      <version>1.3.2</version>    </dependency>    <!--其他需要的包-->    <dependency>      <groupId>org.apache.commons</groupId>      <artifactId>commons-lang3</artifactId>      <version>3.4</version>    </dependency>    <dependency>      <groupId>commons-fileupload</groupId>      <artifactId>commons-fileupload</artifactId>      <version>1.3.1</version>    </dependency>  </dependencies></project>

2.然后接着是web.xml的配置,参考spring,以及shiro的sample。

<?xml version="1.0" encoding="UTF-8"?><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">  <!--把applicationContext.xml加入到配置文件中-->  <context-param>    <param-name>contextConfigLocatiton</param-name>    <param-value>/WEN-INF/applicationContext.xml</param-value>  </context-param>  <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <servlet>    <servlet-name>spring</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>    <servlet-name>spring</servlet-name>    <url-pattern>/</url-pattern>  </servlet-mapping>  <!-- Shiro Filter is defined in the spring application context: -->  <!-- 1.配置shiro的过滤器 -->  <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></web-app>

以及通用的spring-servlet.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"       xmlns:mvc="http://www.springframework.org/schema/mvc"       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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">    <context:component-scan base-package="com.atguigu.shiro"></context:component-scan>    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/"></property>        <property name="suffix" value=".jsp"></property>    </bean>    <mvc:annotation-driven></mvc:annotation-driven>    <mvc:default-servlet-handler/></beans>

最后是applicationContext.xml配置,参考shiro的sample的spring里面的例子

<?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.xsd">    <!-- =========================================================         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)-->    <!-- 配置SecurityManager -->    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">        <property name="cacheManager" ref="cacheManager"/>        <property name="realm" ref="jdbcRealm"/>    </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 -->    <!-- 2.配置CacheManager .需要加入ehcache的jar包以及配置-->    <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. -->    <!-- 3.配置realm -->    <bean id="jdbcRealm" class="com.atguigu.shiro.reamls.ShiroRealm">    </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. -->    <!-- 4.配置lifecycleBeanPostProcessor。 可以自动的调用配置在Spring IOC容器中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: -->    <!-- 5.启用IOC容器中使用shiro的注解 -->    <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>    <!-- 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: -->    <!-- 6.配置ShrioFilter ,id必须和web.xml的DelegatingFilterProxy的name一样-->    <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"/>        <!-- 配置哪些页面需要受保护,以及访问这么页面需要的权限。        anon 可以被匿名访问        authc 被认证的即登录之后才能访问-->        <property name="filterChainDefinitions">            <value>                /login.jsp = anon                /** = authc            </value>        </property>    </bean></beans>
原创粉丝点击