一 简介:Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作.

 Spring Security入门小Demo

1.创建工程spring-security-demo 引入pom依赖

<--spring相关依赖  略-->
<--security相关依赖-->

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

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

</web-app>

3.创建index.xml

4.配置spring-security.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.xsd

http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">

<!-- 页面拦截规则 -->

<http use-expressions="false">

<intercept-url pattern="/**" access="ROLE_USER" />

<form-login/>

</http>

<!-- 认证管理器 -->

<authentication-manager>

<authentication-provider>

<user-service>

<user name="admin" password="123456" authorities="ROLE_USER"/>

</user-service>

</authentication-provider>

</authentication-manager>

</beans:beans>

use-expressions 为是否使用使用 Spring 表达式语言( SpEL ),默认为true ,如果开启,则

拦截的配置应该写成以下形式

<intercept-url pattern="/**" access="hasRole('ROLE_USER')" />

这样就可以实现拦截了  不过默认有登陆界面   也可以自己定义登陆界面

1.首先自定义一个登陆界面(一个成功界面,一个失败界面)

2.修改spring-security.xml

<!-- 以下页面不被拦截 -->

<http pattern="/login.html" security="none"></http>

<http pattern="/login_error.html" security="none"></http>

<!-- 页面拦截规则 -->

<http use-expressions="false">

<intercept-url pattern="/*" access="ROLE_USER" />

<form-login login-page="/login.html" default-target-url="/index.html" authentication-failure-url="/login_error.html"/>

<csrf disabled="true"/>

</http>

security="none"  设置此资源不被拦截.

login-page:指定登录页面。

authentication-failure-url:指定了身份验证失败时跳转到的页面。

default-target-url:指定了成功进行身份验证和授权后默认呈现给用户的页面。

csrf disabled="true"  关闭csrf ,如果不加会出现错误

ok.

Spring Security 安全框架的更多相关文章

  1. Spring Security安全框架

    今天来简单介绍一下Spring Security安全框架 简介 Spring Security 提供了基于javaEE的企业应有个你软件全面的安全服务.这里特别强调支持使用SPring框架构件的项目, ...

  2. Spring Security安全框架入门篇

    一.Spring Security相关概念 1.1..Spring Security介绍: Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安 ...

  3. springboot集成spring security安全框架入门篇

    一. :spring security的简介 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下 ...

  4. Spring boot 整合spring Data JPA+Spring Security+Thymeleaf框架(上)

    近期上班太忙所以耽搁了给大家分享实战springboot 框架的使用. 以下是spring boot 整合多个框架的使用. 首先是准备工作要做好. 第一  导入框架所需的包,我们用的事maven 进行 ...

  5. Spring Security框架中踢人下线技术探索

    1.背景 在某次项目的开发中,使用到了Spring Security权限框架进行后端权限开发的权限校验,底层集成Spring Session组件,非常方便的集成Redis进行分布式Session的会话 ...

  6. Spring boot +Spring Security + Thymeleaf 认证失败返回错误信息

    [Please make sure to select the branch corresponding to the version of Thymeleaf you are using] Stat ...

  7. 最简单易懂的Spring Security 身份认证流程讲解

    最简单易懂的Spring Security 身份认证流程讲解 导言 相信大伙对Spring Security这个框架又爱又恨,爱它的强大,恨它的繁琐,其实这是一个误区,Spring Security确 ...

  8. spring security入门demo

    一.前言 因项目需要引入spring security权限框架,而之前也没接触过这个一门,于是就花了点时间弄了个小demo出来,说实话,刚开始接触这个确实有点懵,看网上资料写的权限大都是静态,即就是在 ...

  9. Spring Security OAuth2 Demo —— 授权码模式

    本文可以转载,但请注明出处https://www.cnblogs.com/hellxz/p/oauth2_oauthcode_pattern.html 写在前边 在文章OAuth 2.0 概念及授权流 ...

随机推荐

  1. python 简单搭建阻塞式单进程,多进程,多线程服务

    由于经常被抓取文章内容,在此附上博客文章网址:,偶尔会更新某些出错的数据或文字,建议到我博客地址 :  --> 点击这里 我们可以通过这样子的方式去理解apache的工作原理 1 单进程TCP服 ...

  2. 639. Decode Ways II

    A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...

  3. 名词-JS

    1: 构造函数的伪装.(JS继承的时候出现 通过call函数改变this指向的对象) 2: 原型链.(JS继承的时候出现) 3:宿主对象:(有浏览器提供的对象.DOM, BOM  - Document ...

  4. jvm(1)类的加载(三)(线程上下文加载器)

    简介: 类加载器从 JDK 1.0 就出现了,最初是为了满足 Java Applet 的需要而开发出来的. Java Applet 需要从远程下载 Java 类文件到浏览器中并执行. 现在类加载器在 ...

  5. 《JAVA与模式》之适配器模式

    在阎宏博士的<JAVA与模式>一书中开头是这样描述适配器(Adapter)模式的: 适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能 ...

  6. 如何解决liunx链接远程数据库10038错误提示

    关于在windows下链接liunx系统下远程数据库报错2003--提示10038的解决方案如下: 在liunx系统中安装配置mysql数据库默认是没有对外开启3600端口,如果出现10038: 1, ...

  7. Java Web入门学习(三)Maven的配置与使用国内仓库

    一.Maven下载与配置 下载好的压缩包,压缩到D盘,或者其他盘符. 新建系统环境变量 MAVEN_HOME  值是压缩好的Maven目录. 比如: D:\apache-maven- 在path环境变 ...

  8. Java之集合(二十五)ConcurrentHashMap

    转载请注明源出处:http://www.cnblogs.com/lighten/p/7520808.html 1.前言 本章介绍使用的最频繁的并发集合类之一ConcurrentHashMap,之前介绍 ...

  9. Android版本分布——2017年5月更新

    Code Name Version API Level Last month This month Change gingerbread(姜饼) 2.3.3——2.3.7 10 0.9% 1.0% 0 ...

  10. nginx 配置说明及优化

    一.配置说明 1.  worker_processes 8; nginx 进程数,建议按照cpu 数目来指定,一般为它的倍数 (如,2个四核的cpu计为8). 2.  worker_cpu_affin ...