<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    記錄、分享

    spring-security3 入門篇[轉載]

    1.下載spring security的最新版本,工程下載的是3.1

    2. 新建工程,結構如下:



     其中,涉及到的jar包可以在spring-security包中的例子中獲取

    3、配置spring-security.xml

    Xml代碼  收藏代碼
    1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
    2. < beans   xmlns = "http://www.springframework.org/schema/beans"   
    3.     xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"   xmlns:security ="http://www.springframework.org/schema/security"   
    4.     xsi:schemaLocation ="http://www.springframework.org/schema/beans   
    5.             http://www.springframework.org/schema/beans/spring-beans.xsd  
    6.             http://www.springframework.org/schema/security   
    7.             http://www.springframework.org/schema/security/spring-security.xsd">   
    8.   
    9.     <!-- 保護應用程序的所有URL,只有擁有ROLE_USER才可以訪問 -->   
    10.     < security:http   auto-config = "true" >   
    11.         < security:intercept-url   pattern = "/**"   access = "ROLE_USER"   />   
    12.     </ security:http >   
    13.       
    14.     <!--配置認證管理器,只有用戶名為user,密碼為user的用戶,角色為ROLE_USER可訪問指定的資源 -->  
    15.     < security:authentication-manager >   
    16.         < security:authentication-provider >   
    17.             < security:user-service >   
    18.                 < security:user   name = "user"    password = "user"   authorities ="ROLE_USER" />   
    19.             </ security:user-service >   
    20.         </ security:authentication-provider >   
    21.     </ security:authentication-manager >   
    22. </ beans >   

     4.配置web.xml

    Xml代碼  收藏代碼
    1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
    2. < 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" >   
    3.   < display-name > springSecurity </ display-name >   
    4.     <!--******************************** -->   
    5.     <!--*******log4j日志信息的配置****** -->   
    6.     <!--******************************* -->   
    7.     < context-param >   
    8.         < param-name > log4jConfigLocation </ param-name >   
    9.         < param-value > classpath:log4j.xml </ param-value >   
    10.     </ context-param >   
    11.     <!--Spring默認刷新Log4j配置文件的間隔,單位為millisecond,可以不設置 -->   
    12.     < context-param >   
    13.         < param-name > log4jRefreshInterval </ param-name >   
    14.         < param-value > 60000 </ param-value >   
    15.     </ context-param >   
    16.   
    17.     <!--******************************** -->   
    18.     <!--*******spring bean的配置******** -->   
    19.     <!--******************************* -->   
    20.     < context-param >   
    21.         < param-name > contextConfigLocation </ param-name >   
    22.         < param-value > classpath:applicationContext.xml </ param-value >   
    23.     </ context-param >   
    24.       
    25.     < listener >   
    26.         < listener-class > org.springframework.web.util.Log4jConfigListener </listener-class >   
    27.     </ listener >   
    28.     < listener >   
    29.         < listener-class > org.springframework.web.context.ContextLoaderListener </listener-class >   
    30.     </ listener >   
    31.     < listener >   
    32.         < listener-class > org.springframework.web.util.IntrospectorCleanupListener </listener-class >   
    33.     </ listener >   
    34.     <!--******************************** -->   
    35.     <!--*******字符集 過濾器************ -->   
    36.     <!--******************************* -->   
    37.     < filter >   
    38.         < filter-name > CharacterEncodingFilter </ filter-name >   
    39.         < filter-class > org.springframework.web.filter.CharacterEncodingFilter </filter-class >   
    40.         < init-param >   
    41.             < param-name > encoding </ param-name >   
    42.             < param-value > UTF-8 </ param-value >   
    43.         </ init-param >   
    44.         < init-param >   
    45.             < param-name > forceEncoding </ param-name >   
    46.             < param-value > true </ param-value >   
    47.         </ init-param >   
    48.     </ filter >   
    49.     < filter-mapping >   
    50.         < filter-name > CharacterEncodingFilter </ filter-name >   
    51.         < url-pattern > /* </ url-pattern >   
    52.     </ filter-mapping >   
    53.   
    54.     <!--******************************** -->   
    55.     <!--*******session的配置************ -->   
    56.     <!--******************************* -->   
    57.     < session-config >   
    58.         < session-timeout > 30 </ session-timeout >   
    59.     </ session-config >   
    60.       
    61.     <!-- SpringSecurity必須的begin -->   
    62.     < filter >   
    63.         < filter-name > springSecurityFilterChain </ filter-name >   
    64.         < filter-class > org.springframework.web.filter.DelegatingFilterProxy </filter-class >   
    65.     </ filter >   
    66.     <!-- 攔截所有的請求 -->   
    67.     < filter-mapping >   
    68.         < filter-name > springSecurityFilterChain </ filter-name >   
    69.         < url-pattern > /* </ url-pattern >   
    70.     </ filter-mapping >   
    71.     <!-- SpringSecurity必須的end -->   
    72.       
    73.   < welcome-file-list >   
    74.     < welcome-file > index.jsp </ welcome-file >   
    75.   </ welcome-file-list >   
    76. </ web-app >   

     

    5.index.jsp

    Html代碼  收藏代碼
    1. < %@ page  language = "java"   contentType = "text/html; charset=UTF-8"   
    2.     pageEncoding = "UTF-8" % >   
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">   
    4. < html >   
    5. < head >   
    6. < meta   http-equiv = "Content-Type"   content = "text/html; charset=UTF-8" >   
    7. < title > 首頁 </ title >   
    8. </ head >   
    9. < body >   
    10.     < h1 > 這里是首頁,歡迎你! </ h1 >   
    11.     < %   
    12.         String[] str  =  session .getValueNames();  
    13.         for(int i = 0 ;i < str.length ;i++){  
    14.             out.println("key =="+str[i]);  
    15.             out.println("value =="+session.getAttribute(str[i]));  
    16.         }  
    17.     %>   
    18. </ body >   
    19. </ html >   

     

    6部署應用,在首次瀏覽index.jsp時,由于沒登錄,spring security會自動生成登錄頁面,頁面內容如下:

     



     7輸入用戶名和密碼,user,則進入首頁

     

     

     至此,簡單的權限控制完成,在index頁面中通過session可以看到存入session中的用戶信息。

    posted on 2013-12-02 19:00 張生 閱讀(321) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    <2013年12月>
    24252627282930
    1234567
    891011121314
    15161718192021
    22232425262728
    2930311234

    導航

    統計

    常用鏈接

    留言簿

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲一线产品二线产品| 久草免费福利视频| 亚洲午夜国产精品无码老牛影视| 国内永久免费crm系统z在线 | 亚洲视频在线免费| 免费成人在线视频观看| 一本色道久久88亚洲精品综合| 亚洲一区二区三区免费| 免费观看黄色的网站| 无码日韩人妻AV一区免费l | 国产高潮久久免费观看| 亚洲熟妇无码爱v在线观看| 高清在线亚洲精品国产二区| 性无码免费一区二区三区在线 | 亚洲aⅴ无码专区在线观看| 日本亚洲视频在线| 在线观看91精品国产不卡免费| 免费在线看黄网站| 美景之屋4在线未删减免费| 亚洲欧洲日产韩国在线| 国产偷窥女洗浴在线观看亚洲| 三年片在线观看免费观看高清电影| 一区二区3区免费视频| 亚洲熟女综合色一区二区三区 | 国产亚洲精品岁国产微拍精品| 女人18毛片a级毛片免费视频| 午夜无码A级毛片免费视频 | 日日AV拍夜夜添久久免费| 无码少妇精品一区二区免费动态| 国产AV无码专区亚洲AV琪琪| 亚洲fuli在线观看| 亚洲日韩区在线电影| 国产成人A亚洲精V品无码| 国产免费69成人精品视频| 国产成人免费网站| 中文字幕免费观看| 成人性生交大片免费看好| 日韩在线视频播放免费视频完整版 | 香港a毛片免费观看 | 久久亚洲精品无码播放| 国产美女做a免费视频软件|