shiro 配置总结

1.web.xml中配置xml文件的路径和过滤器

<!-- Spring ApplicationContext配置文件的路径,可使用通配符,多个路径用,号分隔
        此参数用于后面的Spring Context Loader -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath*:/applicationContext.xml
            classpath*:/applicationContext-shiro.xml
        </param-value>
    </context-param>

 

<!-- Shiro Security filter-->
    <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>

 

2.applicationContext-shiro.xml中配置


    <!-- 项目自定义的Realm -->
    <bean id="shiroDbRealm" class="com.pqc.tbk.service.account.ShiroDbRealm">
        <property name="userService" ref="userService"></property>
    </bean>

    <!-- 用户授权信息Cache 可以省略-->
    <bean id="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager" />

    <!-- Shiro's main business-tier object for web-enabled applications -->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"       depends-on="userDao">
        <property name="realm" ref="shiroDbRealm" />
        <property name="cacheManager" ref="cacheManager" />
    </bean>
    

    <!-- Shiro Filter -->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager" />
        <property name="loginUrl" value="/user/loginTest" />
        <property name="successUrl" value="/" />
        <property name="filterChainDefinitions">
            <!-- 访问这些路径需要哪些哪些权限认证 此处很关键*****-->
            <value>
                /user/loginTest = authc
                /user/logout = logout
                /static/** = anon
                /** = user
             </value>
        </property>
    </bean>
    
    <!-- AOP式方法级权限检查   这两个类主要用于注解-->
    <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>
    <!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

 

3.spring-mvc.xml中配置

 

<!-- shiro -->
    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  
        <property name="exceptionMappings">  
            <props>  
                <prop key="org.apache.shiro.authz.UnauthorizedException">error/403</prop>  
            </props>  
        </property>  
    </bean> 


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

 

4.define your ShiroDbRealm

 

5.pox.xml

<!-- SECURITY begin -->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-core</artifactId>
            <version>${shiro.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>${shiro.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-web</artifactId>
            <version>${shiro.version}</version>
        </dependency>
        <!-- SECURITY end -->

在使用Shiro配置Redis时,可以按照以下步骤进行操作: 1. 首先,将shiro-redis-tutorial克隆到本地磁盘上,使用以下命令来克隆项目:git clone https://github.com/alexxiyang/shiro-redis-tutorial.git 2. 打开克隆下来的项目,并进入src/main/resources目录下。 3. 在resources目录下,可以找到shiro.ini配置文件。这个文件用于配置ShiroShiro-Redis。可以根据自己的需要对文件进行修改。 4. 如果需要详细了解ShiroShiro-Redis的配置,可以参考Shiro官网或者这个博客:https://blog.youkuaiyun.com/LHacker/article/details/10438387 5. 根据项目的需求,可以使用开源项目中的类来实现Redis作为缓存,缓存用户的权限和session信息。同时,还可以使用开源项目中的其他功能,如并发登录限制和密码错误次数限制。 6. 在整合过程中,首先需要进行Redis的配置。可以根据项目的具体情况,对Redis进行相关的优化。 总结:要配置Shiro使用Redis作为缓存,可以参考shiro.ini配置文件,并根据需要进行修改。在整合过程中,可以使用开源项目中的类来实现Redis的相关功能,并根据项目的需求进行优化。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [shiro-redis-tutorial:shiro-redis教程](https://download.youkuaiyun.com/download/weixin_42117032/18665099)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Shiro配置以及redis配置](https://blog.youkuaiyun.com/wucaifang819787/article/details/82050394)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Shiro使用Redis作为缓存(解决Shiro频繁访问Redis)(十一)](https://blog.youkuaiyun.com/qq_45822970/article/details/109582568)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值