5

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

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

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

我的问题是我怎样才能允许访问所有文件/resources/public/而不必为每个文件扩展名定义蚂蚁匹配器,或者我只是小气?

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();
    }

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

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

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

}

我的目录结构在/resources

在此处输入图像描述

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

1 回答 1

3

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

Servlet 上下文

<!-- 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 />

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

在此处输入链接描述


也许你有用这个春天的安全内容。

CustomWebSecurityConfigurerAdapter

我们的 HelloWebSecurityConfiguration 示例演示了 Spring Security 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”的用户进行内存身份验证</li>
  • 允许使用名为“admin”的管理用户进行内存身份验证</li>
  • 忽略任何以“/resources/”开头的请求。这类似于在使用 XML 命名空间配置时配置 http@security=none。
  • 允许任何人(包括未经身份验证的用户)访问网址“/signup”和“/about”</li>
  • 允许任何人(包括未经身份验证的用户)访问 URL“/login”和“/login?error”。在这种情况下, permitAll() 意味着允许访问 formLogin() 使用的任何 URL。
  • 任何以“/admin/”开头的 URL 都必须是管理用户。对于我们的示例,这将是用户“admin”。
  • 所有剩余的 URL 都要求用户成功通过身份验证
  • 使用 Java 配置默认设置基于表单的身份验证。当使用参数“username”和“password”向 URL“/login”提交 POST 时执行身份验证。
  • 显式声明登录页面,这意味着当请求 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 与任何用于Web 且不需要父级的Security 命名空间元素非常相似(即security=none、debug 等)。它允许配置影响所有网络安全的事物。
  • WebSecurityConfigurerAdapter 是一个方便类,它允许对 WebSecurity 和 HttpSecurity 进行自定义。我们可以多次扩展 WebSecurityConfigurerAdapter(在不同的对象中)以复制具有多个 http 元素的行为。
  • 通过格式化我们的 Java 配置代码,它更容易阅读。它可以类似于等效的 XML 命名空间来读取,其中“and()”表示可选地关闭 XML 元素。

Spring Security Java 配置预览

于 2018-03-29T06:20:18.347 回答