Spring security - authorizerequest()、anyRequest() 和authenticated() 有什么作用?

wol*_*olf 2 java rest spring spring-security spring-boot

在下面的代码中,不同的链式方法有什么作用? PUBLIC_URL是一个包含公共 URL 的字符串数组。

protected void configure(HttpSecurity http ) throws Exception {

    http.authorizeRequests()
        .antMatchers(PUBLIC_URL).permitAll()
        .anyRequest().authenticated();

}
Run Code Online (Sandbox Code Playgroud)

Pat*_*mil 5

  • authorizeRequests()允许基于HttpServletRequest使用RequestMatcher实现限制访问。

  • permitAll()这将允许任何人都可以在没有身份验证的情况下访问端点PUBLIC_URL 的公共访问

  • anyRequest().authenticated()将限制对PUBLIC_URL以外的任何其他端点的访问,并且用户必须经过身份验证。

我们还可以根据权限配置访问,可以管理会话、HTTPS 通道等等。您可以从configure(HttpSecurity http) 中找到更多详细信息