设为首页 收藏本站
查看: 573|回复: 0

[经验分享] 如何将Apache Shiro集成到基于Spring的应用

[复制链接]

尚未签到

发表于 2017-1-11 10:55:14 | 显示全部楼层 |阅读模式
本文主要介绍如何将Apache Shiro集成到基于Spring的应用。

Shiro兼容javabean使得它能很好的与Spring XML或其他基于Spring的配置方式集成。在基于Shiro的应用程序中的SecurityManager是单例的。不过,SecurityManager不一定是静态单例,但是不论是否是静态单例,必须保证一个应用程序中只有一个SecurityManager的实例。

独立的应用程序

下面是在Spring的应用程序中以最简单的方式配置单例SecurityManager:

<!-- 定义连接后台安全数据源的realm -->
<bean id="myRealm" class="...">
   ...
</bean>
<bean id="securityManager" class="org.apache.shiro.mgt.DefaultSecurityManager">
   <!-- 单realm应用。如果有多个realm,使用‘realms’属性代替 -->
   <property name="realm" ref="myRealm"/>
</bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<!-- 最简单的集成,是将securityManager bean配置成一个静态单例,也就是让            SecurityUtils.*
下的所有方法在任何情况下都起作用。在web应用中不要将securityManager bean配置为静态单例,
具体方式请参阅下文中的‘Web Application’部分 -->
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
   <property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager"/>
   <property name="arguments" ref="securityManager"/>
</bean>
Web应用程序

Shiro对基于Spring的Web应用提供了完美的支持,web应用中,Shiro可控制的web请求必须经过Shiro主过滤器的拦截。Shiro主过滤器本身功能十分强大,其强大之处就在于它支持任何基于URL路径表达式的、自定义的过滤器的执行。

Shiro1.0版本前,Spring web应用使用混合方式进行配置,在web.xml中定义Shiro的过滤器和它所有的配置,而在Spring XML中定义SecurityManager。这样有些别扭,因为第一你没办法将所有的配置放到一个位置;第二没法最大发挥Spring提供配置上的优点,比如PropertyPlaceholderConfigurer或者通过抽象beans来合并配置。

在Shiro1.0或更高版本中,所有的Shiro配置都放到Spring XML中,这样做进一步发挥了Spring配置机制的优势。

下面是如何在基于Spring的web应用中配置Shiro:

web.xml
除了定义ContextLoaderListener, Log4jConfigListener等Spring元素外,只要在web.xml中增加如下的filter和filter-mapping:

<!-- filter-name对应applicationContext.xml中定义的名字为“shiroFilter”的bean -->
<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-mapping>
    <filter-name>shiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
applicationContext.xml
在applicationContext.xml中定义SecurityManager和web.xml中使用的名字叫shiroFilter的bean

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    <property name="securityManager" ref="securityManager"/>
    <!-- 可根据项目的URL进行替换 -->
    <property name="loginUrl" value="/login.jsp"/>
    <property name="successUrl" value="/home.jsp"/>
    <property name="unauthorizedUrl" value="/unauthorized.jsp"/>
    <!-- 因为每个已经定义的javax.servlet.Filter类型的bean都可以在链的定义中通过bean名
    称获取,所以filters属性不是必须出现的。但是可以根据需要通过filters属性替换filter
    实例或者为filter起别名 -->
    <property name="filters">
        <util:map>
            <entry key="anAlias" value-ref="someFilter"/>
        </util:map>
    </property>
    <property name="filterChainDefinitions">
        <value>
            # some example chain definitions:
            /admin/** = authc, roles[admin]
            /docs/** = authc, perms[document:read]
            /** = authc
            # more URL-to-FilterChain definitions here
        </value>
    </property>
</bean>
<!-- 定义应用上下文的 javax.servlet.Filter beans。这些beans 会被上面定义的shiroFilter自
动感知,并提供给“filterChainDefinitions”属性使用。或者也可根据需要手动的将他们添加在
shiroFilter bean的“filters”属性下的Map标签中。 -->
<bean id="someFilter" class="..."/>
<bean id="anotherFilter" class="..."> ... </bean>
...
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
    <!-- 单realm应用。如果需要配置多个realm,使用“realms”属性 -->
    <property name="realm" ref="myRealm"/>
    <!-- 默认使用servlet容器session。下面是使用shiro 原生session的例子(细节请参考帮助文档)-->
    <!-- <property name="sessionMode" value="native"/> -->
</bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<!-- 定义连接后台安全数据源的realm -->
<bean id="myRealm" class="...">
...
</bean>
开启Shiro的注解
不论独立的应用程序还是web应用程序,都可以使用Shiro提供的注解进行安全检查。比如@RequiresRoles, @RequiresPermissions等。这些注解需要借助Spring的AOP扫描使用Shiro注解的类,并在必要时进行安全逻辑验证。

下面让我们看下如何开启注解,其实很简单,只要在applicationContext.xml中定义两个bean即可。

<!-- 开启Shiro注解的Spring配置方式的beans。在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>
Spring远程安全
Shiro的Spring远程支持有两部分组成:远程调用的客户端配置和接收、处理远程调用的服务器端配置。

Server端配置

当Shiro的Server端接收到一个远程方法调用时,与远程调用相关的Subject必须在接收线程执行时绑定到接收线程上,这项工作通过在applicationContext.xml中定义SecureRemoteInvocationExecutor bean完成。

<!-- Spring远程安全确保每个远程方法调用都与一个负责安全验证的Subject绑定 -->
<bean id="secureRemoteInvocationExecutor" class="org.apache.shiro.spring.remoting.SecureRemoteInvocationExecutor">
    <property name="securityManager" ref="securityManager"/>
</bean>
SecureRemoteInvocationExecutor定义完成后,需要将它加入到Exporter中,这个Exporter用于暴露向外提供的服务,而且Exporter的实现类由具体使用的远程处理机制和协议决定。定义Exporter beans请参照Spring的Remoting章节。

以基于HTTP的远程SecureRemoteInvocationExecutor为例。(remoteInvocationExecutor属性引用自secureRemoteInvocationExecutor)

<bean name="/someService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
    <property name="service" ref="someService"/>
    <property name="serviceInterface" value="com.pkg.service.SomeService"/>
    <property name="remoteInvocationExecutor" ref="secureRemoteInvocationExecutor"/>
</bean>
Client端配置

当远程调用发生时,负责鉴别信息的Subject需要告知server远程方法是谁发起的 。如果客户端是基于Spring的,那么这种关联可以通过Shiro的SecureRemoteInvocationFactory 完成。

<bean id="secureRemoteInvocationFactory" class="org.apache.shiro.spring.remoting.SecureRemoteInvocationFactory"/>
然后将SecureRemoteInvocationFactory 添加到与协议相关的Spring远程ProxyFactoryBean 中。

以基于HTTP协议的远程ProxyFactoryBean为例。(remoteInvocationExecutor属性引用自secureRemoteInvocationExecutor)

<bean id="someService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
    <property name="serviceUrl" value="http://host:port/remoting/someService"/>
    <property name="serviceInterface" value="com.pkg.service.SomeService"/>
    <property name="remoteInvocationFactory" ref="secureRemoteInvocationFactory"/>
</bean>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-326978-1-1.html 上篇帖子: 使用 Jersey 和 Apache Tomcat 构建 RESTful Web 服务 下篇帖子: Apache MINA (4) 接收处理请求的过程
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表