首页 > 解决方案 > Spring security - authorizerequest()、anyRequest() 和 authenticated() 做什么?

问题描述

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

protected void configure(HttpSecurity http ) throws Exception {

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

}

标签: javaspringspring-bootrestspring-security

解决方案


  • authorizeRequests()允许基于HttpServletRequestusingRequestMatcher实现来限制访问。

  • permitAll()这将允许任何人无需身份验证即可访问端点PUBLIC_URL的公共访问

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

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


推荐阅读