shiro的配置和使用

pom.xml的配置

<?xml version="1.0" encoding="UTF-8"?>

<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor 
license agreements. See the NOTICE file distributed with this work for additional 
information regarding copyright ownership. The ASF licenses this file to 
you under the Apache License, Version 2.0 (the "License"); you may not use 
this file except in compliance with the License. You may obtain a copy of 
the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required 
by applicable law or agreed to in writing, software distributed under the 
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 
OF ANY KIND, either express or implied. See the License for the specific 
language governing permissions and limitations under the License. -->
<!-- $Id: pom.xml 642118 2008-03-28 08:04:16Z reinhard $ -->
<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>
<packaging>war</packaging>


<name>Day1705_shiro_web_01</name>
<groupId>com.qf.shiro.web</groupId>
<artifactId>Day1705_shiro_web_01</artifactId>
<version>0.0.1-SNAPSHOT</version>


<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.7</version>
<configuration>
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>8888</port>
<maxIdleTime>30000</maxIdleTime>
</connector>
</connectors>
<webAppSourceDirectory>${project.build.directory}/${pom.artifactId}-${pom.version}</webAppSourceDirectory>
<contextPath>/</contextPath>
</configuration>
</plugin>
</plugins>
</build>


<dependencies>
<!--导入的是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-core</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.shiro</groupId>
<artifactId>shiro-spring</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-quartz</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</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-ehcache</artifactId>
<version>1.3.2</version>
</dependency>


<!--导入我们Spring的相关的包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>


<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>


<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>


<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>


<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>


<!--导入我们的servlet -->
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--这个是为了解决整合ehcache的时候出现的问题-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
</dependencies>

</project>

spring-MVC.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
         http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd"
default-autowire="byName">




<!-- 开启aop,对类代理 -->
<aop:config proxy-target-class="true"></aop:config>
<!-- 开启shiro注解支持 -->
<!-- <bean
class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
也注入了一个安全管理器
<property name="securityManager" ref="securityManager" />
</bean>
-->
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.qf"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>

</beans>

bean-base.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd"
default-autowire="byName">

<!--这里首先配置的是shiro的过滤器-->
<bean id="shiroFilter"  class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
 <!--
           用户没有通过认证的时候 访问的action  实际上应该访问我们的登陆页面
 -->
 <property name="loginUrl" value="/login.action"></property>
 <!--这个表示的是用户没有权限的时候自动跳转的页面-->
 <property name="unauthorizedUrl" value="/refuse.jsp"></property>
 <!--注入我们的安全管理器-->
 <property name="securityManager" ref="securityManager"></property>
 <!--配置我们RememberMe的过滤器-->
 <property name="filters">
<map>
<entry key="authc" value-ref="formAuthenticationFilter" />
</map>
</property>
 <!--配置的是我们的拦截器-->
 <property name="filterChainDefinitions">
    <value>
       <!--配置哪些页面只要记住用户名了就可以访问了-->
        /index.jsp=user
        /first.action=user
        /index1.action=user
        /=user
       <!--授权判断-->
       /bobo.action=perms[bobo:create]
    
       /logout.action=logout
       <!--所有页面只有认证通过了之后才能访问  如果是要拦截所有请求的话那么必须卸载最后这个位置否则没效果-->
       /**=authc
    </value>
 </property>
</bean>

<!--配置我们的安全管理器-->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
  <property name="realm" ref="customRealm1"></property>
  <!--注入我们的缓存管理-->
  <property name="cacheManager" ref="cache"></property>
  <!--注入我们的session管理器-->
  <property name="sessionManager" ref="sessionManager"></property>
  <!--注入我们的RememberMe的管理器-->
  <property name="rememberMeManager" ref="rememberMeManager"></property>
</bean>

<!--配置我们的realm-->
<bean id="customRealm1" class="com.qf.realm.CustomRealm1">
  <property name="credentialsMatcher" ref="credentialsMatcher"></property>
</bean>

<!--配置的是凭证匹配器-->
<bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
 <!--注入的是散列算法的名字-->
 <property name="hashAlgorithmName" value="md5"></property>
 <!--迭代的次数-->
 <property name="hashIterations" value="1"></property>
</bean>





<!--配置我们的缓存-->
<!--配置我们的缓存管理器   这里不能按照以前的做法直接写类而是要使用Spring中的ehcache来解决这个问题-->
<bean id="cache" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManager" ref="ehCacheManager" />
</bean>






<!--配置的是我们Spring中的ehcache的使用-->
<bean id="ehCacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml" />
<property name="shared" value="true"></property>
</bean>
<!--实现session的管理-->
<bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
  <property name="globalSessionTimeout" value="10000000"></property>
  <property name="deleteInvalidSessions" value="true"></property>
</bean>








<!--配置我们remeberMe的功能-->
<bean id="rememberMeManager" class="org.apache.shiro.web.mgt.CookieRememberMeManager">
  <property name="cookie" ref="simpleCookie"></property>
</bean>

<!--下面注入Cookie-->
<bean id="simpleCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
 <constructor-arg value="rememberMe"></constructor-arg>
 <property name="maxAge" value="2592000"></property>
</bean>

<!--配置验证表单的名字-->
<bean id="formAuthenticationFilter" class="org.apache.shiro.web.filter.authc.FormAuthenticationFilter">
 <property name="usernameParam" value="username"></property>
 <property name="passwordParam" value="password"></property>
 <property name="rememberMeParam" value="rememberMe"></property>
</bean>


</beans>

配置缓存的位置ehcache.xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">

<!--缓存到硬盘的位置-->
<diskStore path="G:\develop\ehcache" />
<!--配置的是默认所有的缓存数据的生命周期-->
<defaultCache 
maxElementsInMemory="1000" 
maxElementsOnDisk="10000000"
eternal="false" 
overflowToDisk="false" 
timeToIdleSeconds="120"
timeToLiveSeconds="120" 
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
</defaultCache>
</ehcache>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值