1.pom.xml文件引入依赖
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.1.0.RELEASE</version>
</dependency>
2.配置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>
<!--过滤器 spring-security的入口-->
<filter>
<!--springSecurityFilterChain名称固定,不能改变. -->
<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.配置spring-security.xml文件
**说明:
beans本身是默认前缀.
这里配置了beans:beans 前缀 ,如果创建其他bean则需要修改标签为:
<beans:bean>XXX</beans:bean>
因为是spring-security配置文件,所以里面主要是对spring-security的配置,加上前缀后,大量配置springsecurity时无需添加前缀,可直接配置,当配置其他少量的bean时加上beans标签即可.
–配置之后,只要默认显示的前缀,都为spring-security的配置信息.
<?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.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
<!-- 放行资源 ,不登入也可以访问(登入页面.失败页面.静态资源.注册页面.退出页面等等) -->
<http pattern="/*.html" security="none"> </http>
<http pattern="/css/**" security="none"> </http>
<http pattern="/img/**" security="none"> </http>
<http pattern="/js/**" security="none"> </http>
<http pattern="/plugins/**" security="none"> </http>
<!-- 1.配置页面的拦截规则
pattern=/* :拦截当前文件目录下的所有资源(不包括子目录)
pattern=/** :包括该目录及子目录下的所有文件夹
access="ROLE_ADMIN" :允许通过的角色 登入用户拥有该角色则放行.
use-expressions="false" :是否启用SPEL表达式 默认true 如果开启,则access="hasRole(ROLE_ADMIN)"
form-login : 开启表单登入功能 ,默认接收的登入账号字段为username 密码字段password 提供了/login 提交方式必须为post
login-page :登入页面
default-target-url :默认登入的页面
authentication-failure-url :页面登入失败后访问的页面
always-use-default-target="true" :登入成功后默认跳转页面
csrf disabled="true" :关闭CRSF保护 (如果所有页面为HTML则需要关闭才能访问)
frame-options policy="SAMEORIGIN" :允许使用其他框架
-->
<http use-expressions="false">
<intercept-url pattern="/**" access="ROLE_ADMIN" />
<form-login login-page="/login.html" default-target-url="/admin/index.html"
authentication-failure-url="/login.html" always-use-default-target="true"
/>
<csrf disabled="true"/>
<headers>
<frame-options policy="SAMEORIGIN"/>
</headers>
</http>
<!-- 2.配置认证管理器
authentication-provider :认证提供者
user-service :配置当前用户
authorities :当前用户拥有的角色
-->
<authentication-manager>
<authentication-provider>
<user-service>
<user name="root" password="root" authorities="ROLE_ADMIN"/>
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
1.登入form表单 提交按钮可以给表单设置ID
οnclick=“document:loginForm.submit()”
设置单击事件提交
2.spring-security默认拦截其他框架
<!-- 内容区域 -->
<div class="content-wrapper">
<iframe width="100%" id="iframe" name="iframe" onload="SetIFrameHeight()"
frameborder="0" src="home.html"></iframe>
</div>