Spring Security 基本介绍和环境搭建

实验介绍

Spring Security 是一个非常强大的身份验证和授权控制框架。为了满足企业项目的不同需求,它提供了很多定制化开发的解决方案,通过简单的调整配置,就能为我们的应用提供一套可靠的安全保障。本节课程主要任务就是搞清楚 Spring Security 是干什么的,以及它的基本用法。

知识点

  • 系统安全和系统保护设计
  • Spring Security 核心功能介绍
  • Spring Security 项目搭建
  • Spring Security 的基本操作

系统安全和系统保护设计

在实际开发过程中,为了保证我们的系统能够安全稳定的运行下去,一般都要从下面两点来考虑:

  1. 系统安全性:防止非法入侵、非法请求、非法拦截等。我们需要阻止和屏蔽不信任的请求源访问,保证数据的安全可靠,不被人窃取。
  2. 系统健壮性:也就是系统可用性,最常见的解决方案就是做服务 “冗余”。当然量级够大的话,要做的事情会很多很多,比如限流、熔断、降级等等。

这里只简单的谈一谈系统的安全性,在项目的开发中需要从全方位、多角度做工作,以确保整个业务链路、整个体系范围都能保证安全。下面就大致介绍下在实际开发过程中,开发者经常用到的一些方法:

  • 数据校验,包括前端 js 校验和后端校验,其实前端校验主要是为了体验,也就是尽可能降低出错率,提高一次性提交的成功率。也可以说前端校验规则是后端校验的子集。

  • 防止命令注入,比如最常见的 SQL 注入,它不是利用操作系统的 BUG 来实现攻击,而是针对程序员编程时的疏忽,通过 SQL 语句,实现无帐号登录,甚至篡改数据库。

  • 认证安全,对于使用应用的实体,无论是人还是系统程序,都应当做到对每个请求都能找到对应的责任实体。因此,在处理请求前,要先对认证信息进行检测。

  • 登录鉴权,即要控制这个用户登录后能在系统中做什么,比如一般要把用户分为外部用户、员工等。

  • 数据加密,对于敏感数据,不得明文传输和明文存储。如数据存储中,密码等信息我们可以加密后再存储;数据传输中,对密文使用 DES3/RSA 加密。

  • 请求签名,在外部请求时也是常见的处理方式,只有通过接口签名验证的请求,才信任为合法的请求。

在系统的安全方面,我们的 Spring Security 框架,解决的最主要的问题就是 认证安全 和 登录鉴权。

Spring Security 核心功能介绍

Spring Security 其核心就是一组过滤器链,项目启动后将会自动配置。最核心的就是 Basic Authentication Filter 用来认证用户的身份,一个在 Spring Security 中一种过滤器处理一种认证方式。比如,对于 username password 认证过滤器来说:

  • 会检查是否是一个登录请求;
  • 是否包含 username 和 password (也就是该过滤器需要的一些认证信息);
  • 如果不满足则放行给下一个。

然后下一个认证过滤器,再次按照自身职责判定是否是自身需要的信息。中间可能还有更多的认证过滤器,只要有一个认证过滤器通过了,就是用户登录成功。

在整个过滤器中的最后一环是 FilterSecurityInterceptor,这里会判定该请求是否能进行访问 REST 服务,如果被拒绝了就会抛出不同的异常(根据具体的原因)。

Exception Translation Filter 会捕获抛出的错误,然后根据不同的认证方式进行信息的返回提示。

项目搭建

概念一下说多了也未必好理解,还是先上手,后面再慢慢熟悉比较好。打开实验楼环境,依次点击 Terminal --> New Terminal(打开命令行),可以看到下方出现命令行工具,创建我们的第一个项目(hello_security),直接输入下面的命令:

mvn archetype:generate -DgroupId=com.shiyanlou -DartifactId=hello_security -DarchetypeArtifactId=maven-archetype-webapp

输入完,然后直接回车即可自动创建项目。我们创建一个基于 Spring Boot 的 Web 项目,maven 全部依赖如下:
hello_security/pom.xml

<dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.8.RELEASE</version><scope>import</scope><type>pom</type></dependency></dependencies>
</dependencyManagement><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
</dependencies>

这个项目就是个简单的 Spring Boot 项目,先把需要的目录创建好,按照下面的目录结构创建出来:

也就是在 src/main/ 下创建 java 目录,再在其下面创建 com/shiyanlou 包目录,同时在该目录下创建我们的 Spring Boot 启动类:
hello_security/src/main/java/com/shiyanlou/Application.java

package com.shiyanlou;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}

再在 src/main/resources/ 下创建 templates 目录,然后在 src/main/resources/templates 目录下创建两个页面:
home.html

<!DOCTYPE html>
<htmlxmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org"xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"
><head><title>home.html</title></head><body><h1>Welcome!</h1><p>Click<a th:href="@{/hello}">here</a>to see a greeting.</p></body>
</html>

hello.html

<!DOCTYPE html>
<htmlxmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org"xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"
><head><title>hello.html</title></head><body><h1>Hello world!</h1></body>
</html>

页面很简单,要做的事也很简单:点击 home 视图中的 “/hello” 链接跳转到 hello 页面。首先配置视图控制器来暴露这些页面, 如下是一个典型的 Spring MVC 配置类:

hello_security/src/main/java/com/shiyanlou/MvcConfig.java

package com.shiyanlou;import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configuration
public class MvcConfig implements WebMvcConfigurer {public void addViewControllers(ViewControllerRegistry registry) {registry.addViewController("/home").setViewName("home");registry.addViewController("/").setViewName("home");registry.addViewController("/hello").setViewName("hello");registry.addViewController("/login").setViewName("login");}

第四个视图控制器引用另一个名为 login 的视图,后面会用上,先不管。现在可以启动项目了,这里注意,在实验楼 WebIDE 中运行项目需要命令行启动,必须在 pom.xml 中添加 maven 插件:

hello_security/pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
><modelVersion>4.0.0</modelVersion><groupId>com.shiyanlou</groupId><artifactId>hello_security</artifactId><version>1.0-SNAPSHOT</version><build><plugins><!--spring boot 打包插件--><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><mainClass>com.shiyanlou.Application</mainClass></configuration><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build>
</project>

同时命令行中切换到自己的项目目录下(和 pom.xml 同级),再执行 mvn spring-boot:run 命令:

等项目启动完成后,点击右侧菜单的 Web 服务 按钮,将会自动打开浏览器,看到下面的运行效果:

本文内容来自蓝桥云课《玩转 Spring Security 从入门到实战》 ,点击可查看全部内容。

Spring Security 基本介绍,初窥路径相关推荐

  1. spring security——基本介绍(一)

    一.spring security 简介 spring security 的核心功能主要包括: 认证 (你是谁) 授权 (你能干什么) 攻击防护 (防止伪造身份) 其核心就是一组过滤器链,项目启动后将 ...

  2. Spring AOP 源码初窥(二) 从注解开始

    版本 spring 5.0.8.BUILD-SNAPSHOT aspectjweaver 1.8.13 从注解开始 由于在本人实际应用中使用的是注解配置AOP,也更倾向于了解Spring AOP的整个 ...

  3. 翼支付门户架构之Spring Security框架介绍

    Spring Security3,其前身是"spring的acegi安全系统". 先来谈一谈Acegi的基础知识,Acegi的架构比较复杂.如果对Web资源进行保护,最好的办法莫过 ...

  4. Spring Security:身份验证入口AuthenticationEntryPoint介绍与Debug分析

    ExceptionTranslationFilter ExceptionTranslationFilter(Security Filter)允许将AccessDeniedException和Authe ...

  5. 【Spring Security】基本功能介绍

    文章目录 1.spring security 简介 spring security 基本原理 2 入门项目 2.1 web工程配置 2.1 加入Spring Security 3. 参数详解 3.1. ...

  6. Spring Security 参考手册(一)

    Spring Security 参考手册 Ben AlexLuke TaylorRob WinchGunnar Hillert Spring security 是一个强大的和高度可定制的身份验证和访问 ...

  7. Spring Security OAuth2.0认证授权知识概括

    Spring Security OAuth2.0认证授权知识概括 安全框架基本概念 基于Session的认证方式 Spring Security简介 SpringSecurity详解 分布式系统认证方 ...

  8. spring security技术分享

    Spring Security技术专题 一.初识认证和授权 1.1 认证 1.2 会话 1.3 授权 1.4 授权的数据模型 1.5 RBAC 1.5.1 角色访问控制 1.5.2 资源访问控制 1. ...

  9. SpringBoot 整合Spring Security(简单版)

    1 写在前面 关于spring security的介绍,网上一大堆,这里就不介绍了,这里直接使用springboot开始整合 2 整个流程 spring security授权和认证的流程大致和shir ...

最新文章

  1. php 手机唯一标示_Php获取移动设备唯一标识
  2. 分支程序与循环程序设计-汇编实验二
  3. java+解析未知json_在Java中解析JSON时如何忽略未知属性– Jackson @JsonIgnoreProperties注释示例...
  4. 未来3-4周可能出现大规模病毒或安全***事件
  5. QCC300X 充电配置 调试笔记
  6. JAVA互联网架构师VIP项目实战(完整)
  7. 如何使用内网穿透,将自己的内网接口暴露到外网
  8. 3000字告诉你如何渡过程序员菜鸟时期
  9. 条形码的正确使用方法和技巧
  10. 汽配供应链管理系统:助力汽配企业实现采购业务全过程数字化管理
  11. 农民抗征地住帐篷夜间起火1死3伤
  12. 数组、数组的定义、对数组的理解
  13. FZOJβ #113 后缀平衡树
  14. Geodesic.WGS84.Inverse通过两点经纬度计算两点间的方位角
  15. 闲谈隐性成本(太多人的思维盲区)
  16. 时钟源系统(时统系统)GPTP对自动驾驶的重要性
  17. (轉貼) 寄發紅帖基本原則(教育部禮儀司頒布) (雜項)
  18. 微博html5版登录,新浪微博模拟登录 支持手动处理验证码
  19. android studio教程,Android Studio一个完整的APP实例
  20. OGNL表达式原理及使用

热门文章

  1. STM32的备份寄存器测试
  2. postman中设置关联点
  3. 1.1 objective-c中的内存管理
  4. JSP生成静态html网页
  5. 卸载 linux http
  6. excel运行最多行数
  7. UVA532 - Dungeon Master(裸BFS)
  8. ADS_LPC2103开发板SPI 4位数码管测试试验
  9. [译]Vulkan教程(32)生成mipmap
  10. IOS登陆+注册+抽奖+排行榜