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

spring-security-oauth2之WebSecurityConfigurerAdapter浅析

衔接上文,下面聊聊配置Oauth2服务时的配置类WebSecurityConfigurerAdapter。
1、WebSecurityConfigurerAdapter
WebSecurityConfigurerAdapter继承自WebSecurityConfigurer,在Spring Boot 中的自动配置实际上是通过自动配置包下的 SecurityAutoConfiguration 总配置类上导入的 Spring Boot Web 安全配置类 SpringBootWebSecurityConfiguration 来配置的。
提供的具体方法如下:
在这里插入图片描述
主要配置3个configure:

  • configure(AuthenticationManagerBuilder)
  • configure(HttpSecurity)
  • configure(WebSecurity)

第1个是配置认证管理器AuthenticationManager,第2个主要是配置 Spring Security 中的过滤器链,第3个主要是配置一些路径放行规则。

2、AuthenticationManagerBuilder
void configure(AuthenticationManagerBuilder auth) 用来配置认证管理器AuthenticationManager,说白了就是所有 UserDetails 相关的它都管,包含 PasswordEncoder 密码等。
常见用法:

 //默认认证管理器DaoAuthenticationConfigurer注入用户信息
 @Override
 protected void configure(AuthenticationManagerBuilder auth) throws Exception {
     auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
 }
 //或自定义认证
 protected void configure(AuthenticationManagerBuilder auth) {
     // 自定义认证
     auth.authenticationProvider(customAuthenticationProvider());
 }

3、HttpSecurity
void configure(HttpSecurity http) 这个是我们使用最多的,用来配置 HttpSecurity 。 HttpSecurity 用于构建一个安全过滤器链 SecurityFilterChain ,SecurityFilterChain 最终被注入核心过滤器 。 HttpSecurity 有许多我们需要的配置,可以通过它来进行自定义安全访问策略。
常见用法:

	@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().disable();
        http.csrf().disable();
        http
                .requestMatchers().antMatchers("/oauth/**")
                //拦截上面匹配后的url,需要认证后访问
                .and()
                .authorizeRequests().antMatchers("/oauth/**").authenticated();
        http
                .sessionManagement()
                .invalidSessionUrl("/login")
                .maximumSessions(1)
                .expiredUrl("/login");
    }

相关文章:

  • Windows与网络基础-14-NTFS权限规则
  • 【Python】列表生成式应用的八重境界
  • SOA、ESB、微服务、分布式概念及专业名词阐述
  • IDEA 集成 Github(八)——Git
  • Go程序(Grpc服务)协程数暴涨的原因排查分析
  • Unity新的Input System
  • YOLOv5代码解析(二)
  • kafka系列(一)安装使用及基本原理
  • C# 第七章『I/O数据流』◆第4节:数据流—FileStream 类
  • 物联网开发笔记(2)- 使用Wokwi仿真树莓派Pico点亮LED灯代码分析
  • Vue路由实例
  • 第一章 软考架构师之计算机系统组成与体系结构
  • Mac中无法运行旧版本印象笔记:版本太旧 你的本地印象笔记数据是由新版印象笔记管理
  • Educational Codeforces Round 134 (Rated for Div. 2)
  • 算法设计与分析作业——递归循环
  • [译]Python中的类属性与实例属性的区别
  • 【译】React性能工程(下) -- 深入研究React性能调试
  • ECMAScript入门(七)--Module语法
  • es6--symbol
  • go语言学习初探(一)
  • JS实现简单的MVC模式开发小游戏
  • oschina
  • Terraform入门 - 1. 安装Terraform
  • vue 配置sass、scss全局变量
  • 前端路由实现-history
  • 前端自动化解决方案
  • 算法-图和图算法
  • 译米田引理
  • 硬币翻转问题,区间操作
  • ###项目技术发展史
  • #include到底该写在哪
  • #pragma data_seg 共享数据区(转)
  • (二)什么是Vite——Vite 和 Webpack 区别(冷启动)
  • (附源码)ssm高校志愿者服务系统 毕业设计 011648
  • (附源码)计算机毕业设计ssm基于B_S的汽车售后服务管理系统
  • (机器学习-深度学习快速入门)第一章第一节:Python环境和数据分析
  • (三)Pytorch快速搭建卷积神经网络模型实现手写数字识别(代码+详细注解)
  • (十八)devops持续集成开发——使用docker安装部署jenkins流水线服务
  • (转)linux下的时间函数使用
  • (转载)利用webkit抓取动态网页和链接
  • .NET Core引入性能分析引导优化
  • .NET 动态调用WebService + WSE + UsernameToken
  • .NET 回调、接口回调、 委托
  • .net 怎么循环得到数组里的值_关于js数组
  • .NET 中的轻量级线程安全
  • .NET连接MongoDB数据库实例教程
  • .NET正则基础之——正则委托
  • @AutoConfigurationPackage的使用
  • @ConfigurationProperties注解对数据的自动封装
  • [ Algorithm ] N次方算法 N Square 动态规划解决
  • [Android Pro] android 混淆文件project.properties和proguard-project.txt
  • [BZOJ4016][FJOI2014]最短路径树问题
  • [C#][opencvsharp]opencvsharp sift和surf特征点匹配
  • [C#]C# winform实现imagecaption图像生成描述图文描述生成
  • [C++]模板与STL简介