Java Spring请求匹配器、AntMatcher和authorizeRequests使用

Java Spring请求匹配器、AntMatcher和authorizeRequests使用,java,spring,spring-security,Java,Spring,Spring Security,我试图理解requestMatchers、antMatchers和authorizeRequests的用途 如果我们只使用requestMatchers会发生什么 http.requestMatchers() 如果使用requestMatchers和antMatchers会发生什么 http.requestMatchers().antMatchers(“/”,“/login”) 如果使用requestMatchers、antMatchers和authorized请求会发生什么 http.requ

我试图理解requestMatchers、antMatchers和authorizeRequests的用途

  • 如果我们只使用requestMatchers会发生什么

    http.requestMatchers()

  • 如果使用requestMatchers和antMatchers会发生什么

    http.requestMatchers().antMatchers(“/”,“/login”)

  • 如果使用requestMatchers、antMatchers和authorized请求会发生什么

    http.requestMatchers().antMatchers(“/”,“/login”)。和()

  • 因此,在案例3中,requestMatchers()是否对“/login”和“/”进行了例外处理,这样它们就不会被验证,或者这是否意味着它们也需要验证

    将requestMatchers()放在authorizeRequests()之前会让人感到困惑。需要知道他们到底在做什么,以及他们会做什么

  • 毫无意义 2.添加一些ant模式的路径,这些路径用于在传入请求中与url匹配,如果匹配,将选择具有这些路径的filterchain来过滤该传入请求。 3.authorizeRequests().anyRequest().authorized()表示通过所选筛选器链对所有传入请求进行身份验证。 总之,requestMatchers用于设置路径以匹配您想要的请求,authorizeRequests用于根据您通过antMatchers(“XXX”).permitAll()等方法设置的规则对这些请求进行身份验证
  • 如果我们只使用requestMatchers会发生什么?http.requestMatchers()

    这只是一个无用的方法调用。在英语中,它的意思是:对于所有传入的http请求,我想根据匹配器列表对它们进行过滤,当然没有匹配列表,所以它什么也不做

    如果使用requestMatchers和antMatchers会发生什么

    http.requestMatchers().antMatchers(“/”,“/login”)

    另一个无用的方法调用。在英语中,它的意思是:对于所有传入的http请求,根据匹配器列表“/”和“/login”对它们进行过滤,但您不告诉它要做什么,所以它也不做任何事情

    如果使用请求匹配器、AntMatcher和 批准请求

    http.requestMatchers().antMatchers(“/”, “/login”)。和().authorizeRequests().anyRequest().authorized()

    那好多了。在英文中,它的意思是:对于所有传入的http请求,我希望根据匹配器列表对它们进行筛选,即“/”和“/”登录”,并且我希望它们只能由经过身份验证的用户访问

    如果您使用的是Spring Security,Spring随后将检查其
    SecurityContext
    (存储在
    ThreadLocal
    变量中)以确定是否存在包含
    主体的
    身份验证
    对象(身份验证用户,通常是
    UserDetails
    对象的实例)。如果有,请求被允许继续,如果没有,你会得到一个丑陋的403,禁止

    所以在案例3中,requestMatchers()是否对“/login”和 “/”所以他们不会通过身份验证,还是说他们也需要身份验证 认证

    如上所述

    问题是,通过SpringSecurity的SpringBoot让我们非常容易,只需要几行配置就可以在坚实的安全基础上开始。试一试,一个小时左右,你就可以开始生产了

    祝你好运,伙计