spring-security3 入門篇[轉載]
1.下載spring security的最新版本,工程下載的是3.1
2. 新建工程,結構如下:
其中,涉及到的jar包可以在spring-security包中的例子中獲取
3、配置spring-security.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" xmlns:security ="http://www.springframework.org/schema/security"
- xsi:schemaLocation ="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/security
- http://www.springframework.org/schema/security/spring-security.xsd">
- <!-- 保護應用程序的所有URL,只有擁有ROLE_USER才可以訪問 -->
- < security:http auto-config = "true" >
- < security:intercept-url pattern = "/**" access = "ROLE_USER" />
- </ security:http >
- <!--配置認證管理器,只有用戶名為user,密碼為user的用戶,角色為ROLE_USER可訪問指定的資源 -->
- < security:authentication-manager >
- < security:authentication-provider >
- < security:user-service >
- < security:user name = "user" password = "user" authorities ="ROLE_USER" />
- </ security:user-service >
- </ security:authentication-provider >
- </ security:authentication-manager >
- </ beans >
4.配置web.xml

- <? xml version = "1.0" encoding = "UTF-8" ?>
- < web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns ="http://java.sun.com/xml/ns/javaee" xmlns:web ="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id = "WebApp_ID" version = "2.5" >
- < display-name > springSecurity </ display-name >
- <!--******************************** -->
- <!--*******log4j日志信息的配置****** -->
- <!--******************************* -->
- < context-param >
- < param-name > log4jConfigLocation </ param-name >
- < param-value > classpath:log4j.xml </ param-value >
- </ context-param >
- <!--Spring默認刷新Log4j配置文件的間隔,單位為millisecond,可以不設置 -->
- < context-param >
- < param-name > log4jRefreshInterval </ param-name >
- < param-value > 60000 </ param-value >
- </ context-param >
- <!--******************************** -->
- <!--*******spring bean的配置******** -->
- <!--******************************* -->
- < context-param >
- < param-name > contextConfigLocation </ param-name >
- < param-value > classpath:applicationContext.xml </ param-value >
- </ context-param >
- < listener >
- < listener-class > org.springframework.web.util.Log4jConfigListener </listener-class >
- </ listener >
- < listener >
- < listener-class > org.springframework.web.context.ContextLoaderListener </listener-class >
- </ listener >
- < listener >
- < listener-class > org.springframework.web.util.IntrospectorCleanupListener </listener-class >
- </ listener >
- <!--******************************** -->
- <!--*******字符集 過濾器************ -->
- <!--******************************* -->
- < filter >
- < filter-name > CharacterEncodingFilter </ 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 >
- < filter-mapping >
- < filter-name > CharacterEncodingFilter </ filter-name >
- < url-pattern > /* </ url-pattern >
- </ filter-mapping >
- <!--******************************** -->
- <!--*******session的配置************ -->
- <!--******************************* -->
- < session-config >
- < session-timeout > 30 </ session-timeout >
- </ session-config >
- <!-- SpringSecurity必須的begin -->
- < 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 >
- <!-- SpringSecurity必須的end -->
- < welcome-file-list >
- < welcome-file > index.jsp </ welcome-file >
- </ welcome-file-list >
- </ web-app >
5.index.jsp

- < %@ page language = "java" contentType = "text/html; charset=UTF-8"
- pageEncoding = "UTF-8" % >
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- < html >
- < head >
- < meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" >
- < title > 首頁 </ title >
- </ head >
- < body >
- < h1 > 這里是首頁,歡迎你! </ h1 >
- < %
- String[] str = session .getValueNames();
- for(int i = 0 ;i < str.length ;i++){
- out.println("key =="+str[i]);
- out.println("value =="+session.getAttribute(str[i]));
- }
- %>
- </ body >
- </ html >
6部署應用,在首次瀏覽index.jsp時,由于沒登錄,spring security會自動生成登錄頁面,頁面內容如下:
7輸入用戶名和密碼,user,則進入首頁
至此,簡單的權限控制完成,在index頁面中通過session可以看到存入session中的用戶信息。
posted @ 2013-12-02 19:00 張生 閱讀(323) | 評論 (0) | 編輯 收藏