Apache Shiro 集成-spring

Shiro的JavaBean兼容性使得它非常适合通过SpringXML或其他基于Spring的配置机制。Shiro应用程序需要一个具有单例SecurityManager实例的应用程序。请注意,这不会是一个静态的单例,但应该只有一个应用程序能够使用的实例,无论它是否是静态单例的。

Standalone应用程序

这里是在Spring应用程序中启用应用程序单例SecurityManager的最简单的方法:

<beanid=“myRealm"class=”…">

<beanid="securityManager"class=“org.apache.shiro.mgt.DefaultSecurityManager”>

<propertyname="realm"ref=“myRealm”/>

<beanid="lifecycleBeanPostProcessor"class=“org.apache.shiro.spring.LifecycleBeanPostProcessor”/>

<beanclass=“org.springframework.beans.factory.config.MethodInvokingFactoryBean”>

<propertyname="staticMethod"value=“org.apache.shiro.SecurityUtils.setSecurityManager”/>

<propertyname="arguments"ref=“securityManager”/>

Web应用程序

Shiro拥有对SpringWeb应用程序的一流支持。在Web应用程序中,所有Shiro可访问的请求必须通过一个主要的Shiro过滤器。该过滤器本身是极为强大的,允许临时的自定义过滤器链基于任何URL路径表达式执行。

以下是如何在基于Springweb应用程序中配置Shiro。除了其他Springweb.xml中的元素(ContextLoaderListener,Log4jConfigListener等等),定义下面的过滤器及过滤器映射:

web.xml

shiroFilter

org.springframework.web.filter.DelegatingFilterProxy

targetFilterLifecycle

true

shiroFilter

/*

在你的applicationContext.xml文件中,定义web支持的SecurityManager和’shiroFilter’bean将会被web.xml引用。

applicationContext.xml

<beanid="shiroFilter"class=“org.apache.shiro.spring.web.ShiroFilterFactoryBean”>

<propertyname="securityManager"ref=“securityManager”/>

<propertyname=“filterChainDefinitions”>

#someexamplechaindefinitions:

/admin/**=authc,roles[admin]

/docs/**=authc,perms[document:read]

/**=authc

#moreURL-to-FilterChaindefinitionshere

<beanid=“someFilter"class=”…"/>

<beanid=“anotherFilter"class=”…">…

<beanid="securityManager"class=“org.apache.shiro.web.mgt.DefaultWebSecurityManager”>

<propertyname="realm"ref=“myRealm”/>

<beanid="lifecycleBeanPostProcessor"class=“org.apache.shiro.spring.LifecycleBeanPostProcessor”/>

<beanid=“myRealm"class=”…">

EnablingShiroAnnotations

在Standalone应用程序和Web应用程序中,你可能想为安全检查使用Shiro的注释(例如,@RequiresRoles,@RequiresPermissions等等)。这需要Shiro的SpringAOP集成来扫描合适的注解类以及执行必要的安全逻辑。

以下是如何使用这些注解的。只需添加这两个bean定义到applicationContext.xml中:

<beanclass="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"depends-on=“lifecycleBeanPostProcessor”/>

<beanclass=“org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor”>

<propertyname="securityManager"ref=“securityManager”/>

SecureSpringRemoting

Shiro的Spring远程支持有两部分:配置客户端远程调用和配置服务器接收及处理远程调用。

Server-sideConfiguration

当一个远程调用方法到达启用Shiro的服务器时,与该RPC调用关联的Subject在线程执行时必须绑定到访问的接收线程。这是通过在applicationContext.xml中定义SecureRemotInvocationExecutorbean来完成的:

<beanid="secureRemoteInvocationExecutor"class=“org.apache.shiro.spring.remoting.SecureRemoteInvocationExecutor”>

<propertyname="securityManager"ref=“securityManager”/>

当你定义这个bean之后,你必须将其插入到任何你正在用来export/expose你服务的远程Exporter。Exporter实现是根据使用的远程处理机制/协议来定义的。请参阅Sping的Remoting章节关于定义Exporterbean的内容。

例如,如果使用基于HTTP的远程调用(注意secureRemoteInvocationExecutorbean的相关属性):

<beanname="/someService"class=“org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter”>

<propertyname="service"ref=“someService”/>

<propertyname="serviceInterface"value=“com.pkg.service.SomeService”/>

<propertyname="remoteInvocationExecutor"ref=“secureRemoteInvocationExecutor”/>

Client-sideConfiguration

当远程调用被执行后,Subject的识别信息必须附加到远程调用的负载上使服务器知道是谁作出了该调用。若客户端是一个基于Spring的客户端,该关联是通过Shiro的SecureRemoteInvocationFactory来完成的:

<beanid="secureRemoteInvocationFactory"class=“org.apache.shiro.spring.remoting.SecureRemoteInvocationFactory”>

在你定义好这个bean后,你需要将它插入到你正在使用的基于特定协议的SpringremotingProxyFactoryBean中。

例如,如果你正在使用基于HTTP的远程调用(注意上面定义的secureRemoteInvocationFactorybean的相关属性):

<beanid="someService"class=“org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean”>

<propertyname="serviceUrl"value=“http://host:port/remoting/someService”/>

<propertyname="serviceInterface"value=“com.pkg.service.SomeService”/>

<propertyname="remoteInvocationFactory"ref=“secureRemoteInvocationFactory”/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
shiro-redis-spring-boot-starter是一个用于集成Apache Shiro和Redis的Spring Boot Starter项目。Apache Shiro是一个强大而灵活的Java安全框架,用于身份验证、授权和会话管理等安全功能。而Redis是一个高性能的内存数据库,其具有快速的数据存取能力和持久化支持。 shiro-redis-spring-boot-starter提供了一种简化和快速集成Shiro和Redis的方式,使得在Spring Boot应用中实现安全功能变得更加容易。通过使用该Starter,我们可以方便地将Shiro的会话管理功能存储到Redis中,从而支持分布式环境下的会话共享和管理。 使用shiro-redis-spring-boot-starter可以带来以下好处: 1. 分布式环境的会话共享:通过将Shiro的会话数据存储到Redis中,不同的应用节点可以共享同一个会话,从而实现分布式环境下的会话管理和跨节点的身份验证和授权。 2. 高可用性和性能:Redis作为一个高性能的内存数据库,具有出色的数据读写能力和持久化支持,可以提供可靠的会话存储和高性能的数据访问能力。 3. 简化配置和集成shiro-redis-spring-boot-starter提供了封装好的配置和集成方式,减少了我们自己实现集成的复杂性和工作量。 总结来说,shiro-redis-spring-boot-starter为我们提供了一种简化和快速集成Shiro和Redis的方式,使得在Spring Boot应用中实现安全功能变得更加容易和高效。通过它,我们可以实现分布式环境下的会话共享和管理,提供高可用性和性能的数据存取能力,同时简化了配置和集成的复杂性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值