8.shiro集成spring,web

1.web集成地址:http://shiro.apache.org/web.html
讲述了怎么配置web.xml filter等 已经jsp引入shiro标签的使用

2.Apache Shiro集成到spring http://shiro.apache.org/spring.html

本页面介绍了如何将Shiro集成到基于spring的应用程序中。
Shiro的JavaBeans兼容性使其非常适合通过Spring XML或其他基于Spring的配置机制进行配置。Shiro应用程序需要一个单例SecurityManager实例。注意,这并不一定是一个静态单例,但是应用程序应该只使用一个实例,不管它是不是一个静态单例。
Standalone Applications 独立的项目
下面是在Spring应用中启用单例SecurityManager的最简单方法:

<!-- 配置你自己的Realm路径.Define the realm you want to use to connect to your back-end security datasource: -->
<bean id="myRealm" class="...">
    ...
</bean>

<bean id="securityManager" class="org.apache.shiro.mgt.DefaultSecurityManager">
    <!-- 配置一个或者多个realm属性.Single realm app.  If you have multiple realms, use the 'realms' property instead. -->
    <property name="realm" ref="myRealm"/>
</bean>

<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

<!-- For simplest integration, so that all SecurityUtils.* methods work in all cases, -->
<!-- make the securityManager bean a static singleton.  DO NOT do this in web web项目不要定义单例        -->
<!-- applications - see the 'Web Applications' section below instead.                 -->
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager"/>
    <property name="arguments" ref="securityManager"/>
</bean>

Web Applications web项目
Shiro对Spring web应用程序有一流的支持。在web应用程序中,所有可访问Shiro的web请求都必须经过一个主Shiro过滤器。这个过滤器本身非常强大,允许基于任何URL路径表达式执行自定义过滤器链。
在Shiro 1.0之前,必须在Spring web应用程序中使用混合方法定义Shiro过滤器 它的所有配置属性都在web.xml中,但是在Spring XML中定义了SecurityManager。这有点令人沮丧,因为您不能1)将您的配置合并到一个地方,2)利用更高级的Spring特性(如PropertyPlaceholderConfigurer或抽象bean)的配置功能来合并通用配置
现在在Shiro 1.0以及之后的版本中,所有Shiro配置都是在Spring XML中完成的,提供了对更健壮的Spring配置机制的访问。
下面是如何在一个基于spring的web应用程序中配置Shiro:
web.xml
除了您的其他Spring web.xml元素(ContextLoaderListener、Log4jConfigListener等)之外,还定义了以下过滤器和过滤器映射:

<!-- The filter-name matches name of a 'shiroFilter' bean inside applicationContext.xml -->
<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>

...

<!-- Make sure any request you want accessible to Shiro is filtered. /* catches all -->
<!-- requests.  Usually this filter mapping is defined first (before all others) to -->
<!-- ensure that Shiro works in subsequent filters in the filter chain:             -->
<filter-mapping>
    <filter-name>shiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

applicationContext.xml
在您的applicationContext.xml文件中,定义web启用的SecurityManager和将从web.xml引用的“shiroFilter”bean。

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    <property name="securityManager" ref="securityManager"/>
    <!-- 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"/>  #下面定义的bean
        </util:map>
    </property> -->
    <property name="filterChainDefinitions">
        <value>
            # some example chain definitions: 权限和角色拦截设置
            /admin/** = authc, roles[admin]     拦截url /admin/**,只要角色admin可以访问
            /docs/** = authc, perms[document:read]  权限时document:read可访问
            /** = authc                             登陆后才可以访问
            # more URL-to-FilterChain definitions here
        </value>
    </property>
</bean>

<!-- Define any javax.servlet.Filter beans you want anywhere in this application context.   -->
<!-- They will automatically be acquired by the 'shiroFilter' bean above and made available -->
<!-- to the 'filterChainDefinitions' property.  Or you can manually/explicitly add them     -->
<!-- to the shiroFilter's 'filters' Map if desired. See its JavaDoc for more details.       -->
<bean id="someFilter" class="..."/>      #定义的bean它们将被上面的“shiroFilter”bean自动获取到filterChainDefinitions
<bean id="anotherFilter" class="..."> ... </bean>
...

<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
    <!-- Single realm app.  If you have multiple realms, use the 'realms' property instead. -->
    <property name="realm" ref="myRealm"/>
    <!-- By default the servlet container sessions will be used.  Uncomment this line
         to use shiro's native sessions (see the JavaDoc for more): -->
    <!-- <property name="sessionMode" value="native"/> -->
</bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

<!-- Define the Shiro Realm implementation you want to use to connect to your back-end -->
<!-- security datasource: -->  #自定义的realm路径
<bean id="myRealm" class="...">
    ...
</bean>

Enabling Shiro Annotations shiro注解开启
在独立应用程序和web应用程序中,您可能希望将Shiro的注释用于安全检查(例如,@RequiresRoles、@ requirespermission等)。这需要Shiro的Spring AOP集成来扫描适当的带注释的类,并根据需要执行安全逻辑。
下面是如何启用这些注释。只需将这两个bean定义添加到applicationContext.xml:

<!-- Enable Shiro Annotations for Spring-configured beans.  Only run after -->
<!-- the lifecycleBeanProcessor has run: -->
<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>

Secure Spring Remoting
Shiro的Spring remoting支持包括两部分:客户端进行远程调用的配置和服务器接收和处理远程调用的配置。
Server-side Configuration
当远程方法调用进入启用shiro的服务器时,必须将与该RPC调用关联的主题绑定到接收线程,以便在线程执行期间进行访问。这是通过定义Shiro的SecureRemoteInvocationExecutor bean在applicationContext.xml:

<!-- Secure Spring remoting:  Ensure any Spring Remoting method invocations -->
<!-- can be associated with a Subject for security checks. -->
<bean id="secureRemoteInvocationExecutor" class="org.apache.shiro.spring.remoting.SecureRemoteInvocationExecutor">
    <property name="securityManager" ref="securityManager"/>
</bean>

一旦定义了此bean,就必须将其插入到用于导出/公开服务的任何remoting导出器。导出器实现是根据使用的remoting机制/协议定义的。参见Spring的Remoting一章关于定义出口商bean。
例如,如果使用基于http的remoting(注意secureRemoteInvocationExecutor bean的属性引用):

<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-side Configuration客户端配置
当执行远程调用时,必须将主题标识信息附加到remoting有效负载,以便让服务器知道是谁在进行调用。如果客户端是基于spring的客户端,则通过Shiro的SecureRemoteInvocationFactory进行关联

<bean id="secureRemoteInvocationFactory" class="org.apache.shiro.spring.remoting.SecureRemoteInvocationFactory"/>:

定义了这个bean之后,需要将它插入到正在使用的特定于协议的Spring remoting ProxyFactoryBean。
例如,如果您使用基于http的remoting(注意上面定义的secureRemoteInvocationFactory bean的属性引用):

<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>
发布了10 篇原创文章 · 获赞 6 · 访问量 355

猜你喜欢

转载自blog.csdn.net/qq_45394274/article/details/104577180
今日推荐