SpringSecurity

本文详细介绍了SpringSecurity框架,包括其核心概念、身份验证模式、模块划分、使用方式和多个实验,如默认及自定义登录页、自定义表单登录逻辑分析等。SpringSecurity提供了强大的安全服务,支持多种身份验证方式,如HTTP BASIC、Digest、OpenID等,并通过Java配置和XML方式实现细粒度的安全控制。此外,文章还深入探讨了登录逻辑、自定义认证用户信息和防止跨站请求伪造(CSRF)的方法。
摘要由CSDN通过智能技术生成

第一章 SpringSecurity-简介

1. 简介

https://docs.spring.io/spring-security/site/docs/4.2.10.RELEASE/guides/html5/helloworld-xml.html

SpringSecurity融合Spring技术栈,提供JavaEE应 用的整体安全解决方案;
Spring Security为基于Java EE的企业软件应用提供全面的安全服务。
Spring Security只需要少量配置,就能构建一个强大的安全的应用系统。
目前市面上受欢迎的两个安全框架:Apache Shiro、SpringSecurity;
SpringSecurity可以无缝整合Spring应用,具有强大的自动化web安全管控功能。而Shiro是一个轻量级强大的安全框架,可以脱离web应用来提供安全管控,但是对于web的一些定制安全需要手动编写;SpringBoot底层默认整合SpringSecurity作为安全框架,所以我们推荐web应用使用SpringSecurity来控制安全;

2. 概念

认证

authentication:身份验证
“身份验证”是指建立主体(principal)的过程,主体就是他们声称是谁(“主体”通常指用户、设备或在应用程序中可以执行动作的其他系统)。也就是“证明你是谁”
授权
authorization:授权
“授权”是指确定主体(principal)是否被允许执行系统中某个动作的过程。 也就是“你能做什么!”
为了达到“授权”决策(安全框架决定你是否有权限做此事),“身份验证”(authentication)过程已经建立了主体的身份(Principal)

3. 文档

Hello Spring Security <https://github.com/spring-projects/spring-security/tree/4.2.10.RELEASE/samples/javaconfig/helloworld > 基于Java配置整合示例
Hello Spring Security Boot https://github.com/spring-projects/spring-security/tree/4.2.10.RELEASE/samples/boot/helloworld 与SpringBoot整合案例
Hello Spring Security XML https://github.com/spring-projects/spring-security/tree/4.2.10.RELEASE/samples/xml/helloworld 基于XML方式整合示例
Hello Spring MVC Security https://github.com/spring-projects/spring-security/tree/4.2.10.RELEASE/samples/javaconfig/hellomvc SpringMVC集成示例
Custom Login Form https://github.com/spring-projects/spring-security/tree/4.2.10.RELEASE/samples/javaconfig/form 自定义登录表单示例

4. 支持的身份认证模式

在身份验证级别,Spring Security支持广泛的认证模型。这些认证模型中的大部分要么由第三方提供,要么由相关标准机构(如互联网工程任务组)开发。此外,Spring Security提供了自己的一套身份验证功能。具体而言,Spring Security当前支持与所有这些技术的身份验证集成;
HTTP BASIC身份验证标头
HTTP BASIC authentication headers (an IETF RFC-based standard)
参考:https://blog.csdn.net/lvxinzhi/article/details/49000003
HTTP Digest身份验证标头
HTTP Digest authentication headers (an IETF RFC-based standard)
HTTP X.509客户端证书交换
HTTP X.509 client certificate exchange (an IETF RFC-based standard)
LDAP(一种非常常见的跨平台身份验证方法,特别是在大型环境中)
LDAP (a very common approach to cross-platform authentication needs, especially in large environments)
** 基于表单的身份验证(用于简单的用户界面需求)**
Form-based authentication (for simple user interface needs)
** OpenID身份验证**
OpenID authentication
** 基于预先建立的请求标头的身份验证**
Authentication based on pre-established request headers (such as Computer Associates Siteminder)
** Jasig中央认证服务(也称为CAS,是一种流行的开源单点登录系统)**
Jasig Central Authentication Service (otherwise known as CAS, which is a popular open source single sign-on system)
** 远程方法调用(RMI)和HttpInvoker(Spring远程协议)的透明身份验证上下文传播**
Transparent authentication context propagation for Remote Method Invocation (RMI) and HttpInvoker (a Spring remoting protocol)
** 自动“记住我”身份验证**
Automatic “remember-me” authentication (so you can tick a box to avoid re-authentication for a predetermined period of time)
** 匿名身份验证(允许每个未经身份验证自动承担特定的安全身份)**
Anonymous authentication (allowing every unauthenticated call to automatically assume a particular security identity)
** Runas身份验证(如果一个调用应继续使用不同的安全标识,则非常有用)**
Run-as authentication (which is useful if one call should proceed with a different security identity)
** Java身份验证和授权服务(JAAS)**
Java Authentication and Authorization Service (JAAS)
** JavaEE容器身份验证(如果需要,您仍然可以使用容器管理身份验证)**
Java EE container authentication (so you can still use Container Managed Authentication if desired)
** Java开源单点登录(JOSSO)**
Java Open Source Single Sign-On (JOSSO) *
** OpenNMS网络管理平台***
OpenNMS Network Management Platform *
** 您自己的身份验证系统**
Your own authentication systems (see below)
** 其他**
Kerberos
AppFuse *
AndroMDA *
Mule ESB *
Direct Web Request (DWR) *
Grails *
Tapestry *
JTrac *
Jasypt *
Roller *
Elastic Path *
Atlassian Crowd *

5. 模块划分

Core - spring-security-core.jar
核心模块 核心认证、授权功能、支持jdbc-user功能、支持独立的Spring应用
Remoting - spring-security-remoting.jar
远程交互模块 一般不需要,可以使用Spring Remoting功能简化远程客户端交互
Web - spring-security-web.jar
web安全模块 web项目使用,基于URL的访问控制(access-control)
Config - spring-security-config.jar
java配置模块 必须依赖包,包含解析xml方式和java 注解方式来使用SpringSecurity功能
LDAP - spring-security-ldap.jar
ldap(轻量目录访问协议)支持模块 可选依赖包,LDAP功能支持
ACL - spring-security-acl.jar
ACL支持 ACL(Access-Control-List)访问控制列表
细粒度的资源访问控制(RBAC+ACL)
CAS - spring-security-cas.jar
CAS整合支持 CAS(Central Authentication Service)中央认证服务。开源ApereoCAS整合
OpenID - spring-security-openid.jar
OpenID认证方式支持 OpenID Web身份验证支持。 用于针对外部OpenID服务器对用户进行身份验证
(微信,qq,新浪微博等第三方登录)
Test - spring-security-test.jar
测试模块 快速的测试SpringSecurity应用

6. 4种使用方式

 一种是全部利用配置文件,将用户、权限、资源(url)硬编码在xml文件中
 二种是用户和权限用数据库存储,而资源(url)和权限的对应采用硬编码配置
 三种是细分角色和权限,并将用户、角色、权限和资源均采用数据库存储,并且自定义过滤器,代替原有的FilterSecurityInterceptor过滤器, 并分别实现AccessDecisionManager、InvocationSecurityMetadataSourceService和UserDetailsService,并在配置文件中进行相应配置。
 四是修改springsecurity的源代码,主要是修改InvocationSecurityMetadataSourceService和UserDetailsService两个类。
 InvocationSecurityMetadataSourceService
 将配置文件或数据库中存储的资源(url)提取出来加工成为url和权限列表的Map供Security使用
 UserDetailsService
 提取用户名和权限组成一个完整的(UserDetails)User对象,该对象可以提供用户的详细信息供AuthentationManager进行认证与授权使用

第二章 SpringSecurity-HelloWorld

2.1 测试环境搭建
2.1.1 创建普通maven-war工程:spring-security-helloworld
 pom文件增加依赖

<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.3.20.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.2</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>

2.1.2 web.xml配置

<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

2…1.3 spring配置:spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值