Demo
1.创建一个web工程导入maven依赖
2.配置web.xml文件,注意拦截器的filter-name必须为springSecurityFilterChain,否则会报错
<?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">
<display-name>youlexuan-springsecurity</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>youlexuanweb</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:springmvc/Springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>youlexuanweb</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 拦截器 -->
<filter>
<!-- name的名字必须固定 -->
<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>
</web-app>
3.添加配置文件applicationContext-security.xml
注意要给静态资源放行,配置springmvc给静态资源放行,applicationContext-security.xml给下面的文件放行
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:mvc="http://www.springframework.org/schema/mvc" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
<!-- 页面拦截的规则 登录,注册,错误 -->
<!-- security="none" 表示不拦截 -->
<!-- 放行的页面 -->
<http pattern="/login.html" security="none"></http>
<http pattern="/error.html" security="none"></http>
<http pattern="/favicon.ico" security="none"></http>
<!-- 页面拦截规则 -->
<http use-expressions="false">
<!-- /* 文件夹下的文件不包括子文件夹中的 /** 文件夹下的所有资源包括子文件夹 -->
<intercept-url pattern="/**" access="ROLE_USER" />
<!-- form-login 默认请求的url login 注销页是logout.html -->
<form-login login-page="/login.html" default-target-url="/success.html" authentication-failure-url="/error.html" />
<logout logout-success-url="/login.html" />
<!-- 关闭csrf -->
<csrf disabled="true" />
</http>
<!-- 认证管理 -->
<authentication-manager>
<authentication-provider>
<user-service>
<user name="rose" password="123456" authorities="ROLE_USER" />
<user name="jack" password="jack" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
在项目中使用SpringSecurity框架
- 导入配置文件applicationContext-security.xml,注意虽然mvc已经放行了静态页面,但是在拦截这里还是要再放行静态界面,与mvc无关,否则静态文件无法加载.
- 需要注意的是,登录需要form表单才能提交信息,我们需要给input标签name属性以便他提交属性,同时如果提交的是一个a连接,我们需要把它做成一个点击事件,模拟form表单.
- 此时我们点击登录还会报下图的错,所以我们在配置spring配置文件中在放行assets/img/favicon.co即可
- 此时我们发现页面内容不见了,因为SpringSecurity默认不适用框架,而我们页面使用了angularjs框架,所以还要添加标签放行框架内容
- 注销a连接直接写logout,此时的流程是进入拦截器,正好我们拦截器匹配到了logout,浴室logout就被拦截了下来,这时logout请求就在这里处理不在进入前端控制器了,在这里定义了拦截后进入的地址,即login.html,这也是为什么拦截器要定义在前端控制器之前的原因,就是要在他之前拦截所有请求
- 页面获取用户名. 我们在后台通过SecurityContextHolder工具类可以获得用户名,前台定义controller接收即可