在 spring 安全中提供静态资源 - 允许访问“/resources/public”中的所有文件

Serving static resources in spring security - Allow access to all files in '/resources/public'

我可以使用此设置完美地提供静态资源,但是我必须逐个文件地定义允许提供的文件。

我当前的用例是位于 /resources/public/ 目录中的任何内容都应该允许客户端访问。

我已经尝试了一个班轮 /resources/public/**/public/** 仍然不允许访问所有 public 资源我得到 403。所以在我的 http 配置中,我已经开始定义允许的文件扩展名,但我不喜欢这种方法,因为我的 webapp 中有很多不同的扩展名。

我的问题是我如何允许访问 /resources/public/ 中的所有文件而不必为每个文件扩展名定义 ant 匹配器,或者我只是小气?

Spring WebSecurityConfigurerAdapter - 根据 jmw5598 的回答编辑。

    @Override
    public void configure(WebSecurity web) throws Exception {
           web
            .ignoring()
            .antMatchers("/resources/**");
    }


    @Override
    protected void configure(HttpSecurity http) {

            http
                .authorizeRequests()
                        .authorizeRequests()
                .antMatchers(
                    "/public/**",
                    "/.svg", "/.ico", "/.eot", "/.woff2",
                    "/.ttf", "/.woff", "/.html", "/.js",
                    "/.map", "/*.bundle.*",
                    "/index.html", "/", "/home", "/dashboard")
                .permitAll()
                .anyRequest().authenticated();
    }

用于服务网络应用程序的控制器:

@CrossOrigin(origins = "http://localhost:4200", maxAge = 3600)
@Controller
public class AngularWebAppController {

    @GetMapping(value = "/{path:[^\.]*}")
    public String redirect() {
        return "forward:/";
    }

}

我的目录结构在/resources

您想请求分隔符资源或 URL 处理程序映射。这在 Spring.

中很容易

Servelet-context

<!-- Handles HTTP GET requests for /resources/** by efficiently serving 
    up static resources in the ${webappRoot}/resources directory -->

<resources mapping="/resources/**" location="/resources/" />

<default-servlet-handler />

This tag allows for mapping the DispatcherServlet to "/" (thus overriding the mapping of the container's default Servlet), while still allowing static resource requests to be handled by the container's default Servlet [...]

enter link description here


也许这个 spring 安全内容对您有用。

CustomWebSecurityConfigurerAdapter

我们的 HelloWebSecurityConfiguration 示例演示了 Spring 安全 Java 配置可以为我们提供一些非常好的默认值。让我们来看看一些基本的定制。

@EnableWebSecurity
@Configuration
public class CustomWebSecurityConfigurerAdapter extends
   WebSecurityConfigurerAdapter {
  @Autowired
  public void configureGlobal(AuthenticationManagerBuilder auth) {
    auth
      .inMemoryAuthentication()
        .withUser("user")  // #1
          .password("password")
          .roles("USER")
          .and()
        .withUser("admin") // #2
          .password("password")
          .roles("ADMIN","USER");
  }

  @Override
  public void configure(WebSecurity web) throws Exception {
    web
      .ignoring()
         .antMatchers("/resources/**"); // #3
  }

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http
      .authorizeUrls()
        .antMatchers("/signup","/about").permitAll() // #4
        .antMatchers("/admin/**").hasRole("ADMIN") // #6
        .anyRequest().authenticated() // 7
        .and()
    .formLogin()  // #8
        .loginUrl("/login") // #9
        .permitAll(); // #5
  }
}

假设我们调整 AbstractAnnotationConfigDispatcherServletInitializer 以加载我们的新配置,我们的 CustomWebSecurityConfigurerAdapter 将执行以下操作:

  • 允许使用名为“user”的用户进行内存验证
  • 允许使用名为的管理用户进行内存验证 “管理员”
  • 忽略任何以“/resources/”开头的请求。这类似于 使用 XML 命名空间时配置 http@security=none 配置。
  • 允许任何人(包括未经身份验证的用户)访问 URLs “/注册”和“/关于”
  • 允许任何人(包括未经身份验证的用户)访问 URLs “/登录”和“/登录?错误”。在这种情况下的 permitAll() 意味着, 允许访问 formLogin() 使用的任何 URL。
  • 任何以“/admin/”开头的 URL 必须是管理用户。 对于我们的示例,这将是用户“admin”。
  • 所有剩余的 URL 要求用户成功 认证
  • 使用 Java 配置设置基于表单的身份验证 默认值。当 POST 提交给 URL “/login”,参数为“用户名”和“密码”。
  • 明确说明登录页面,这意味着开发者是 需要在请求 GET /login 时呈现登录页面。

对于那些熟悉基于 XML 的配置的人来说,上面的配置与下面的 XML 配置非常相似:

<http security="none" pattern="/resources/**"/>
<http use-expressions="true">
  <intercept-url pattern="/logout" access="permitAll"/>
  <intercept-url pattern="/login" access="permitAll"/>
  <intercept-url pattern="/signup" access="permitAll"/>
  <intercept-url pattern="/about" access="permitAll"/>
  <intercept-url pattern="/**" access="hasRole('ROLE_USER')"/>
  <logout
      logout-success-url="/login?logout"
      logout-url="/logout"
  />
  <form-login
      authentication-failure-url="/login?error"
      login-page="/login"
      login-processing-url="/login"
      password-parameter="password"
      username-parameter="username"
  />
</http>
<authentication-manager>
  <authentication-provider>
    <user-service>
      <user name="user" 
          password="password" 
          authorities="ROLE_USER"/>
      <user name="admin" 
          password="password" 
          authorities="ROLE_USER,ROLE_ADMIN"/>
    </user-service>
  </authentication-provider>
</authentication-manager>

与 XML 命名空间的相似之处

看完我们稍微复杂一点的例子后,您可能会发现 XML 命名空间和 Java 配置之间的一些相似之处。以下是一些比较有用的要点:

  • HttpSecurity 与 http 名称空间元素非常相似。它 允许为特定选择配置基于 Web 的安全性(在 这种情况下所有)请求。
  • WebSecurity 与任何 Security 命名空间元素非常相似 适用于网络且不需要父级(即安全=none, 调试等)。它允许配置影响所有网络的东西 安全。
  • WebSecurityConfigurerAdapter 是一种便利 class 允许 对 WebSecurity 和 HttpSecurity 的自定义。我们可以扩展 WebSecurityConfigurerAdapter 多次(在不同的对象中)以 复制具有多个 http 元素的行为。
  • 通过格式化我们的 Java 配置代码,它更容易阅读。 它的读法类似于 XML 命名空间等价物,其中“and()” 表示可选地关闭一个 XML 元素。

Spring Security Java Config Preview