ITeye博客

spring-security3 入门篇

snoopy7713 · 2012-11-28 13:04 · spring--security · 行业应用 · Spring · Security · Markdown

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. <!-- 保护应用程序的所有URL,只有拥有ROLE_USER才可以访问 -->
  9. < security:http   auto-config = "true" >
  10. < security:intercept-url   pattern = "/**"   access = "ROLE_USER"   />
  11. </ security:http >
  12. <!--配置认证管理器,只有用户名为user,密码为user的用户,角色为ROLE_USER可访问指定的资源 -->
  13. < security:authentication-manager >
  14. < security:authentication-provider >
  15. < security:user-service >
  16. < security:user   name = "user"    password = "user"   authorities = "ROLE_USER" />
  17. </ security:user-service >
  18. </ security:authentication-provider >
  19. </ security:authentication-manager >
  20. </ 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. <!--*******spring bean的配置******** -->
  18. <!--******************************* -->
  19. < context-param >
  20. < param-name > contextConfigLocation </ param-name >
  21. < param-value > classpath:applicationContext.xml </ param-value >
  22. </ context-param >
  23. < listener >
  24. < listener-class > org.springframework.web.util.Log4jConfigListener </ listener-class >
  25. </ listener >
  26. < listener >
  27. < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >
  28. </ listener >
  29. < listener >
  30. < listener-class > org.springframework.web.util.IntrospectorCleanupListener </ listener-class >
  31. </ listener >
  32. <!--******************************** -->
  33. <!--*******字符集 过滤器************ -->
  34. <!--******************************* -->
  35. < filter >
  36. < filter-name > CharacterEncodingFilter </ filter-name >
  37. < filter-class > org.springframework.web.filter.CharacterEncodingFilter </ filter-class >
  38. < init-param >
  39. < param-name > encoding </ param-name >
  40. < param-value > UTF-8 </ param-value >
  41. </ init-param >
  42. < init-param >
  43. < param-name > forceEncoding </ param-name >
  44. < param-value > true </ param-value >
  45. </ init-param >
  46. </ filter >
  47. < filter-mapping >
  48. < filter-name > CharacterEncodingFilter </ filter-name >
  49. < url-pattern > /* </ url-pattern >
  50. </ filter-mapping >
  51. <!--******************************** -->
  52. <!--*******session的配置************ -->
  53. <!--******************************* -->
  54. < session-config >
  55. < session-timeout > 30 </ session-timeout >
  56. </ session-config >
  57. <!-- SpringSecurity必须的begin -->
  58. < filter >
  59. < filter-name > springSecurityFilterChain </ filter-name >
  60. < filter-class > org.springframework.web.filter.DelegatingFilterProxy </ filter-class >
  61. </ filter >
  62. <!-- 拦截所有的请求 -->
  63. < filter-mapping >
  64. < filter-name > springSecurityFilterChain </ filter-name >
  65. < url-pattern > /* </ url-pattern >
  66. </ filter-mapping >
  67. <!-- SpringSecurity必须的end -->
  68. < welcome-file-list >
  69. < welcome-file > index.jsp </ welcome-file >
  70. </ welcome-file-list >
  71. </ 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中的用户信息。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。