Spring Security学习笔记入门(一)

来源:互联网 发布:淘宝聊天记录怎么查 编辑:程序博客网 时间:2024/06/07 03:50

什么是 Spring Security?
Spring Security,这是一种基于Spring AOP和Servlet过滤器 [7] 的安全框架。它提供全面
的安全性解决方案,同时在 Web 请求级和方法调用级处理身份确认和授权。在 Spring
Framework 基础上,Spring Security 充分利用了依赖注入(DI,Dependency Injection)和
面向切面技术。

spring Security3.0.7下载
链接:http://pan.baidu.com/s/1gfqvVph 密码:ap89

解压后目录
这里写图片描述

dist:目录中存放发布包
docs:存放文档

新建一个web项目
这里写图片描述

将下载的压缩包打开,在dist目录下找到两个war包,随意解压一个,将其中的jar文件靠到新建的项目中的lib
这里写图片描述

添加后的jar
这里写图片描述

spring-security-core-3.0.7.RELEASE.jar :包含了核心认证和权限控制类和接口, 运程支持
和基本供应 API。使用 Spring Security 所必须的。支持单独运行的应用, 远程客户端,方法
(服务层)安全和 JDBC 用户供应

spring-security-web-3.0.7.RELEASE.jar :包含过滤器和对应的 web 安全架构代码。任何需
要依赖 servlet API 的。如果需要 Spring Security Web 认证服务和基于 URL 的权限控制都需
要它

开始配置文件

web.xml
在 web.xml 文件中添加一个过滤器,这个过滤器不在 security 包中. ,它可以代理一个
application context 中定义的 Spring bean 所实现的 filte

DelegatingFilterProxy 这个类在 spring-web-3.0.0RELEASE.jar 包中

<?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" id="WebApp_ID" version="2.5">  <display-name>springSecurity</display-name>  <!-- 配置springSecurity过滤器 -->  <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>  <!-- 通过监听器启动 spring 容器 -->   <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>             /WEB-INF/applicationContext.xml            /WEB-INF/springSecurity.xml        </param-value>    </context-param>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>

注:过滤器名称必须为springSecurityFilterChain,否则会报错
这里写图片描述

springSecurity.xml配置

<?xml version="1.0" encoding="UTF-8"?><b:beans xmlns="http://www.springframework.org/schema/security"    xmlns:b="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-3.0.xsd                        http://www.springframework.org/schema/security                         http://www.springframework.org/schema/security/spring-security-3.0.xsd">    <!-- 这表示,我们要保护应用程序中的所有 URL,只有拥有  ROLE_USER 角色的用户才能访问 -->    <http auto-config="true">        <intercept-url pattern="/*" access="ROLE_USER"/>    </http>    <!-- 配置认证管理器 -->    <authentication-manager>        <authentication-provider>            <user-service>                <user name="user" password="user" authorities="ROLE_USER"/>            </user-service>        </authentication-provider>    </authentication-manager></b:beans>
<http> 元素:是所有 web 相关的命名空间功能的上级元素。 <intercept-url> 元素:定义了pattern ,用来匹配进入的请求 URL,上面的表示拦截根,以及根子目录下的所有的路径。access 属性定义了请求匹配了指定模式时的需求。使用默认的配置,这个一般是一个逗号分隔的角色队列,一个用户中的一个必须被允许访问请求。 前缀“ROLE_”表示的是一个用户应该拥有的权限比对。

编写首页index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>首页</title></head><body>    欢迎来到springSecurity世界!</body></html>

启动我们的项目,然后访问首页 index.jsp,我们发现首页并没有出现,而是跳转到了 一个登录页面上。因为项目刚启动,第一次访问的时候,没有任何用户登录,而在配置文件中我们拦截的是所有的请求,所以第一次请求被拦截了。
这里写图片描述

查看页面源代码
这里写图片描述

输入刚才在配置文件中配置的 用户名:user,密码:user,提交后,发现能够正确的转到首页

这里写图片描述

我们没有配置登录页面,那这个页面是从那里来的呢?
当有请求道来的时候,Spring Security 框架开始检查要访问的资源是否有权访问,如果当
前登录用户无权或者当前根本就没有用户登录,则 Spring Securtiy 框架就自动产生一个 登录
页面。
当在登录页面进行了正确的登录后,Spring Security 会自动进行登录验证,如果成功登录,
将用户信息放到 session 中,然后转到先前请求的页面上。

原创粉丝点击