1. 程式人生 > >Spring安全許可權管理(Spring Security的配置使用)

Spring安全許可權管理(Spring Security的配置使用)

1.Spring Security簡要介紹

Spring Security以前叫做acegi,是後來才成為Spring的一個子專案,也是目前最為流行的一個安全許可權管理框架,它與Spring緊密結合在一起。

Spring Security關注的重點是在企業應用安全層為您提供服務,你將發現業務問題領域存在著各式各樣的需求。銀行系統跟電子商務應用就有很大的不同。電子商務系統與企業銷售自動化工具又有很大不同。這些客戶化需求讓應用安全顯得有趣,富有挑戰性而且物有所值。Spring Security為基於J2EE的企業應用軟體提供了一套全面的安全解決方案。

2.為Spring Security配置過濾器和其他引數

要使用Spring Security,首先就是在web.xml中為它配置過濾器, 其次因為我的spring配置檔案是放在WEB-INF下的,因此還要配置上下文的引數,最後新增spring的監聽器:

view plaincopy to clipboardprint?

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="

http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <!-- 配置上下文引數,指定spring配置檔案的位置 -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/spring-*.xml</param-value>
 </context-param>
 <!-- spring security必須的過濾器,保證在訪問所有的頁面時都必須通過認證 -->
 <filter>
  <filter-name>springSecurityFilterChain</filter-name>
  <filter-class>
   org.springframework.web.filter.DelegatingFilterProxy
  </filter-class>
 </filter>
 <filter-mapping>
  <filter-name>springSecurityFilterChain</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <listener>
  <listener-class>
   org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 <login-config>
  <auth-method>BASIC</auth-method>
 </login-config>
</web-app>
 

3.配置security(spring-security.xml)

view plaincopy to clipboardprint?
<?xml version="1.0" encoding="UTF-8"?> 
<!-- 這裡必須使用security的名稱空間,提供了beans這個假名 --> 
<beans:beans xmlns="http://www.springframework.org/schema/security
    xmlns:beans="http://www.springframework.org/schema/beans
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
                        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd"> 
 
    <!-- Spring Security採用就近原則,有多個約束時,從上至下只要找到第一條滿足就返回,因此因該將最嚴格的約束放在最前面,而將最寬鬆的約束放在最後面.auto-config屬性可以讓spring security為我們自動配置幾種常用的許可權控制機制,包括form,anonymous, rememberMe等。當然你也可以手工配置。--> 
    <http auto-config="true"> 
        <!-- 我們利用intercept-url來判斷使用者需要具有何種許可權才能訪問對應的url資源,可以在pattern中指定一個特定的url資源,也可以使用萬用字元指定一組類似的url資源。例子中定義的兩個intercepter-url,第一個用來控制對/security/**的訪問,第二個使用了萬用字元/**,說明它將控制對系統中所有url資源的訪問。 --> 
        <intercept-url pattern="/security/**" access="ROLE_ADMIN" /> 
        <intercept-url pattern="/**" access="ROLE_ADMIN,ROLE_USER" /> 
        <intercept-url pattern="/login.jsp*" filters="none" /> 
        <logout logout-url="/logout.jsp" 
            logout-success-url="/j_spring_security_check" /> 
    </http> 
 
    <!-- 使用記憶體許可權管理的配置資訊, 在tomcat啟動時,會載入這個檔案並一直儲存在記憶體中,知道應用程式重啟,所以也叫記憶體許可權管理  
        <authentication-provider> 
        <user-service> 
        <user name="admin" password="tomcat" authorities="ROLE_ADMIN"/> 
        <user name="liky" password="redhat" authorities="ROLE_USER"/>       
        </user-service> 
        </authentication-provider> 
    --> 
    <!-- 使用資料庫作為許可權管理的來源,data-source-ref指定了資料來源,所指定的資料來源必須包含users, authorities表,並符合security的定義規範 --> 
    <authentication-provider> 
        <jdbc-user-service data-source-ref="dataSource" /> 
    </authentication-provider> 
 
</beans:beans> 
<?xml version="1.0" encoding="UTF-8"?>
<!-- 這裡必須使用security的名稱空間,提供了beans這個假名 -->
<beans:beans xmlns="http://www.springframework.org/schema/security"
 xmlns:beans="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
      http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd">

 <!-- Spring Security採用就近原則,有多個約束時,從上至下只要找到第一條滿足就返回,因此因該將最嚴格的約束放在最前面,而將最寬鬆的約束放在最後面.auto-config屬性可以讓spring security為我們自動配置幾種常用的許可權控制機制,包括form,anonymous, rememberMe等。當然你也可以手工配置。-->
 <http auto-config="true">
  <!-- 我們利用intercept-url來判斷使用者需要具有何種許可權才能訪問對應的url資源,可以在pattern中指定一個特定的url資源,也可以使用萬用字元指定一組類似的url資源。例子中定義的兩個intercepter-url,第一個用來控制對/security/**的訪問,第二個使用了萬用字元/**,說明它將控制對系統中所有url資源的訪問。 -->
  <intercept-url pattern="/security/**" access="ROLE_ADMIN" />
  <intercept-url pattern="/**" access="ROLE_ADMIN,ROLE_USER" />
  <intercept-url pattern="/login.jsp*" filters="none" />
  <logout logout-url="/logout.jsp"
   logout-success-url="/j_spring_security_check" />
 </http>

 <!-- 使用記憶體許可權管理的配置資訊, 在tomcat啟動時,會載入這個檔案並一直儲存在記憶體中,知道應用程式重啟,所以也叫記憶體許可權管理
  <authentication-provider>
  <user-service>
  <user name="admin" password="tomcat" authorities="ROLE_ADMIN"/>
  <user name="liky" password="redhat" authorities="ROLE_USER"/>  
  </user-service>
  </authentication-provider>
 -->
 <!-- 使用資料庫作為許可權管理的來源,data-source-ref指定了資料來源,所指定的資料來源必須包含users, authorities表,並符合security的定義規範 -->
 <authentication-provider>
  <jdbc-user-service data-source-ref="dataSource" />
 </authentication-provider>

</beans:beans>

4.資料來源的配置(spring-common.xml)

 <!-- 定義資料來源 -->
 <bean id="dataSource"
  class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName"
   value="com.mysql.jdbc.Driver">
  </property>
  <property name="url" value="jdbc:mysql://localhost:3306/csu"></property>
  <property name="username" value="root"></property>
  <property name="password" value="redhat"></property>
  <property name="maxActive" value="100"></property>
  <property name="maxIdle" value="30"></property>
  <property name="maxWait" value="300"></property>
  <property name="defaultAutoCommit" value="true"></property>
 </bean> 
</beans>

5.專案的目錄結構


6. 資料庫指令碼

view plaincopy to clipboardprint?
/-- 注意這裡的指令碼是MYSQL的,因此在你演示這個例項的時候,要加入MySQL的驅動包 --/  
   
create table users  
(  
username varchar(50) primary key,  
password varchar(50),  
enabled tinyint(1)  
);  
 
create table authorities  
(  
id int auto_increment primary key,  
username varchar(50),  
authority varchar(50),  
constraint fk_authorities_users foreign key(username) references users(username)  
);  
 
create unique index ix_auth_username on authorities (username,authority); 
/-- 注意這裡的指令碼是MYSQL的,因此在你演示這個例項的時候,要加入MySQL的驅動包 --/
 
create table users
(
username varchar(50) primary key,
password varchar(50),
enabled tinyint(1)
);

create table authorities
(
id int auto_increment primary key,
username varchar(50),
authority varchar(50),
constraint fk_authorities_users foreign key(username) references users(username)
);

create unique index ix_auth_username on authorities (username,authority);


7.部署和配置的要點說明

這是一個Spring Security的資料庫認證例項,要注意以下幾點:
(1)請自行加入Spring必須的包,Spring security的包和MySQL的驅動包,當然你也可以換成其他的資料庫,但是你要相應的修改spring-common.xml中的dataSource部分
(2)資料庫中的兩個表users,authorites必須完全按照指令碼所示來定義,也就是說表的名字不能修改.
(3)users表必須包含username,password,enabled欄位,這三個欄位是絕對不能少的,也不能修改型別.另外enabled一定要為1才能登入
(4)authorities表必須包含username欄位,這個欄位引用users的username作為外來鍵,authority欄位就是角色的名字,角色名字必須滿足ROLE_XXX的格式(例如:ROLE_ADMIN,ROLE_USER,ROLE_MAMAGER)
(5)如果一個使用者有多個角色,不要將多個角色放在一起用逗號隔開.而是每個角色定義一條記錄(例如:abu有ROLE_ADMIN,ROLE_USER兩個角色,那麼應該定義兩條記錄: 一條為abu, ROLE_USER,另一條為abu, ROLE_ADMIN.而不是隻有一條:abu, ROLE_ADMIN,ROLE_USER)
(6)你可以給authorities表新增一個id欄位作為主鍵.