京山游俠

          專注技術,拒絕扯淡
          posts - 50, comments - 868, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理
          使用SpringSide 3.1.4.3開發Web項目的全過程(上)
          使用SpringSide 3.1.4.3開發Web項目的全過程(中)

          第八步、使用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.PropertyPlaceholderConfigurer">
                  
          <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> -->
                      
          </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.AnnotationSessionFactoryBean">
                  
          <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</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.EhCacheProvider
                          
          </prop>
                          
          <prop key="hibernate.cache.provider_configuration_file_resource_path">${hibernate.ehcache_config_file}</prop>
                      
          </props>
                  
          </property>
                  
          <property name="packagesToScan" value="personal.youxia.entity.*" />
              
          </bean>
              
          <bean id="sessionFactoryIndex" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
                  
          <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</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.EhCacheProvider
                          
          </prop>
                          
          <prop key="hibernate.cache.provider_configuration_file_resource_path">${hibernate.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>   
              
          </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">
                  
          <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</filter-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</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</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</filter-class>
              
          </filter>

              
          <!-- Struts2 filter -->
              
          <filter>
                  
          <filter-name>struts2Filter</filter-name>
                  
          <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</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</listener-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一個,其余的都是標準的,大家可以從別的項目中拷貝過來。在運行項目之前,大家一定要記得手動創建數據庫,并修改配置文件里面的數據庫名和密碼。

          評論

          # re: 使用SpringSide 3.1.4.3開發Web項目的全過程(下)  回復  更多評論   

          2009-07-23 10:30 by 虎嘯龍吟
          很好,很強大。
          有不少亮點,如JTA多數據源的事務管理。

          # re: 使用SpringSide 3.1.4.3開發Web項目的全過程(下)  回復  更多評論   

          2009-07-24 16:48 by 施華洛世奇
          很好

          # re: 使用SpringSide 3.1.4.3開發Web項目的全過程(下)  回復  更多評論   

          2009-07-26 09:45 by 虎嘯龍吟
          博主:多數據源web運行正常。但DataAccessTest junit測試異常。錯誤代碼如下:
          2009-07-26 09:42:45,734 [main] ERROR [org.springframework.test.context.TestContextManager] - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@1583882] to prepare test instance [personal.youxia.unit.DataAccessTest@e60a94]
          org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personal.youxia.unit.DataAccessTest': Autowiring of methods failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests.setDataSource(javax.sql.DataSource); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: [dataSourceContent, dataSourceIndex]

          # re: 使用SpringSide 3.1.4.3開發Web項目的全過程(下)  回復  更多評論   

          2009-07-26 20:58 by 海邊沫沫
          @虎嘯龍吟
          在多數據環境下單元測試是不能運行成功,主要原因是我的單元測試是在單數據源環境下寫的。經過最后一步改到多數據源環境后,直接就項目運行成功了,所以沒有修改單元測試的代碼。

          如果要想單元測試運行成功,就必須在DataAccessTest類中重寫setDataSource方法,并使用@Resource注解指定一個在applicationContext.xml中配置好的dataSource。指定哪一個數據源并不重要,因為我們沒有使用到這個注入的數據源。之所以要這么配置,主要是為了解決Spring的自動匹配問題。

          在我的博文《SpringSide 3 中的多數據源配置的問題》中有具體的探討和代碼。

          # re: 使用SpringSide 3.1.4.3開發Web項目的全過程(下)  回復  更多評論   

          2009-07-29 09:49 by Cloud kensin
          拜讀京山之文章,很是受用,謝謝拿出來分享

          # re: 使用SpringSide 3.1.4.3開發Web項目的全過程(下)[未登錄]  回復  更多評論   

          2009-07-31 18:34 by Sam
          前輩,如果想保護article.action是不是在數據庫中添加insert into RESOURCES (RESOURCE_TYPE,VALUE,ORDER_NUM) values('url','/article!input*',7.0);就好?

          我看到ss3例子中的數據庫設置,有一條數據是insert into RESOURCES (RESOURCE_TYPE,VALUE,ORDER_NUM) values('url','/role*',xx);這樣的value應該是覆蓋了role!input和role!save這種資源,為什么在運行時用非普通用戶登錄,資源還是能被保護呢?
          謝謝!

          # re: 使用SpringSide 3.1.4.3開發Web項目的全過程(下)  回復  更多評論   

          2009-07-31 21:35 by 海邊沫沫
          @Sam
          不是用戶直接對應到資源的,而是用戶-角色-授權-資源。你查看SS3的例子,會發現這一句:
          insert into RESOURCES_AUTHORITIES values(3,6);
          說明只有具有授權3的角色才能夠查看資源6,也就是只有具備“查看角色"
          授權的人才能夠訪問/role*
          另外,在SpringSecurity中,資源配置的順序也是很重要的,因為/role!input和/role!save排在/role*前面,所以前面的會先匹配,只有不能匹配/role!input和/role!save的才會匹配/role*,這也是RESOURCE表中需要ORDER_NUM字段的原因。

          # re: 使用SpringSide 3.1.4.3開發Web項目的全過程(下)  回復  更多評論   

          2011-02-18 14:10 by 哎 春天
          創建Entity類ArticleIndex.java,創建Dao類ArticleIndexDao.java,創建Manager類ArticleIndexManager.java

          請問是手動創建還是基于模版生成?

          # re: 使用SpringSide 3.1.4.3開發Web項目的全過程(下)  回復  更多評論   

          2012-06-07 16:34 by 雪刀浪子
          不錯,多謝分享。

          # re: 使用SpringSide 3.1.4.3開發Web項目的全過程(下)[未登錄]  回復  更多評論   

          2012-12-06 10:17 by 初學者
          請問在Struts1中Action怎么取得修改頁面FormBean的值?
          是用doGetEntity(form, request)嗎? 好像取到的是原來沒用修改的!

          # re: 使用SpringSide 3.1.4.3開發Web項目的全過程(下)  回復  更多評論   

          2013-01-29 14:35 by 白天太白
          請問一下,springside3的官方文檔哪里有呢?
          主站蜘蛛池模板: 儋州市| 陇西县| 霞浦县| 青冈县| 云林县| 保山市| 博罗县| 汕尾市| 海门市| 玛曲县| 宁强县| 嘉祥县| 延寿县| 武安市| 米易县| 东至县| 平湖市| 重庆市| 丹阳市| 武功县| 武邑县| 竹山县| 开江县| 南投市| 丹阳市| 平阳县| 华安县| 新昌县| 普陀区| 巴中市| 海丰县| 华容县| 沐川县| 桂平市| 汶上县| 饶阳县| 连南| 陆川县| 克东县| 商洛市| 灵宝市|