• Hello SpringSecurity


     👩‍💻博客主页:大家好我是poizxc2014的博客主页
    ✨欢迎关注🖱点赞🎀收藏⭐留言✒
    📖个人主页:poizxc2014的博客_CSDN博客-数据库,mysql,java领域博主
    💻首发时间:🎞2022年08月07日🎠
    🔥💖🔮😘🔏🀄🎧如果觉得博主的文章还不错的话,👍请三连支持一下博主哦🤞
    最后的话,在很多方面还做的不好的地方,欢迎大佬指正,一起学习哦,冲冲冲

    目录

    一、hello-security

    第 1 步:引入依赖

     第 2 步:创建启动类

    第 3 步:创建请求处理资源类

    第 4 步:启动,浏览器访问 http://localhost:8080/hello 验证


    Spring Security 官网文档:https://docs.spring.io/spring-security/reference/index.html

    Spring Security 是一个安全框架,提供了身份验证、授权和对常见攻击的保护。对命令式和响应式应用程序的安全提供了一流的支持,它实际上是保护基于 spring 的应用程序的标准。

    一、hello-security

    第 1 步:引入依赖

    1. "1.0" encoding="UTF-8"?>
    2. "http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. 4.0.0
    5. org.springframework.boot
    6. spring-boot-starter-parent
    7. 2.6.4
    8. example
    9. hello-security
    10. 0.0.1-SNAPSHOT
    11. hello-security
    12. Hello Security
    13. 8
    14. org.springframework.boot
    15. spring-boot-starter-security
    16. org.springframework.boot
    17. spring-boot-starter-web
    18. org.springframework.boot
    19. spring-boot-starter-test
    20. test
    21. org.springframework.security
    22. spring-security-test
    23. test
    24. org.springframework.boot
    25. spring-boot-maven-plugin

     第 2 步:创建启动类

    1. package example.hellosecurity;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. import org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration;
    5. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration;
    6. /**
    7. * Spring Security Servlet Application.
    8. *
    9. * @author poizxc2014
    10. * @date 2022/8/7
    11. */
    12. @SpringBootApplication
    13. public class HelloSecurityApplication {
    14. /**
    15. * Spring Boot 自动:
    16. * 启用 Spring Security 的默认配置,它将创建一个 servlet Filter 作为一个名为 springSecurityFilterChain 的 bean
    17. * 此 bean 负责应用程序内的所有安全性(保护应用程序url、验证提交的用户名和密码、重定向到登录表单,等等)。
    18. * 创建一个 UserDetailsService bean,该 bean 的用户名为 user,并随机生成一个登录到控制台的密码。
    19. * 为每个请求,向 Servlet 容器注册一个名为 springSecurityFilterChain 的 bean 的 Filter。
    20. *
    21. * @param args
    22. * @see WebSecurityConfiguration#springSecurityFilterChain()
    23. * @see UserDetailsServiceAutoConfiguration#inMemoryUserDetailsManager
    24. */
    25. public static void main(String[] args) {
    26. SpringApplication.run(HelloSecurityApplication.class, args);
    27. }
    28. }

    第 3 步:创建请求处理资源类

    1. package example.hellosecurity.rest;
    2. import org.springframework.web.bind.annotation.GetMapping;
    3. import org.springframework.web.bind.annotation.RestController;
    4. @RestController
    5. public class UserResource {
    6. @GetMapping("/hello")
    7. public String hello() {
    8. return "hello spring security";
    9. }
    10. }

    第 4 步:启动,浏览器访问 http://localhost:8080/hello 验证

    • user: user

    • password: 控制台中查找

    Using generated security password: 164cd0e7-d246-405b-bf06-7330ffdf9efe

    二、整体架构

    基于 Servlet Filter

    Spring 提供了一个名为 DelegatingFilterProxy 的过滤器实现,它允许在 Servlet 容器的生命周期和 Spring 的 ApplicationContext 之间进行桥接。

    Servlet 容器允许使用它自己的标准注册 Filters,但它不知道 Spring 定义的 bean。

    DelegatingFilterProxy 可以通过标准的 Servlet 容器机制进行注册,但将所有工作委托给实现 Filter 的 Spring Bean。

    DelegatingFilterProxy 从 ApplicationContext 中查找 Filter0 并调用,下面是伪代码:

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
    	Filter delegate = getFilterBean(someBeanName);
    	delegate.doFilter(request, response);
    }

    FilterChainProxy

    Spring Security 的 Servlet 支持包含在 FilterChainProxy 中。

    FilterChainProxy 是 Spring Security 提供的一个特殊的过滤器,它允许通过 SecurityFilterChain 委托给许多过滤器实例。

    因为 FilterChainProxy 是一个 Bean,所以它通常被封装在 DelegatingFilterProxy 中。

    SecurityFilterChain

    SecurityFilterChain 被 FilterChainProxy 用来决定哪个 Spring Security Filters 应该被这个请求调用。

    在多个 SecurityFilterChain 中,FilterChainProxy 如何决定使用哪个SecurityFilterChain。

    • 只有第一个匹配的 SecurityFilterChain 将被调用。
    • 如果一个URL /api/messages/ 被请求,它将首先匹配 SecurityFilterChain0 的 /api/** 模式,所以只有 SecurityFilterChain0 将被调用,即使它也匹配SecurityFilterChainN。
    • 如果一个URL /messages/ 被请求,它将不匹配 SecurityFilterChain0 的模式 /api/**,所以 FilterChainProxy 将继续尝试每个 SecurityFilterChain

     过滤器的顺序

    ​​​​​​ChannelProcessingFilter
    WebAsyncManagerIntegrationFilter
    SecurityContextPersistenceFilter
    HeaderWriterFilter
    CorsFilter
    CsrfFilter
    LogoutFilter
    OAuth2AuthorizationRequestRedirectFilter
    Saml2WebSsoAuthenticationRequestFilter
    X509AuthenticationFilter
    AbstractPreAuthenticatedProcessingFilter
    CasAuthenticationFilter
    OAuth2LoginAuthenticationFilter
    Saml2WebSsoAuthenticationFilter
    UsernamePasswordAuthenticationFilter
    OpenIDAuthenticationFilter
    DefaultLoginPageGeneratingFilter
    DefaultLogoutPageGeneratingFilter
    ConcurrentSessionFilter
    DigestAuthenticationFilter
    BearerTokenAuthenticationFilter
    BasicAuthenticationFilter
    RequestCacheAwareFilter
    SecurityContextHolderAwareRequestFilter
    JaasApiIntegrationFilter
    RememberMeAuthenticationFilter
    AnonymousAuthenticationFilter
    OAuth2AuthorizationCodeGrantFilter
    SessionManagementFilter
    ExceptionTranslationFilter
    FilterSecurityInterceptor
    SwitchUserFilte 

  • 相关阅读:
    monaco-editor 简单使用
    ExcelPatternTool 开箱即用的Excel工具包现已发布!
    svg常用属性及动画效果
    将vue2组件发布成一个npm包
    叫人头疼的diff算法原理
    EasyExcel实现动态表头功能
    JAVA项目maven
    【NOI模拟赛】最大生成树(Brouvka算法,并查集)
    maven简单配置
    如果Controller里有私有的方法,能成功访问吗?
  • 原文地址:https://blog.csdn.net/poizxc2014/article/details/126219631