您的位置:首页 > 编程语言 > Java开发

使用SpringSide 3.1.4.3开发Web项目的全过程(三)

2011-12-20 17:33 483 查看
第八步、使用Spring Security保护Web资源。在SpringSide 3项目中,已经 整合进了SpringSecurity,实现了符合RBAC规范的权限管理系统,并把数据保存 到了数据库中。我以前的博文SpringSide 3 中的安全框架中对SpringSecurity 有一个初步的探讨,我认为我写的东西对入门来说是很有帮助的,入门以后再深 入就简单了,在评论中我又补充了几点,其中就提到如果要把资源权限配置内容 放到数据库中, 就要从objectDefinitionSource着手。事实上,在最新的
SpringSide 3版本中,就是通过定义一个databaseDefinitionSource来实现从数 据库中读取资源和权限之间的关系,而 databaseDefinitionSource引用 resourceDetailService,而该Service调用 personal.youxia.service.security.ResourceDetailServiceImpl。就是这样一 层套一层的关系,但是最终只需要用户实现 personal.youxia.service.security.ResourceDetailServiceImpl即可。在
SpringSide 3项目中,实现该Service的工作都可以省略,因为江南白衣已经做 好了。而我们要做的,就是在他提供的基础上进行扩展。
在项目中,已经定义好了users、roles、authorities和resource,如果需要 扩展其中任意一项,只需要向对应的数据表添加记录即可。预定义的role有“管 理员”和“用户”两种,我认为在该示例中已经没有增加角色的必要了,而 authorities是肯定要增加的,我想让只有“用户”能够添加文章,只有“管理 员”能够删除文章,所以在authorities表中增加如下两行:

insert into AUTHORITIES (NAME,DISPLAY_NAME) values ('A_ADD_ARTICLE','添加文章');
insert into AUTHORITIES (NAME,DISPLAY_NAME) values ('A_DELETE_ARTICLE','删除文章');

建立authorities表和roles表的联系,用户可以添加文章,管理员当然也能 够添加文章,而只有管理员能够删除文章,所以在数据库中添加如下三行:

insert into ROLES_AUTHORITIES values(1,5);
insert into ROLES_AUTHORITIES values(1,6);
insert into ROLES_AUTHORITIES values(2,5);

再来看看需要保护的资源,它们应该分别为article.action、article! input.action、article!save.action、article!delete.action,其中只有后面 三个需要保护,因此在数据库中添加如下三行:

insert into RESOURCES (RESOURCE_TYPE,VALUE,ORDER_NUM)  values('url','/article!input*',7.0);
insert into RESOURCES (RESOURCE_TYPE,VALUE,ORDER_NUM) values ('url','/article!save*',8.0);
insert into RESOURCES (RESOURCE_TYPE,VALUE,ORDER_NUM) values ('url','/article!delete*',9.0);

最后,需要建立授权和资源之间的联系,如下:

insert into RESOURCES_AUTHORITIES values(5,7);
insert into RESOURCES_AUTHORITIES values(5,8);
insert into RESOURCES_AUTHORITIES values(6,9);


这时,再运行项目,会发现没有登录的用户只能察看文章,而点击增加文章 或删除文章的链接,就会跳到Login界面,或显示你没有访问该页面的权限。

对于项目中自带的用户、角色、授权和资源管理,我是这样的看法:最开始 接触SpringSide 3项目的时候,我觉得该功能是个鸡肋,甚至想过把这些功能删 除掉,弄一个干净的项目从头做;经过一段时间的思考后,我的观念变了,我觉 得这个功能非常有用,是一个很好的基础,而我们自己的功能,都可以从这里进 行扩展;这里的扩展,大部分都只需要在数据库中添加数行记录即可,正如上面 的演示,唯一不能达到要求的,可能是有的人觉得users表字段太少,而实际项 目中我们要记录用户的信息远远不止这么少,其实这个问题也好解决,只需要创
建一个 user_details表即可,或者叫user_profiles,再按照之前的步骤创建针 对user_details表的增删查改功能;总之,尽量不要去更改江南白衣已经实现了 的数据库结构和程序代码。

SpringSecurity针对资源的保护,不仅仅是只能在数据库中配置,其实 SpringSecurity更提供了一些有用的标签,可以在视图文件中灵活使用。具体内 容,大家请参考SpringSecurity文档。

第九步、将项目迁移到多数据库环境。其实只要了解前面的八步,简单的项 目就可以搞定了,但是对于致力于高并发高负载的分布式应用,则离不开多数据 源和分布式事务管理,Web Service和AJAX的跨域访问也是做分布式应用的有力 武器。在我前面的博文中,我已经探讨过了多数据源配置的各种问题:

SpringSide 3 中的多数据源配置的问题

在SpringSide 3 中使用多个数据库的方法

在这里,我选择了第三种方法,就是在Spring中整合Atomikos。下载 Atomikos 3.5.5版,把如下transactions-essentials-alljar文件和 jta.properties文件拷入到项目的WEB-INF/lib目录下。

创建第二个数据库,名称为MultiDatasourceExampleIndex,其中包含一个 Article表,如下:



创建Entity类ArticleIndex.java,创建Dao类ArticleIndexDao.java,创建 Manager类 ArticleIndexManager.java,这几个过程和前面的过程没有什么区别,所以我就不列代码出来了。为了减少编写Action的工作,我将添加 ActionIndex的动作放到了ArticleAction中,即在添加Article的同时添加 ArticleIndex。

修改applicationContext.xml文件,配置双份的dataSource、双份的 sessionFactory,并使用JTATransactionManager,如下:

<?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:jee="http://www.springframework.org/schema/jee"  xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  http://www.springframework.org/schema/jee  http://www.springframework.org/schema/jee/spring-jee-2.5.xsd  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context- 2.5.xsd"
default-lazy-init="true">
<description>Spring公共配置文件 </description>
<!-- 定义受环境影响易变的变量 -->
<bean  class="org.springframework.beans.factory.config.PropertyPlaceholderCon figurer">
<property name="systemPropertiesModeName"  value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<!-- 标准配置 -->
<value>classpath*:/application.properties</value>
<!-- 本地开发环境配置 -->
<value>classpath*:/application.local.properties</value>
<!-- 服务器生产环境配置 -->
<!--  <value>file:/var/myapp/application.server.properties</value&g t; -->
</list>
</property>
</bean>
<!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属 性被注入 -->
<context:component-scan base- package="personal.youxia" />
<bean id="dataSourceContent"  class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init"  destroy-method="close">
<property name="uniqueResourceName">
<value>jdbc/dataSourceContent</value>
</property>
<property name="xaDataSourceClassName">
<value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value >
</property>
<property name="xaProperties">
<props>
<prop key="serverName">localhost</prop>
<prop key="portNumber">3306</prop>
<prop  key="databaseName">MultiDatasourceExample</prop>
<prop key="user">youxia</prop>
<prop key="password">******</prop>
</props>
</property>
<property name="poolSize">
<value>3</value>
</property>
</bean>
<bean id="dataSourceIndex"  class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init"  destroy-method="close">
<property name="uniqueResourceName">
<value>jdbc/dataSourceIndex</value>
</property>
<property name="xaDataSourceClassName">
<value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value >
</property>
<property name="xaProperties">
<props>
<prop key="serverName">localhost</prop>
<prop key="portNumber">3306</prop>
<prop  key="databaseName">MultiDatasourceExample</prop>
<prop key="user">youxia</prop>
<prop key="password">******</prop>
</props>
</property>
<property name="poolSize">
<value>3</value>
</property>
</bean>
<!-- Hibernate配置 -->
<bean id="sessionFactoryContent"  class="org.springframework.orm.hibernate3.annotation.AnnotationSession FactoryBean">
<property name="dataSource" ref="dataSourceContent" />
<property name="namingStrategy">
<bean  class="org.hibernate.cfg.ImprovedNamingStrategy" />
</property>
<property name="hibernateProperties">
<props>
<prop  key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect&l t;/prop>
<prop key="hibernate.show_sql">${hibernate.show_sql} </prop>
<prop key="hibernate.format_sql">${hibernate.format_sql} </prop>
<prop  key="hibernate.cache.provider_class">org.hibernate.cache.EhCachePro vider
</prop>
<prop  key="hibernate.cache.provider_configuration_file_resource_path">${h ibernate.ehcache_config_file}</prop>
</props>
</property>
<property name="packagesToScan"  value="personal.youxia.entity.*" />
</bean>
<bean id="sessionFactoryIndex"  class="org.springframework.orm.hibernate3.annotation.AnnotationSession FactoryBean">
<property name="dataSource" ref="dataSourceIndex" />
<property name="namingStrategy">
<bean  class="org.hibernate.cfg.ImprovedNamingStrategy" />
</property>
<property name="hibernateProperties">
<props>
<prop  key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect&l t;/prop>
<prop key="hibernate.show_sql">${hibernate.show_sql} </prop>
<prop key="hibernate.format_sql">${hibernate.format_sql} </prop>
<prop  key="hibernate.cache.provider_class">org.hibernate.cache.EhCachePro vider
</prop>
<prop  key="hibernate.cache.provider_configuration_file_resource_path">${h ibernate.ehcache_config_file}</prop>
</props>
</property>
<property name="packagesToScan"  value="personal.youxia.entity.*" />
</bean>

<!-- 事务管理器配置,多数据源JTA事务-->
<bean id="atomikosTransactionManager"  class="com.atomikos.icatch.jta.UserTransactionManager" init- method="init" destroy-method="close">
<property  name="forceShutdown"><value>true</value></property&g t;
</bean>

<bean id="atomikosUserTransaction"  class="com.atomikos.icatch.jta.UserTransactionImp">
<property name="transactionTimeout" value="300"/>
</bean>
<bean id="transactionManager"  class="org.springframework.transaction.jta.JtaTransactionManager">< BR>          <property name="transactionManager"  ref="atomikosTransactionManager" />
<property name="userTransaction"  ref="atomikosUserTransaction"/>
</bean>
<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager"  />
</beans>

修改web.xml,配置双份的OpenSessionInViewFilter,如下: 1321

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>MultiDatasourceExample</display-name>
<!-- Spring ApplicationContext配置文件的路径,可使用通配符,多个路 径用,号分隔
此参数用于后面的Spring Context Loader -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/applicationContext*.xml</param- value>
</context-param>
<!-- Character Encoding filter -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter- class>org.springframework.web.filter.CharacterEncodingFilter</fi lter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!-- SpringSide's Hibernate Open Session In View filter-- >
<filter>
<filter-name>hibernateOpenSessionInViewFilterContent</filter -name>
<filter- class>org.springside.modules.orm.hibernate.OpenSessionInViewFilter& lt;/filter-class>
<init-param>
<param-name>excludeSuffixs</param-name>
<param-value>js,css,jpg,gif</param-value>
</init-param>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactoryContent</param-value>
</init-param>
</filter>
<filter>
<filter-name>hibernateOpenSessionInViewFilterIndex</filter- name>
<filter- class>org.springside.modules.orm.hibernate.OpenSessionInViewFilter& lt;/filter-class>
<init-param>
<param-name>excludeSuffixs</param-name>
<param-value>js,css,jpg,gif</param-value>
</init-param>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactoryIndex</param-value>
</init-param>
</filter>
<!-- SpringSecurity filter-->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter- class>org.springframework.web.filter.DelegatingFilterProxy</filt er-class>
</filter>
<!-- Struts2 filter -->
<filter>
<filter-name>struts2Filter</filter-name>
<filter- class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecu teFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>hibernateOpenSessionInViewFilterContent</filter -name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>hibernateOpenSessionInViewFilterIndex</filter- name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--Spring的ApplicationContext 载入 -->
<listener>
<listener- class>org.springframework.web.context.ContextLoaderListener</lis tener-class>
</listener>
<!-- Spring 刷新Introspector防止内存泄露 -->
<listener>
<listener- class>org.springframework.web.util.IntrospectorCleanupListener</ listener-class>
</listener>
<!-- session超时定义,单位为分钟 -->
<session-config>
<session-timeout>20</session-timeout>
</session-config>
<!-- 出错页面定义 -->
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/common/500.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/common/500.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/common/404.jsp</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/common/403.jsp</location>
</error-page>
</web-app>


在每一个Dao类里面使用@Resource注解指定使用哪一个SessionFactory。代 码我就不列出来了,运行项目进行测试,成功。

到此,我们的征途圆满结束。但是SpringSide 3包含的特性远远不止这些, 在showcase中,江南白衣不断在演示一些新的技术,这些技术如果用得恰当,会 有效提高我们项目的开发速度和项目的运行效率,在以后的日子里,我会逐步为 大家揭开这些神秘的面纱。

这里是该示例项目的源代码MultiDatasourceExample.rar,欢迎大家点击下 载,使用Eclipse 3.4导入后,即可以编辑和运行。由于jar文件太占空间,这个 源代码里面是不提供jar文件的,幸好我只给项目增加了mysql- connector.5.0.18.jar、 transaction-essential-all.jar两个和 jta.properties一个,其余的都是标准的,大家可以从别的项目中拷贝过来。在运行项目之前,大家一定要记得手动创建数据库,并修改配置文件里面的数据库 名和密码。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: