ssh的shiro实际应用

本文介绍如何在SSH框架中整合Shiro进行权限管理,包括配置Shiro的拦截器、添加XML配置文件及实现基于数据库的身份认证。通过具体步骤展示如何设置登录页面、未授权页面,并提供页面权限校验的示例。

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

对于shiro的应用,在ssm和ssh大抵差不多,但是由于老系统用刀割是ssh,所以不得不重新试一次在ssh的框架上:

1.引入三个shiro的包(具体可以看ssm那一篇文章)

2.web.xml添加拦截器:

<!-- ===========================shiro==============================-->

    <!-- shiro的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>

    <!-- shiro的filter-mapping-->
    <filter-mapping>
        <filter-name>shiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
 <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

ps:务必在struts前

3.添加shiro的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"
	   xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">

	<!-- 导入数据库的相关配置 -->
	<import resource="classpath:spring/dataAccessContext-hibernate.xml"/>

	<!-- 对应于web.xml中配置的那个shiroFilter -->
	<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
		<!-- Shiro的核心安全接口,这个属性是必须的 -->
		<property name="securityManager" ref="securityManager"/>
		<!-- 要求登录时的链接(登录页面地址),非必须的属性,默认会自动寻找Web工程根目录下的"/login.jsp"页面 -->
		<property name="loginUrl" value="/base/Shiro/login.html"/>
		<!-- 登录成功后要跳转的连接(本例中此属性用不到,因为登录成功后的处理逻辑在LoginController里硬编码) -->
		<!-- <property name="successUrl" value="/" ></property> -->
		<!-- 用户访问未对其授权的资源时,所显示的连接 -->
		<!--<property name="unauthorizedUrl" value="/error/unauthorized"/>-->

		<!--<property name="filterChainDefinitions">-->
			<!--<value>-->
				<!--/admin/**=authc-->
			<!--</value>-->
		<!--</property>-->

	</bean>


	<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"></bean>
	<!-- 数据库保存的密码是使用MD5算法加密的,所以这里需要配置一个密码匹配对象 -->
	<!--<bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.Md5CredentialsMatcher"></bean>-->
	<!-- 缓存管理 -->
	<!--<bean id="shiroCacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager"></bean>-->

	<!--
    使用Shiro自带的JdbcRealm类
    指定密码匹配所需要用到的加密对象
    指定存储用户、角色、权限许可的数据源及相关查询语句
    -->
	<bean id="jdbcRealm" class="org.apache.shiro.realm.jdbc.JdbcRealm">
		<!--<property name="credentialsMatcher" ref="credentialsMatcher"></property>-->
		<property name="permissionsLookupEnabled" value="true"></property>
		<property name="dataSource" ref="dataSource"></property>
		<property name="authenticationQuery"
				  value="SELECT password FROM base_user WHERE login_name = ?"></property>
		<property name="userRolesQuery"
				  value="SELECT t.role_code from base_role t left join base_user tt on t.ROLE_CODE=tt.DEFAULT_ROLE_CODE  WHERE LOGIN_NAME = ? "></property>
		<property name="permissionsQuery"
				  value="SELECT bu.url from base_role_url_map rm LEFT JOIN base_url bu on rm.url_id = bu.url_id LEFT JOIN base_role br on br.role_id = rm.role_id where br.role_code = ?"></property>
	</bean>

	<!-- Shiro安全管理器 -->
	<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
		<property name="realm" ref="jdbcRealm"></property>
		<!--<property name="cacheManager" ref="shiroCacheManager"></property>-->
	</bean>

	<!-- Shiro的注解配置一定要放在spring-mvc中 -->

</beans>

4.在相关的页面添加:

<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>



<shiro:hasPermission name="/base/index.jsp">
			okok
		</shiro:hasPermission>
		<shiro:hasPermission name="/base/BaseUser/checkEmail.jspx">
			验证email重复
		</shiro:hasPermission>



具体文件放置如下:



其他参考资料:

ssm的shiro实际应用

http://blog.youkuaiyun.com/pengbin790000/article/details/79427535

SSH整合shiro

http://blog.youkuaiyun.com/desert568/article/details/78848551

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值