1:在pom.xml中添加依赖

<!-- 身份验证 -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.1..RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.1..RELEASE</version>
</dependency>

2:在web.xml中添加配置

注意:

classpath:spring/spring-security.xml写的是自己配置sping-security的路径
<!--登录校验-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-security.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<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>

3:在配置文件中配置spring-security.xml

我配置在resources下的spring文件夹下

<?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="/login.html" security="none"/>
<http pattern="/css/**" security="none"/>
<http pattern="/img/**" security="none"/>
<http pattern="/js/**" security="none"/>
<http pattern="/plugins/**" security="none"/> <!-- use-expressions:设置是否启动SpEL表达式,默认值是true。 -->
<http use-expressions="false">
<!--
配置SpringSecurity的拦截路径(拦截规则)
* pattern:配置拦截规则。 /* 代表的是根路径下的所有资源(不包含子路径) /**代表的是根路径下所有的资源(包含子路径)
* access:设置角色 角色命名 ROLE_角色名称 如: ROLE_USER
-->
<intercept-url pattern="/**" access="ROLE_ADMIN"/> <!--
开启表单验证
username-parameter="username"
password-parameter="password"
login-page :登录页面名称 以 / 开始
default-target-url :登录成功后跳转的页面
login-processing-url:提交的路径的设置 默认值"/login" 可以修改
-->
<form-login login-page="/login.html" default-target-url="/admin/index.html" always-use-default-target="true" authentication-failure-url="/login.html"/>
<!-- 不使用csrf的校验 -->
<csrf disabled="true"/>
<!-- 配置框架页面不拦截 -->
<headers>
<frame-options policy="SAMEORIGIN"/>
</headers>
<!-- 注销的配置,这里登录可以先不写 -->
<logout logout-url="/logout" logout-success-url="/login.html" />
</http>
<!-- 配置认证管理器 -->
<authentication-manager>
<!-- 认证的提供者 -->
<authentication-provider>
<user-service>
          <!-- 可以用来登录的用户名和密码 -->
<user name="admin" password="123456" authorities="ROLE_ADMIN"/>
<user name="wc" password="123456" authorities="ROLE_ADMIN"/>
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>

4:登录页面设置及注意事项

注意:表单的action必须为/login 请求方式必须为post请求

用户名和密码输入框的name值必须分别设置为username和password

点击超链接提交表单onclick="document:loginform.submit()"

<form id="loginform" action="/login" method="post">
<input name="username" type="text" placeholder="邮箱/用户名/手机号">
<input name="password" type="password" placeholder="请输入密码">
<a onclick="document:loginform.submit()" target="_blank">登&nbsp;&nbsp;录</a>
</form>

最新文章

  1. (原)android补间动画(四)之插补器Interpolator
  2. ios调用系统相册、相机 显示中文标题、本地化多语言支持
  3. Android复习资料
  4. ThinkPHP自动获取关键词(调用第三方插件)
  5. C#:只运行一个程序
  6. NTFS文件系统简介(转载)
  7. 学习总结 java 数据库 ResultSet 、PreparedStatement
  8. linux - 自动删除n天前日志
  9. Python之时间统计
  10. 基于Struts2的用户登录程序
  11. SVN服务器的搭建 分类: 网络 2014-11-27 01:18 204人阅读 评论(4) 收藏
  12. T-SQL技术收集——删除重复数据
  13. ASCII 码对应表
  14. 双向循环链表(C语言描述)(三)
  15. Vuejs环境安装与工程建立【小白Windows向】
  16. 字节输入流:io包中的InputStream为所有字节输入流的父类。
  17. ss源码学习--事件处理
  18. JAVA框架之Hibernate【配置文件详解】
  19. 4+1视图与UML对应关系
  20. 原创zynq文章整理(MiZ702教程+例程)

热门文章

  1. PHP格林威治时间
  2. jQuery AJAX方法详谈
  3. React Router 4.0 体验
  4. page的js访问全局变量:app.globalData.openid
  5. 【Android】天气应用
  6. Apicloud微信支付iOS可以,安卓返回-1的之避坑指南
  7. centos7 下 通过终端 连接 蓝牙设备
  8. 当请求进入Nginx后,每个HTTP执行阶段的作用
  9. [基础]Deep Learning的基础概念
  10. 201871010105-曹玉中《面向对象程序设计(java)》第八周学习总结