Spring Security简单总结

Spring Security是一个基于Filter的安全框架,用于处理HTTP请求的认证和授权。它通过Request Matching来决定过滤器链的适用性。配置包括引入依赖、设置自定义规则以及授权逻辑。在实际应用中,需要启用Web安全配置,并根据需求定制规则。
摘要由CSDN通过智能技术生成

技术原理

Spring security 就是一个filter,然后又把request 分发给spring security内部注册的filters.

 

Note:Request Matching for Dispatch and Authorization

A security filter chain (or equivalently a WebSecurityConfigurerAdapter) has a request matcher that is used for deciding whether to apply it to an HTTP request. Once the decision is made to apply a particular filter chain, no others are applied. But within a filter chain you can have more fine grained control of authorization by setting additional matchers in the HttpSecurity configurer

使用步骤:

  1. 引入依赖

    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        <!--<version>3.0.4.RELEASE</version>-->
    </dependency>
    <!-- 导入依赖  -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
  2. 配置相关文件,

    就是配置自己rule认证跟授权规则,后面稍微解释。

  3. 测试代码编写

配置自己网站的rule 认证跟授权规则

// 适配器模式 不影响原来业务代码的情况下,实现了动态增加功能!
@Configuration
@Order(SecurityProperties.BASIC_AUTH_ORDER - 10)
public class ApplicationConfigurerAdapter extends WebSecurityConfigurerAdapter {
  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.antMatcher("/foo/**")
     ...;
  }
}

@EnableWebSecurity 可能应该也是需要的,然后根据网站的跳转逻辑添加自己的规则。

参考文档

https://spring.io/guides/topicals/spring-security-architecture

https://docs.spring.io/spring-security/site/docs/5.3.1.BUILD-SNAPSHOT/reference/html5/#modules

https://www.thymeleaf.org/doc/articles/springsecurity.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值