当前位置: 首页 > article >正文

SpringSecurity(安全)基础

SpringSecurity(安全)

Spring Security,这是一种基于 Spring AOP 和 Servlet 过滤器的安全框架。它提供全面的安全性解决方案,同时在 Web 请求级和方法调用级处理身份确认和授权。他是基于AspectJ的切面经行配置的。

Spring Security是针对Spring项目的安全框架,也是Spring Boot底层安全模块默认的技术选型,他可以是吸纳强大的Web安全控制,对于安全控制,我们仅需要引入spring-boot-starter-security模块,进行少量的配置,即可实现强大的安全管理!

Spring Security的几个重要的类:

  • WebSecurityConfigureAdapter:自定义策略
  • AuthenticationManagerBuilder:自定义认证策略
  • @EnableWebSecurity:开启WebSecurity模式,@Enablexxxx开启某个功能

WebSecurityConfigureAdapter共有三个configure方法:

  • configure(WebSecurity) 通过重载,配置Spring Security的Filter链
  • configure(HttpSecurity) 通过重载,配置如何通过拦截器保护请求
  • configure(AuthenticationManagerBuilder) 通过重载,配置user-detail服务

Spring Security的主要两个目标是“认证”和“授权”(访问控制)。

特征:

  • 对身份验证和授权的全面和可扩展支持
  • 防止攻击,如会话固定、点击劫持、跨站点请求伪造等
  • 服务 API 集成
  • 可选集成与Spring Web MVC

参考官网:https://spring.io/projects/spring-security

Spring Security测试:

需要导入依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId>
</dependency>

写入测试类测试

package com.wj.springsecuiity;import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {//链式编程   授权  用户存储认证protected  void configure(HttpSecurity http,AuthenticationManagerBuilder auth)throws Exception{//首页所有人可以访问,功能也只有对应有权限的人才能访问http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/level1/**").hasRole("vip1");.antMatchers("/level2/**").hasRole("vip2");.antMatchers("/level3/**").hasRole("vip3");//基于数据库的用户存储、认证//    auth.jdbcAuthentication().dataSource(dataSource)//             .usersByUsernameQuery("select account,password,true from user where account=?")//             .authoritiesByUsernameQuery("select account,role from user where account=?");//没有权限默认回到登录页面http.formLogin().loginPage("toLogin");//防止网站工具: get, posthttp.csrf().disable();//关闭//注销,开起来注销功能,跳到页面http.logout().logoutSuccessUrl("/");}//认证  spring Boot 2.1.x可以直接使用//密码编码: passwordEncoder//在Spring Secutiry 5.0+ 新增了很多的加密方法protected  void configure(AuthenticationManagerBuilder auth)throws  Exception{auth.inMemoryAuthentication().withUser("wj").password("123456").roles("vip1","vip2");
.withUser("root").password("123456").roles("vip2","vip3");}
}

标签库

Security框架可以精确控制页面的一个按钮、链接,它在页面上权限的控制实际上是通过它提供的标签来做到的。

1.引入依赖

<dependency><groupId>org.thymeleaf.extras</groupId><artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>

2.在HTML页面引入Spring Security的命名空间

<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">

sec:authorize

这个标签用来决定它的内容是否会被执行.

显示一个特定的链接,如果用户允许点击它.

<div sec:authorize="isAuthenticated()"><a class="item" th:href="@{/toLogin}"><i class="address card icon"></i>登录</a>
</div>

sec:authentication

这个标签允许访问当前的Authentication 对象, 保存在安全上下文中。

<div sec:authorize="isAuthenticated()"><a class="item" th:href="@{/toLogin}">用户名:<span sec:authentication="name"></span>密码:<span sec:authentication="principal.getAuthorities()"></span></a>
</div>

sec:accesscontrollis

这个标签纸在使用Spring Security ACL 模块时才可以使用。它检测一个用逗号分隔的特
定领域对象的需要权限列表。如果当前用户拥有这些权限的任何一个,标签内容就会被执行。否则,就会被略过。

http://www.lryc.cn/news/2417792.html

相关文章:

  • Oracle数据库CDB与PDB
  • ubuntu搜狗输入法
  • 日期操作类(DateFormat与SimpleDateFormat)的区别和使用详解
  • Java中Map详解
  • SQL中的like语句用法
  • 仓库管理WMS软件(Warehouse Management Software)百科解析
  • 在vue中使用CKEditor4富文本编辑器
  • Unity基础三: 什么是Shader
  • CIDR 基础知识
  • SHA1 算法加密技术核心思想
  • 详解Tensorboard及使用教程
  • Android Binder机制解析
  • 【传输层协议】 TCP UDP协议 解析(一)
  • FLOPs如何计算
  • 取拼音字头
  • VB 在Visio 2010 以编程方式创建子进程图
  • 根文件系统(二):busybox
  • 浅谈NBIOT
  • 全网最全python教程,从零到精通(学python有它就够必收藏)_python学习相关博客
  • 如何使用好google学术?
  • js刷新当前页面的5种方式
  • LNMP架构环境搭建(Linux+Nginx+Mysql+PHP)
  • 数组详细讲解
  • loki介绍
  • 正确配置安装和卸载Cygwin
  • Windows server 2016——SQL server 简介与安装
  • 都2023年了,Servlet还有必要学习吗?一文带你快速了解Servlet
  • Activiti入门及案例
  • window硬盘管理
  • 什么是UTF-8编码