spring-security.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 下面这个事前缀 若是用默认的beans即可不用前缀 因为后面要配置的security的标签很多 为了方便就直接给beans前统一加上前缀-->
<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.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
<!-- 设置页面不登陆也可以访问到 -->
<http pattern="/login.html" security="none"></http>
<http pattern="/login_error.html" security="none"></http>
<!-- 页面的拦截规则 use-expression:是否启动SPEL表达式默认是true-->
<http use-expressions="false">
<!-- 这里的/*只包括这个跟目录本机的资源 /**才是包括子目录的资源 与web.xml不一样 -->
<!-- access当前用户必须有ROLE_USER的角色 才可以访问根目录及所属子目录的资源 -->
<intercept-url pattern="/**" access="ROLE_USER"/>
<!-- 开启表单登录功能 form-login能很智能的自动生成一个表单登录界面-->
<form-login login-page="/login.html" default-target-url="/index.html" authentication-failure-url="/login_error.html"/>
<csrf disabled="true"/>
</http>
<!-- 若是启动SPEL表达式要这样写-->
<!-- <http >
<intercept-url pattern="/**" access="hasRole('ROLE_USER')"/>
</http> -->
<!-- 认证管理器 -->
<authentication-manager>
<authentication-provider>
<user-service>
<user name="admin" password="123456" authorities="ROLE_USER"/>
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
web.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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-security.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<!--
过滤器代理类 由spring web 包提供 作用是将这个请求经过这个过滤器之后,他会自动寻找一个
名为springSecurityFilterChain的类,注意这个名称不要改,他就是这个安全过滤框架的入口
-->
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern> <!-- /*过滤掉根目录下的所有资源 -->
</filter-mapping>
</web-app>