现在的位置: 首页 > 综合 > 正文

spring security2学习笔记一(最小配置)

2013年10月14日 ⁄ 综合 ⁄ 共 2351字 ⁄ 字号 评论关闭

一、新建web project:

      所需jar包:

     

二、配置过滤器:

     <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>

 

三、applicationContext.xml基础配置:

      <?xml version="1.0" encoding="UTF-8"?>

      <!——①命名空间配置——>
      <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-2.0.xsd
      http://www.springframework.org/schema/security
      http://www.springframework.org/schema/security/spring-security-2.0.4.xsd">

      <!——②http部分配置如何拦截用户请求。auto-config='true'将自动配置几种常用的权限控制机制——>

    <http auto-config='true'>

      <!——③intercept-url来判断用户需要具有何种权限才能访问对应的url资源——>
        <intercept-url pattern="/admin.jsp" access="ROLE_ADMIN" />
        <intercept-url pattern="/**" access="ROLE_USER" />
    </http>

      <!——④authorities,定义了这个用户登陆之后将会拥有的权限,它与上面intercept-url中定义的权限内容一一对——>

    <authentication-provider>
        <user-service>
            <user name="admin" password="admin" authorities="ROLE_USER, ROLE_ADMIN" />
            <user name="user" password="user" authorities="ROLE_USER" />
        </user-service>
    </authentication-provider>

</beans:beans>

 

四、web.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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>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>
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext.xml</param-value>
 </context-param>
 <listener>
  <listener-class>
   org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>

</web-app>

 

抱歉!评论已关闭.