Shiro初步5_与spring的整合

本文介绍如何使用Spring管理Shiro的配置,通过示例展示如何定义权限解析器、凭证匹配器、用户认证及授权等核心组件,并配置过滤器实现登录、登出等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.spring-shiro

及将shiro.ini中的内容使用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:mybatis="http://mybatis.org/schema/mybatis-spring"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">

    <bean id="urlPermissionResolver" class="org.shiro.permission.UrlPermissionResovler" />
    <bean id="hashMatcher"
        class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
        <property name="hashAlgorithmName" value="md5" />
    </bean>
    <bean id="userRealm" class="org.shiro.realm.UserRealm">
        <property name="credentialsMatcher" ref="hashMatcher" />
    </bean>



    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="userRealm" />
        <property name="authorizer.permissionResolver" ref="urlPermissionResolver" />
    </bean>

    <bean id="resourceCheckFilter" class="org.shiro.filter.ResourceCheckFilter">
        <property name="errorUrl" value="/unauth.jsp" />
    </bean>


    <bean id="shiroFilter"  class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">  
        <property name="securityManager" ref="securityManager" />
        <property name="loginUrl" value="/login" />
        <!-- override these for application-specific URLs if you like: <property 
            name="loginUrl" value="/login.jsp"/> <property name="successUrl" value="/home.jsp"/> 
            <property name="unauthorizedUrl" value="/unauthorized.jsp"/> -->
        <!-- The 'filters' property is not necessary since any declared javax.servlet.Filter 
            bean -->
        <!-- defined will be automatically acquired and available via its beanName 
            in chain -->
        <!-- definitions, but you can perform instance overrides or name aliases 
            here if you like: -->
        <!-- <property name="filters"> <util:map> <entry key="anAlias" value-ref="someFilter"/> 
            </util:map> </property> -->
        <property name="filterChainDefinitions">
            <value>
                /admin/**=authc,resourceCheckFilter
                /login=anon
                /logout = logout
            </value>
        </property>
    </bean>
    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
</beans>

2.web.xml

使spring-shiro同spring其他xml文件一起加载


3.

https://github.com/Amant-huangqi/shiro_spring

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值