【问题标题】:Spring security how to allow root url without intefering with security 'below' the rootSpring security如何允许root url而不干扰“低于”根的安全性
【发布时间】:2021-06-19 12:55:45
【问题描述】:

我有 REST 服务和静态页面,它们都是由我的 Spring Boot 应用程序提供的。

应用程序操作将在 /myapp 下进行,而在 /myapp/api 下有一些受过滤器保护的服务。 过滤器需要一个 cookie。

@Override
protected void configure(final HttpSecurity http) throws Exception {

   http.authorizeRequests()
            .antMatchers("/api/**")
            .permitAll()
            .and()
            .addFilter(new CookieFilter(...));
}

'root-context (/myapp) 中未受保护的页面没有 cookie。

如何配置静态页面被 Spring 安全“忽略”?静态页面下方的 REST 端点是否经过安全检查?

当我尝试通过网络安全为“排除”配置静态页面时,/myapp/api 下的所有 REST 端点也会被忽略

@Override
public void configure(final WebSecurity web) {
   web.ignoring()
            .antMatchers("/");
}

如果我配置了 permitAll():

@Override
protected void configure(final HttpSecurity http) throws Exception {

    http.authorizeRequests().antMatchers("/").permitAll();

    http.authorizeRequests()
            .antMatchers("/api/**")
            .permitAll()
            .and()
            .addFilter(new CookieFilter(...));

}

Spring security 报我的安全过滤器无法检查静态页面,所以进行了授权。

【问题讨论】:

    标签: spring-boot spring-security


    【解决方案1】:

    这将使 /api 下的任何内容都需要身份验证,并允许其他所有内容通过。

    http.authorizeRequests()
                .antMatchers("/api/**").authenticated()
                .anyRequest().permitAll()
                .and()
                .addFilter(new CookieFilter(...));
    

    【讨论】:

    • 好的,谢谢,我还在“WebSecurity 忽略列表”中添加了一个“/*”,以便将 rot 目录中的静态内容传递出去。
    猜你喜欢
    • 2018-05-25
    • 2017-11-09
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 2017-11-06
    • 1970-01-01
    • 2016-10-16
    相关资源
    最近更新 更多