`

【shiro权限管理】3.shiro集成spring

 
阅读更多

 

shiro在web环境下最简单的使用。现在大部分项目都在是spring环境下开发的,这里在spring和springMVC框架下使用shiro

 

1.新建web项目,加入spring、springMVC、shiro相关包。

  要引入的包见附件中的pom文件

 

2.配置spring以及springMVC

  配置spring:

 守在在web.xml中配置spring

<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:applicationContext.xml</param-value>
</context-param> 
<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

 然后在src下新建applicationContext.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.xsd">
 
</beans>
 

 配置SpringMVC:

 在Web.xml添加SpringMVC的前端控制器DispatcherServlet

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

 其中加入包扫描、视图前后缀、注解的处理器适配器和映射器,以及基本的servletHandler。

 

然后在webapp下新建一个user.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>USER</title>
  </head>
  <body>
    This is User page. <br>
  </body>
</html>

部署项目,访问地址 localhost:8080/user.jsp 访问成功,证明我们的spring和springMVC基本环境搭建成功

 

3.配置shiro环境

 首先在web.xml中添加shiro的核心过滤

<!-- 
1. 配置  Shiro 的 shiroFilter.  
2. DelegatingFilterProxy 实际上是 Filter 的一个代理对象. 默认情况下, Spring 会到 IOC 容器中查找和 
<filter-name> 对应的 filter bean. 也可以通过 targetBeanName 的初始化参数来配置 filter bean 的 id. 
这里注意一点filter的位置最好放在listener前面
-->
<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>

 

在Spring的配置文件applicationContext.xml下加入Shiro的配置,由于文件中的配置比较多,不过不用着急,我们一个一个添加并分析。

(1)配置SecurityManager

首先配置的第一个是Shiro的核心组件SecurityManager,在该配置中配置了三个属性,分别是cacheManager、authenticator以及realms。之前提到过CacheManager为缓存控制器,来管理如用户、角色、权限等信息的缓存,而authenticator负责Subject认证,Realm为安全实体数据源。

<!--1. 配置 SecurityManager-->     
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
    <property name="cacheManager" ref="cacheManager"/>
    <property name="realm" ref="shiroRealm"/>
</bean>
 

(2)配置CacheManager

紧跟着下面就是cacheManager的配置,这里需要使用第三方的缓存API,如Redis或ehcache。这里我们使用的是ehcache,其jar包在附件中pom文件已经引用,在cacheManager的配置中指定cacheManagerConfigFile的配置文件为ehcache的配置文件即可:

<!--  
2. 配置 CacheManager. 
2.1 需要加入 ehcache 的 jar 包及配置文件. 
-->     
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
	<property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/> 
</bean>

 ehcache.xml配置文件在附件中,直接放在资源根目录下,其中defaultCache是默认配置,其余的是我们后面需要测试时使用的cache配置。

 

(3)配置Realm 
然后配置紧跟着是Realm的配置,由于测试样例目前不连接数据库,所以这里我们自定义一个Realm
配置:

<!-- 
3. 配置 Realm 
3.1 直接配置实现了 com.test.shiro.realms.ShiroRealm 接口的 bean
-->     
<bean id="shiroRealm" class="com.test.shiro.realms.ShiroRealm"></bean>

 里面就是我们自定义的Realm数据源,具体逻辑如下:

public class ShiroRealm implements Realm{
	@Override
	public AuthenticationInfo getAuthenticationInfo(AuthenticationToken arg0)
			throws AuthenticationException {
		return null;
	}
	@Override
	public String getName() {
		return null;
	}
 
	@Override
	public boolean supports(AuthenticationToken arg0) {
		return false;
	}
}

 实现Realm数据源类,必须继承Realm提供的org.apache.shiro.realm.Realm接口,并实现getAuthenticationInfo、getName以及supports方法(这里主要讲解框架的搭建,具体方法细节在之后会详细介绍)。

 

(4)配置LifecycleBeanPostProcessor

然后配置LifecycleBeanPostProcessor(Bean生命周期过程处理器,可以自动来调用配置在Spring IOC容器中shiro bean的生命周期方法. )

<!--  
4. 配置 LifecycleBeanPostProcessor. 可以自动来调用配置在 Spring IOC 容器中 shiro bean 的生命周期方法. 
-->       
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

 

(5)启用shiro 的注解

启用IOC容器中使用shiro的注解。但必须在配置了LifecycleBeanPostProcessor之后才可以使用: 

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

 

(6)配置ShiroFilter

记得之前我们在web.xml中加入了Shiro的过滤器,名称为“ShiroFilter”,而该bean就配置在applicationContext.xml中:

<!--  
6. 配置 ShiroFilter. 
6.1 id 必须和 web.xml 文件中配置的 DelegatingFilterProxy 的 <filter-name> 一致.
若不一致, 则会抛出: NoSuchBeanDefinitionException. 因为 Shiro 会来 IOC 容器中查找和 <filter-name> 名字对应的 filter bean.
-->     
<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="/list.jsp"/>
	<property name="unauthorizedUrl" value="/unauthorized.jsp"/>
	<!--  
		配置哪些页面需要受保护. 
		以及访问这些页面需要的权限. 
		1). anon 可以被匿名访问
		2). authc 必须认证(即登录)后才可能访问的页面. 
	-->	
       <property name="filterChainDefinitions">
		<value>
			/login.jsp = anon
			# everything else requires authentication:
			/** = authc
		</value>
	</property>
</bean>

 在上面的配置中,其中securityManager就是最上面的配置对应的bean,而loginUrl、successUrl以及unauthorizedUrl配置的是登录页面、登录成功页以及用户访问了没有被授权的资源自动跳转到的页面的路径,这里我们分别在webapp下添加这三个页面

下面的filterChainDefinitions表示的是配置哪些页面需要受保护,以及访问这些页面需要的权限。说白了它就是代表一个过滤器的过滤数据集合。在filterChainDefinitions中我们仅配置login.jsp页面为可匿名访问的页面,其它所有页面都必须认证后才可访问。

 

上面完整的Shiro基础配置如下:

<?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.xsd">
 
 
    <!--1. 配置 SecurityManager-->     
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="cacheManager" ref="cacheManager"/>
        <property name="realm" ref="shiroRealm"/>
    </bean>
 
    <!--  
    2. 配置 CacheManager. 
    2.1 需要加入 ehcache 的 jar 包及配置文件. 
    -->     
    <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
        <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/> 
    </bean>
 
 
    <!-- 
    	3. 配置 Realm 
    	3.1 直接配置实现了 org.apache.shiro.realm.Realm 接口的 bean
    -->     
    <bean id="shiroRealm" class="com.test.shiro.realms.ShiroRealm"></bean>
 
    <!--  
    4. 配置 LifecycleBeanPostProcessor. 可以自定的来调用配置在 Spring IOC 容器中 shiro bean 的生命周期方法. 
    -->       
    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
 
    <!--  
    5. 启用 IOC 容器中使用 shiro 的注解. 但必须在配置了 LifecycleBeanPostProcessor 之后才可以使用. 
    -->     
    <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>
 
    <!-- 6. 配置 ShiroFilter. 
    6.1 id 必须和 web.xml 文件中配置的 DelegatingFilterProxy 的 <filter-name> 一致.
       若不一致, 则会抛出: NoSuchBeanDefinitionException. 因为 Shiro 会来 IOC 容器中查找和 <filter-name> 名字对应的 filter bean.
    -->     
    <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="/list.jsp"/>
        <property name="unauthorizedUrl" value="/unauthorized.jsp"/>
        <!--  
        	配置哪些页面需要受保护. 
        	以及访问这些页面需要的权限. 
        	1). anon 可以被匿名访问
        	2). authc 必须认证(即登录)后才可能访问的页面. 
        -->
        <property name="filterChainDefinitions">
            <value>
                /login.jsp = anon
                # everything else requires authentication:
                /** = authc
            </value>
        </property>
    </bean>
 
</beans>

 

目前已经将Shiro所有的基础配置编写完毕,这里保存applicationContext.xml文件,并重启项目。

根据Shiro的shiroFilter中的filterChainDefinitions配置,只能访问login.jsp页面,访问其它页面都应该被重定向到login.jsp页面:至此,一个基本的Spring与Shiro的集成的效果就出来了。关于配置方面,还有很多配置以及细节没有讲解,在后面的总结中会一一拓展和讲解。

 

分享到:
评论

相关推荐

    springboot-shiro权限管理系统.zip

    总的来说,"springboot-shiro权限管理系统"是一个集成了SpringBoot、Shiro和Bootstrap的高效解决方案,它为开发者提供了便捷的身份认证、权限控制和日志管理功能,是构建企业级Web应用的理想选择。通过理解和掌握这...

    shiro权限管理框架.pdf

    8. Testing(测试支持):提供测试Shiro集成应用的支持。 9. RunAs(执行身份切换):允许用户以其他用户的身份执行操作。 10. Remember Me(记住我功能):一次登录后,下次访问无需重新登录。 Shiro的架构从外部...

    管理系统系列--Spring Boot &amp; Shiro 权限管理系统.zip

    【标题】"管理系统系列--Spring Boot & Shiro 权限管理系统.zip" 提供了一个基于Spring Boot和Apache Shiro实现的权限管理系统的实例。这个压缩包可能包含了一个完整的项目结构,用于展示如何在Java环境中构建一个...

    Shiro最新全套在线教程(1.74G)

    01.权限管理概述.avi 02.shiro认识.avi 03.使用ini完成认证.avi 04.shiro登录登出流程分析.avi 05.自定义realm登录登出.avi 06.加密realm登录登出.avi 07.RBAC权限模型理解.avi 08.ini方式检查用户拥有角色.avi 09....

    springboot集成freemarker和shiro框架

    Apache Shiro是一个强大且易用的Java安全框架,处理认证、授权、加密和会话管理,与Spring等其他框架集成良好。 **四、集成Shiro** 1. 添加依赖:在`pom.xml`中添加Shiro依赖: ```xml &lt;groupId&gt;org.apache.shiro...

    spring boot+shiro 权限认证管理案例

    通过以上步骤,我们可以构建一个集成了 Spring Boot 和 Shiro 的权限认证管理系统,能够实现用户登录、权限校验、会话管理等功能,同时利用缓存提升系统性能。在实际开发中,还可以根据需求扩展 Shiro 功能,例如...

    shiro1.7.1全包修补漏洞.rar

    Apache Shiro是一个强大的Java安全框架,它为应用程序提供了身份验证、授权、会话管理和加密服务。这个"shiro1.7.1全包修补漏洞.rar"文件包含了针对Apache Shiro 1.7.1版本的一些安全修复和更新,旨在解决可能存在的...

    shiro1.7.1.zip

    3. **shiro-lang-1.7.1.jar**:Shiro的语言支持模块,可能包含了表达式语言(EL)支持,用于权限判断和配置。 4. **shiro-config-ogdl-1.7.1.jar**:这可能是OGDL配置解析器,OGDL(Object-Graph Description ...

    shiro_tool.zip

    3. **实战代码**:可能包含一些使用Shiro实现的具体功能的代码,如登录、权限控制、会话管理等场景的示例。 4. **自定义 Realm**:Shiro通过Realm与应用程序的数据源交互,获取用户信息。如果包含自定义Realm,那...

    spring mvc shiro的配置使用.zip_DEMO_spring mvc_spring mvc shiro

    - **配置 Shiro**:首先,我们需要在 Spring 的配置文件中声明并配置 SecurityManager,通常会使用 `org.apache.shiro.spring.web.ShiroFilterFactoryBean`。同时,定义 Realm,比如 `AuthorizingRealm`,与数据库...

    【SpringBoot】廿三、SpringBoot中整合Shiro实现权限管理.pdf

    【SpringBoot】与Apache Shiro整合进行权限管理是构建安全Web应用的一种常见方式。Shiro是一个轻量级、易用的Java安全框架,提供身份...这样,你的Spring Boot应用就能利用Shiro的强大功能,实现安全、高效的权限管理。

    shiro与spring web 项目集成.pdf

    &lt;bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"&gt; &lt;property name="loginUrl" value="/user/login"&gt;&lt;/property&gt; &lt;property name="unauthorizedUrl" value="/refuse.jsp"&gt; ...

    shiro-1.7.1.zip

    3. **shiro-lang-1.7.1.jar**: 该组件包含了Shiro的表达式语言支持,如在角色和权限定义时使用SpEL(Spring Expression Language)或者Shiro自己的表达式语言。 4. **shiro-config-ogdl-1.7.1.jar**: OGDL(Open ...

    SpringMVC+Shiro权限管理.

    ### SpringMVC+Shiro权限管理系统详解 #### 权限管理概述 权限管理在软件开发中扮演着至关重要的角色,特别是在涉及多用户交互的应用场景下。通过合理的权限控制,可以确保不同用户仅能访问和操作他们被授权的功能...

    【SpringBoot】廿三、SpringBoot中整合Shiro实现权限管理.docx

    - 引入`shiro-spring`以集成Shiro权限管理。 - 引入`thymeleaf-extras-shiro`,以便在HTML模板中使用Shiro标签。 注意:依赖版本应与你的SpringBoot版本兼容,这里使用的是SpringBoot 2.3.1。 2. **配置Shiro**...

    1.JAVA后台管理系统,企业开发首选脚手架2.后台采用Springboot框架+ Shiro权限+ Mybatis.zip

    系统的核心是采用了Springboot框架,Shiro权限管理和Mybatis持久层框架。下面我们将详细探讨这些技术栈的知识点。 **Springboot框架** Springboot是由Pivotal团队提供的全新框架,旨在简化Spring应用的初始搭建以及...

    shiro实战教程资料.zip

    这些示例将展示如何在Spring Boot应用中集成Shiro,实现用户的登录验证、权限控制以及页面的访问权限检查等功能。 学习这些资料,你将能掌握如何在Java应用中使用Shiro进行身份验证和授权,了解如何配置Shiro以适应...

    shiro简单的demo.zip

    2. **SpringBoot与Shiro集成**:通过Spring Boot的AutoConfiguration特性,简化Shiro的配置。 3. **Shiro的认证流程**:如何通过自定义Realm实现用户身份的验证。 4. **Shiro的授权机制**:角色和权限的管理,以及...

    Spring Boot整合Shiro搭建权限管理系统.pdf

    在本文中,我们将深入探讨如何使用Spring Boot与Apache Shiro框架来构建一个权限管理系统。Spring Boot因其简洁的配置和快速的开发效率而受到广大开发者喜爱,而Shiro则是一个轻量级的安全框架,用于实现身份认证、...

Global site tag (gtag.js) - Google Analytics