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

Con*_*asy 5 java spring-security static-resource

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

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

我已经尝试过 one liner /resources/public/**,但/public/**仍然不允许访问我收到 403 的所有公共资源。因此,在我的 http 配置中,我已经开始定义允许的文件扩展名,但我不喜欢这种方法,因为我的 web 应用程序中有很多不同的扩展名。

我的问题是,如何允许访问所有文件,而/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();
    }
Run Code Online (Sandbox Code Playgroud)

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

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

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

}
Run Code Online (Sandbox Code Playgroud)

我的目录结构在/resources

在此输入图像描述

  • 根据 dur 评论的要求添加。 在此输入图像描述

0ga*_*gam 5

您想要请求分隔符资源或 URL 处理程序映射。这在春天很容易。

\n\n

Servlet上下文

\n\n
<!-- Handles HTTP GET requests for /resources/** by efficiently serving \n    up static resources in the ${webappRoot}/resources directory -->\n\n<resources mapping="/resources/**" location="/resources/" />\n
Run Code Online (Sandbox Code Playgroud)\n\n

\n\n
<default-servlet-handler />\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n

此标记允许将 DispatcherServlet 映射到“/”(从而覆盖容器默认 Servlet 的映射),同时仍允许容器默认 Servlet 处理静态资源请求 [ ...]

\n
\n\n

在此输入链接描述

\n\n
\n\n

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

\n\n

自定义WebSecurityConfigurerAdapter

\n\n

我们的 HelloWebSecurityConfiguration 示例演示了 Spring Security Java 配置可以为我们提供一些非常好的默认值。让\xe2\x80\x99s 看一下一些基本的定制。

\n\n
@EnableWebSecurity\n@Configuration\npublic class CustomWebSecurityConfigurerAdapter extends\n   WebSecurityConfigurerAdapter {\n  @Autowired\n  public void configureGlobal(AuthenticationManagerBuilder auth) {\n    auth\n      .inMemoryAuthentication()\n        .withUser("user")  // #1\n          .password("password")\n          .roles("USER")\n          .and()\n        .withUser("admin") // #2\n          .password("password")\n          .roles("ADMIN","USER");\n  }\n\n  @Override\n  public void configure(WebSecurity web) throws Exception {\n    web\n      .ignoring()\n         .antMatchers("/resources/**"); // #3\n  }\n\n  @Override\n  protected void configure(HttpSecurity http) throws Exception {\n    http\n      .authorizeUrls()\n        .antMatchers("/signup","/about").permitAll() // #4\n        .antMatchers("/admin/**").hasRole("ADMIN") // #6\n        .anyRequest().authenticated() // 7\n        .and()\n    .formLogin()  // #8\n        .loginUrl("/login") // #9\n        .permitAll(); // #5\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
    \n
  • 允许使用名为 \xe2\x80\x9cuser\xe2\x80\x9d 的用户进行内存身份验证
  • \n
  • 允许使用名为\n\xe2\x80\x9cadmin\xe2\x80\x9d 的管理用户进行内存身份验证
  • \n
  • 忽略以 \xe2\x80\x9c/resources/\xe2\x80\x9d 开头的任何请求。这类似于\n使用 XML 命名空间\n配置时配置 http@security=none。
  • \n
  • 允许任何人(包括未经身份验证的用户)访问 URL\n\xe2\x80\x9c/signup\xe2\x80\x9d 和 \xe2\x80\x9c/about\xe2\x80\x9d
  • \n
  • 允许任何人(包括未经身份验证的用户)访问 URL\n\xe2\x80\x9c/login\xe2\x80\x9d 和 \xe2\x80\x9c/login?error\xe2\x80\x9d。在这种情况下,permitAll() 意味着\n允许访问 formLogin() 使用的任何 URL。
  • \n
  • 任何以 \xe2\x80\x9c/admin/\xe2\x80\x9d 开头的 URL 都必须是管理用户。\n对于我们的示例,这将是用户 \xe2\x80\x9cadmin\xe2\x80\x9d。
  • \n
  • 所有剩余的 URL 都要求用户成功\通过身份验证
  • \n
  • 使用 Java 配置\n默认设置基于表单的身份验证。当使用参数 \xe2\x80\x9cusername\xe2\x80\x9d 和 \xe2\x80\x9cpassword\xe2 将 POST 提交到\nURL \xe2\x80\x9c/login\xe2\x80\x9d 时执行身份验证\x80\x9d。
  • \n
  • 明确声明登录页面,这意味着当请求 GET /login 时,开发人员需要呈现登录页面。
  • \n
\n\n

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

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

与 XML 命名空间的相似之处

\n\n

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

\n\n
    \n
  • HttpSecurity 与 http 命名空间元素非常相似。它允许为特定选择(在本例中为所有)请求配置基于 Web 的安全性。
  • \n
  • WebSecurity 与任何用于 Web 且不需要父级的 Security 命名空间元素非常相似(即 security=none、\ndebug 等)。它允许配置影响所有网络安全的事物。
  • \n
  • WebSecurityConfigurerAdapter 是一个方便的类,允许自定义 WebSecurity 和 HttpSecurity。我们可以多次扩展 WebSecurityConfigurerAdapter(在不同的对象中)来复制具有多个 http 元素的行为。
  • \n
  • 通过格式化我们的 Java 配置代码,它更容易阅读。\n它的读取方式与 XML 命名空间等效,其中 \xe2\x80\x9cand()\xe2\x80\x9d\n 表示可选择关闭 XML 元素。
  • \n
\n\n

Spring Security Java 配置预览

\n