关于spring 中集成shrio注意点

  1. 项目中需要包含shrio包

    WebContent.WEB-INF.lib.shrio-all-1.2.1.jar

  2. 在项目的web.xml文件中加入

    <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>

    其中 <filter-name>shiroFilter</filter-name>需要与之后的xml配置中的名字相关联

    <url-pattern>/*</url-pattern>将会拦截所有url,这里可能会由于之后xml配置中的url拦截设置出问题。、

  3. 在config-log.xml配置文件中(此配置文件是当前项目特有的,相当于spring对应的正规.xml配置文件)添加

    首先创建一个bean,名字关联2中的filter-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="/main" /> -->
      <property name="unauthorizedUrl" value="/login.jsp" />
      <property name="filterChainDefinitions">
       <value>
        /login = anon
        /logout = logout
            /view/** = anon
            /UI/** = anon
            /user/login = anon

    <!-- /login = anon                        (这里开始被注释掉)
        /logout = logout
        /images/** = anon
        /scripts/** = anon
        /frames/** = anon
        /Flex/** = anon
        /Flex/**/** = anon
        /Flex/apps/qxgt/** = anon
        /Flex/**/**/** = anon
        /Flex/**/**/**/** = anon
        /Flex/widgets/DKAnalysis/DKAnalysisWidget.swf = anon
        /*.swf = anon
        /*.xml = anon
        /logout = logout
        /login = anon
        /** = anon                                   (如果此行不被注释掉,将引起问题,与2中拦截所有url有关联)
           /view/** = user
           /** = user
           -->
           
        </value>
      </property>
     </bean>

  4. 继续3,继续添加bean

    定义自定义的realm,后面的class是自己编写的类,其中有需要注入的dao层实例

    <!--自定义Realm 继承自AuthorizingRealm -->
     <bean id="myDaoRealm" class="cn.stargis.estar.basic.log.realm.myrealm">
      <property name="logindao" ref="logtestdao"></property>
     </bean>

    利用自定义的realm制作securityManager
     <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
      <!--设置自定义realm -->
      <property name="realm" ref="myDaoRealm" />
     </bean>

  5. 继续4,继续添加bean

    此处相当于securityManager的factory的生成,此处如果正常工作,在代码中就可直接得到subject然后进行login,集成spring之前需要factory.xx

    <!-- securityManager -->
     <bean
      class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
      <property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager" />
      <property name="arguments" ref="securityManager" />
     </bean>

至此,需要配置的内容已经完毕

时间: 2024-08-29 16:41:20

关于spring 中集成shrio注意点的相关文章

spring 中集成quartz定时器及quartz中cronExpression配置说明

 spring 中集成quartz: 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" xmlns:p="http:/

spring中集成使用jedis(2)

本文主要就spring注入的连接池使用问题,做简要说明. 使用过JedisPool的同学会发现,通过JedisPool获取资源,在使用完毕后,需要显式的将资源放回连接池中, 如下: JedisPool jedisPool; Jedis jedis = jedisPool.getResource(); //操作 jedisPool.returnResource(jedis); 如果未显示的回收资源,则在连接池中资源使用完毕后,系统会出现阻塞. 因为如果使用连接池,就相当于将客户端连接托管给池,而池

spring中集成shiro进行安全管理

shiro是一款轻量级的安全框架,提供认证.授权.加密和会话管理四个基础功能,除此之外也提供了很好的系统集成方案. 下面将它集成到之前的demo中,在之前spring中使用aop配置事务这篇所附代码的基础上进行集成 一.添加jar包引用 修改pom.xml文件,加入: <!-- security --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core&

基于Maven在Spring中集成CXF Web Service框架

引言: 在跨系统和跨平台的系统通信中,WebService是一个事实上的标准,其以平台无关性,获得了广泛的应用.本文将讲述如何基于Spring来集成CXF,并开发出第一个Hello World的应用. 1.  Web Service是什么? Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配置这些应用程序,用于开发分布式的互操作的应用程序. Web Service技术, 能使

spring中集成使用jedis(1)

本文主要说明在项目中通过spring集成jedis的使用方法. 首先,在jedis的连接配置中,出于性能考虑选择连接池形式.jedis中的连接池类为 redis.clients.jedis.JedisPool 其中包含多个构造器,可根据需要自行选取,这里选择较为基础形式: public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, final int port) { this(poolConfig,

在Spring中集成shiro

1.在pxm添加集成导入 <!-- shiro的支持包 --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-all</artifactId> <version>1.4.0</version> <type>pom</type> </dependency> <!-- shiro与Spr

Spring中集成Shiro授权实例

授权流程回顾 首先说一句,使用授权的前提当然是先要实现身份验证,也就是要保证用户登录之后才可能考虑授权的问题.关于身份验证之前已经写过了,还不清楚的童鞋可以点这里 上一篇文章介绍了Shiro中授权的一些基础知识和原理.学了就要用,本篇文章就介绍如何在项目中应用Shiro的授权.这里为了方便大家阅读,先贴出上一篇文章中分析出的授权流程: 当我们调用Subject.hasRole(...)后 首先会委托给securityManager来处理,而securityManager内部有一个Authoriz

细说shiro之五:在spring框架中集成shiro

官网:https://shiro.apache.org/ 1. 下载在Maven项目中的依赖配置如下: <!-- shiro配置 --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>${version.shiro}</version> </dependency&g

Spring Boot集成Jasypt安全框架

Jasypt安全框架提供了Spring的集成,主要是实现 PlaceholderConfigurerSupport类或者其子类. 在Sring 3.1之后,则推荐使用PropertySourcesPlaceholderConfigurer类作为属性替换配置类,这里Spring集成Jasypt则使用Jasypt对属性替换配置类的实现.EncryptablePropertySourcesPlaceholderConfigurer. 在Spring中集成比较容易,而且Jasypt官方也给出了配置Bea