Spring security 基本原理(一)

SpringSecurity是一个强大的安全框架,它提供了声明式的安全访问控制。核心功能包括认证和授权,通过一系列过滤器链实现。UsernamePasswordAuthenticationFilter负责获取用户信息,ExceptionTranslationFilter捕获认证异常,FilterSecurityInterceptor执行权限验证。配置SpringSecurity后,所有请求默认需要验证。在SpringBoot应用中,添加依赖并自定义WebSecurityConfigurerAdapter配置,即可快速实现安全控制。用户认证涉及AuthenticationManager、AuthenticationProvider和UserDetailsService等组件,授权则依赖于FilterInvocation、SecurityMetadataSource和AccessDecisionManager。了解这些核心组件,能更好地理解和使用SpringSecurity。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring Security介绍

Spring Security 是一个基于Spring的安全框架,Spring boot更好的集成了Spring Security的开发工作。在Spring 框架基础上,Spring Security 充分利用了依赖注入DI和 面向切面编程AOP功能,为应用系统提供声明式的安全访问控制功能,减少了企业安全控制编写大量重复代码的功能,是一个轻量级的安全框架。

Spring Security 的核心功能有哪些?

1、 认证(Authentication):指的是验证某个用户是否为系统的合法主体,也就是说用户能否访问该系统。
2、授权(Authorization):指的是验证某个用户是否有权限执行某个操作。

Spring Security原理

在这里插入图片描述
Spring Security 功能实现主要是由一系列的过滤器链相互配合完成的。
绿色的过滤器链可以通过配置进行修改的,其他部分的过滤器不能进行更改。

请求过程:

  • 请求通过UsernamePasswordAuthenticationFilter获取用户信息
  • ExceptionTransationFilter是认证异常进行捕获,
  • FilterSecurityInterceptor 是对在 WebSecurityConfigurerAdapter的实现类中的指定的权限进行验证。
  • 处理自己实现的controller 接口代码。

1、springSecurityFilterChain中各个过滤器怎么创建的只需了解即可。不要太过关注。

2、重点记忆UsernamePasswordAuthenticationFilter,ExceptionTranslationFilter, FilterSecurityInterceptor这三个过滤器的作用及源码分析。

3、重点记忆认证中Authentication,AuthenticationManager,ProviderManager, AuthenticationProvider,UserDetailsService,UserDetails这些类的作用及源码分析。

4、重点记忆授权中FilterInvoction,SecurityMetadataSource,AccessDecisionManager的作用。

框架的核心组件

SecurityContextHolder: 提供对SecurityContext的访问。
SecurityContext: 持有uthentication对象和其他可能需要的信息。
AuthenticationManager: 其中可以包含多个AuthenticationProvider。
ProviderManager: 为AuthenticationManager接口的实现类。
AuthenticationProvider: 主要用来进行认证操作的类,调用其中的authenticate()方法进行认证操作。
Authentication: Spring Security方式的认证主体。
GrantedAuthority: 对认证主题的应用层面的授权,含当前用于的权限信息,通常使用角色表示。
**UserDetails:**构建Authentication对象必须的信息,可以自定义,可能需要访问db得到。
**UserDetailsService:**通过username构建UserDetails对象,通过loadUserByUsername根据username获取UserDetails对象。

基于Spring boot的demo

1、 添加依赖

添加依赖后,默认所有的请求就都需要进行验证。

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

2、自定义配置

实现WebSecurityConfigurerAdapter 类

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 定义请求方式 是form表单 还是 basics模式
        http.formLogin()
//          http.httpBasic()
                .and()
                .authorizeRequests()
                .anyRequest()
                .authenticated();
    }
}

3、controller

@Controller
public class HelloController {

    @GetMapping("/hello")
    @ResponseBody
    public String hello () {
        return "hello spring security";
    }
}

当请求 /hello 时会自动跳转到login登录页面。
需要在配置类中 指定为 form表单格式。

basic模式:是通过请求头Authentication: Basic 的方式进行传输用户信息,在BasicAuthenticationFilter中进行base64 解码,获取用户信息。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值