1.spring Security簡要介紹html
Spring Security之前叫作acegi,是後來才成爲Spring的一個子項目,也是目前最爲流行的一個安全權限管理框架,它與Spring緊密結合在一塊兒。java
Spring Security關注的重點是在企業應用安全層爲您提供服務,你將發現業務問題領域存在着各式各樣的需求。銀行系統跟電子商務應用就有很大的不一樣。電子商務系統與企業銷售自動化工具又有很大不一樣。這些客戶化需求讓應用安全顯得有趣,富有挑戰性並且物有所值。Spring Security爲基於J2EE的企業應用軟件提供了一套全面的安全解決方案。mysql
2.爲Spring Security配置過濾器和其餘參數web
要使用Spring Security,首先就是在web.xml中爲它配置過濾器, 其次由於個人spring配置文件是放在WEB-INF下的,所以還要配置上下文的參數,最後添加spring的監聽器:spring
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5" xmlns="http://<a href="http://lib.csdn.net/base/17" class='replace_word' title="Java EE知識庫" target='_blank' style='color:#df3434; font-weight:bold;'>Java</a>.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">
-
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/spring-*.xml</param-value>
- </context-param>
-
- <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)sql
- <?xml version="1.0" encoding="UTF-8"?>
- <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">
-
-
- <http auto-config="true">
-
- <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>
- -->
-
- <authentication-provider>
- <jdbc-user-service data-source-ref="dataSource" />
- </authentication-provider>
-
- </beans:beans>
4.數據源的配置(spring-common.xml)數據庫
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="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.5.xsd">
-
- <!-- 定義數據源 -->
- <bean id="dataSource"
- class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName"
- value="com.<a href="http:
- </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.項目的目錄結構apache
![](http://static.javashuo.com/static/loading.gif)
6. 數據庫腳本tomcat
- /-- 注意這裏的腳本是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字段做爲主鍵.