spring-security3 入門(mén)篇[轉(zhuǎn)載]
1.下載spring security的最新版本,工程下載的是3.1
2. 新建工程,結(jié)構(gòu)如下:
其中,涉及到的jar包可以在spring-security包中的例子中獲取
3、配置spring-security.xml
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">
- <!-- 保護(hù)應(yīng)用程序的所有URL,只有擁有ROLE_USER才可以訪問(wèn) -->
- < security:http auto-config = "true" >
- < security:intercept-url pattern = "/**" access = "ROLE_USER" />
- </ security:http >
- <!--配置認(rèn)證管理器,只有用戶(hù)名為user,密碼為user的用戶(hù),角色為ROLE_USER可訪問(wèn)指定的資源 -->
- < 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代碼 

- <? 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默認(rèn)刷新Log4j配置文件的間隔,單位為millisecond,可以不設(shè)置 -->
- < 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 >
- <!--******************************** -->
- <!--*******字符集 過(guò)濾器************ -->
- <!--******************************* -->
- < 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 >
- <!-- 攔截所有的請(qǐng)求 -->
- < 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
Html代碼 

- < %@ 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 > 首頁(yè) </ title >
- </ head >
- < body >
- < h1 > 這里是首頁(yè),歡迎你! </ 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部署應(yīng)用,在首次瀏覽index.jsp時(shí),由于沒(méi)登錄,spring security會(huì)自動(dòng)生成登錄頁(yè)面,頁(yè)面內(nèi)容如下:
7輸入用戶(hù)名和密碼,user,則進(jìn)入首頁(yè)
至此,簡(jiǎn)單的權(quán)限控制完成,在index頁(yè)面中通過(guò)session可以看到存入session中的用戶(hù)信息。
posted on 2013-12-02 19:00 張生 閱讀(325) 評(píng)論(0) 編輯 收藏