Shiro整合SSH开发1:整合Shiro+Struts2+Spring+Hibernate 初步整合配置【基于Maven】

来源:互联网 发布:2015乡村旅游数据统计 编辑:程序博客网 时间:2024/05/09 08:14

整合SSH(Struts2 + Spring + Hibernate):这个是基于配置文件在spring中配置Shiro的bean

前言:

     我看的视频教程中使用的是SSM(Spring+SpringMVC+mybaties),但是我目前用的是SSH,特别是Struts2,在学习的时候特别不方便,后面在网络上面查找对应的文章,大多都是SpringMVC的,关于SSH的写的东西都不够详尽,以我的基础还不能够理解。因此我自己根据视频教程中的配置流程一步步转化为关于SSH的配置。(虽然初步配置完成后发现和Controller层的关系不大,但是写下这篇文章还是觉得挺有成就感的。)
     这是一个基础的配置,直到基础配置完成为止,我并没有进行一个详细的认证和授权的流程,但是也希望通过这篇文章能帮助到大家。如果大家觉得有哪里不正确的可以在下面的评论区提出,我会进行审查和更改,希望和各位一起进步!
老猫

此文老猫原创,转载请加本文连接:http://blog.csdn.net/nthack5730/article/details/51002218
更多有关老猫的文章:http://blog.csdn.net/nthack5730



加入依赖

     首先要在Maven中加入Shiro对Spring和WEB的依赖。下面给出所有Shiro需要的依赖【SSH原本的依赖在这里为了节省篇幅就不贴上来了】,pom.xml:

Edit
<!-- 给出Shiro的版本 -->
<properties>
    <shiro.version>1.2.3</shiro.version>
</properties>

<!-- Shiro的依赖 -->
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-core</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-web</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-spring</artifactId>
</dependency>




     注意:和当使用Shiro的时候去除原工程的授权和拦截器。【用shiro就不要用其他的了,不然后期很难维护,规范很重要】



配置web.xml:

     配置Shiro的filter,因为在web系统中,Shiro也是通过filter进行拦截。【Shiro会提供很多filter】
     下面的filter拦截后,会将操作权给我等等在下面贴出的applicationContext-shiro.xml(Spring中配置的filter,过滤链)
     在web.xml中加入Shiro的配置:
Edit
<!-- Shiro的filter -->
<!-- shiro过虑器,DelegatingFilterProx会从spring容器中找shiroFilter -->
<filter>
    <filter-name>shiroFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>

    <!-- 设置true由servlet容器控制filter的生命周期 -->
    <init-param>
        <param-name>targetFilterLifecycle</param-name>
        <param-value>true</param-value>
    </init-param>

    <!-- 设置spring容器filter的beanID,如果不设置则默认自动找与filter name一致的bean,指定更加稳定快速 -->
    <init-param>
        <param-name>targetFilterLifecycle</param-name>
        <param-value>shiroFilter</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>shiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>




在Spring中配置filter:

     刚刚上面是在web.xml中配置filter的,现在要Spring中配置filter:applicationContext-shiro.xml【在下面会讲讲Shiro的内置过滤器】
     在applicationContext-shiro.xml中配置在web.xml中对应的spring容器中的bean,在这里我用的是spring 4.0,所以xml文件对应的schema也是4.0版本的,需要配置的内容有:
Edit
<!-- web.xml中shiro的filter对应的bean -->
<!-- SecurityManager -->
<!-- Realm -->


     具体配置如下:【整合的时候最基础的配置】
Edit
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd"
>

    <!-- web.xml中shiro的filter对应的bean -->
    <!-- Shiro 的Web过滤器 -->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <!-- 指定安全管理器 -->
        <property name="securityManager" ref="securityManager" />

        <!-- 如果没有认证将要跳转的登陆地址,http可访问的url,如果不在表单认证过虑器FormAuthenticationFilter中指定此地址就为身份认证地址 -->
        <!-- loginUrl认证提交地址,如果没有通过认证将会请求此地址进行认证,请求地址酱油formAuthenticationFilter进行表单认证 -->
        <property name="loginUrl" value="/login.action" />

        <!-- 认证通过后,指定如果没有权限跳转的地址 -->
        <property name="unauthorizedUrl" value="/refuse.jsp" />

        <!-- shiro拦截器配置 -->
        <property name="filters">
            <map>
                <entry key="authc" value-ref="formAuthenticationFilter" />
            </map>
        </property>

        <!-- 真正的filter,也是过滤链,从上向下顺序执行,一般都将/**放在最下边 -->
        <property name="filterChainDefinitions">
            <value>
                <!-- 所有的URL都可以匿名访问 -->
                /** = anon
            </value>
        </property>
    </bean>


    <!-- 安全管理器 -->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <!-- 注入realm -->
        <property name="realm" ref="userRealm" />
    </bean>


    <!-- 自定义 realm -->
    <bean id="userRealm" class="cn.marer.shiro.CustomRealm"></bean>
</beans>


此文老猫原创,转载请加本文连接:http://blog.csdn.net/nthack5730/article/details/51002218
更多有关老猫的文章:http://blog.csdn.net/nthack5730

     记住要在web.xml中加入applicationContext-shiro.xml的读取,否则上面的配置是无效的。
     【没有加入这个配置文件的话会抛出:No Bean name "shiroFilter"...  异常】
Edit
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
    <param-value>classpath:applicationContext-shiro.xml</param-value>
</context-param>

     也可以使用通配进行配置,不过为了系统的稳定性,一般我都不采用这种方法,毕竟在后面使用的都是注解的方式配置Spring的bean,配置文件不多,就慢慢打上去吧。
Edit
<param-value>classpath:applicationContext-*.xml</param-value>




Shiro的内置过滤器:【概述】

anon(匿名)  org.apache.shiro.web.filter.authc.AnonymousFilter
authc(身份验证)       org.apache.shiro.web.filter.authc.FormAuthenticationFilter
authcBasic(http基本验证)    org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter
logout(退出)        org.apache.shiro.web.filter.authc.LogoutFilter
noSessionCreation(不创建session) org.apache.shiro.web.filter.session.NoSessionCreationFilter
perms(许可验证)  org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter
port(端口验证)   org.apache.shiro.web.filter.authz.PortFilter
rest  (rest方面)  org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter
roles(权限验证)  org.apache.shiro.web.filter.authz.RolesAuthorizationFilter
ssl (ssl方面)   org.apache.shiro.web.filter.authz.SslFilter
user (用户方面)  org.apache.shiro.web.filter.authc.UserFilter



 完成:

     好了,至此,基础的初步配置已经完成,自己也在tomcat上面跑了一下,是可以正常运行和访问index的。至于具体的权限认证和授权操作,我会在一步步学习中给大家总结和po出我的文章。希望大家继续支持!

此文老猫原创,转载请加本文连接:http://blog.csdn.net/nthack5730/article/details/51002218
更多有关老猫的文章:http://blog.csdn.net/nthack5730






2 0
原创粉丝点击