spring security学习(一)

web.xml配置:


<web-app id="WebApp_ID" version="2.4"  
    xmlns="http://java.sun.com/xml/ns/j2ee"   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
      
  <filter>  
        <filter-name>springSecurityFilterChainfilter-name>  
        <filter-class>org.springframework.web.filter.DelegatingFilterProxyfilter-class>  
    filter>  

    <filter-mapping>  
        <filter-name>springSecurityFilterChainfilter-name>  
        <url-pattern>/*url-pattern>  
    filter-mapping>  

    <context-param>  
        <param-name>contextConfigLocationparam-name>  
        <param-value>  
        classpath:spring-security.xml  
        param-value>  
    context-param>  

    <servlet>  
        <servlet-name>springservlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>  
        <load-on-startup>1load-on-startup>  
    servlet>  

    <servlet-mapping>  
        <servlet-name>springservlet-name>  
        <url-pattern>/**url-pattern>  
    servlet-mapping>  

    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>  
    listener> 
web-app>
spring-security.xml配置:
  
<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-3.0.xsd
           http://www.springframework.org/schema/security
           http://www.springframework.org/schema/security/spring-security.xsd">  
           
  <http>
  <intercept-url pattern="/**" access="ROLE_USER" />
  <form-login />
  <logout />
http> 


         <authentication-manager>
  <authentication-provider>
    <user-service>
      <user name="zw" password="123" authorities="ROLE_USER, ROLE_ADMIN" />
      <user name="sam" password="123" authorities="ROLE_USER" />
    user-service>
  authentication-provider>
authentication-manager> 
beans:beans>  

另外在WEB-INF底下,提供一个spring-servlet.xml的配置文件

所需jar包:见截图
spring security学习(一)_第1张图片
访问index.jsp,被spring拦截,跳转到登录界面(spring提供的)见截图
spring security学习(一)_第2张图片
输入正确(xml文件里面配置的)的用户名密码,跳转成功
这里写图片描述
输入错误,则显示提示
spring security学习(一)_第3张图片
第二章将写使用自定义的登录界面进行学习测试,欢迎留言讨论!

你可能感兴趣的:(spring)