Spring Security 配置 anyRequest().authenticated() 未按预期工作

2024-01-10

我对spring security配置的理解http.anyRequest().authenticated()是任何请求都必须经过身份验证,否则我的 Spring 应用程序将返回 401 响应。

不幸的是,我的 spring 应用程序不会以这种方式运行,而是允许未经身份验证的请求通过。

这是我的 Spring 安全配置:

@Configuration
@Order(1)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    private AuthenticationTokenFilter authenticationTokenFilter;

    @Autowired
    private TokenAuthenticationProvider tokenAuthenticationProvider;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .addFilterBefore(authenticationTokenFilter, BasicAuthenticationFilter.class)
                .antMatcher("/*")
                .authenticationProvider(tokenAuthenticationProvider)
                .authorizeRequests()
                .anyRequest().authenticated();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring()
                .antMatchers("/index.html")
                .antMatchers("/error")
                .antMatchers("/swagger-ui.html")
                .antMatchers("/swagger-resources");
    }
}

AuthenticationTokenFilter 从请求中获取 JWT 令牌,检查其有效性,从中创建身份验证,并在 SecurityContextHolder 中设置身份验证。如果未提供令牌,则 SecurityContextHolder.getContext().getAuthentication() 保持为 null。

我的控制器看起来像这样:

@RestController
@RequestMapping("/reports")
@Slf4j
public class ReportController {

    @RequestMapping()
    @ResponseBody
    public List<String> getIds() {
        log.info(SecurityContextHolder.getContext().getAuthentication());
        return Collections.emptyList();
    }

}

当在没有令牌的情况下对端点运行curl请求时,我只需得到一个有效的响应:

-> curl 'localhost:8080/reports/'
[]

调试时,我可以看到SecurityContextHolder.getContext().getAuthentication()一片空白。

有任何想法吗?


它实际上按预期工作,这取决于您编写配置的方式。

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .addFilterBefore(authenticationTokenFilter, BasicAuthenticationFilter.class)
            .antMatcher("/*")
                .authenticationProvider(tokenAuthenticationProvider)
                .authorizeRequests()
                .anyRequest().authenticated();
}

这是您自己的配置,由我进行了一些额外的缩进。当你写一个antMatcher这意味着接下来的一切antMatcher适用于其中写入的路径/表达式(请参阅 JavadocAntPathMatcher https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/util/AntPathMatcher.html).

现在正如你所写的/*这适用于/reports它不适用于/reports/(是的,最后/很重要!)。

你可能想写的是

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .addFilterBefore(authenticationTokenFilter, BasicAuthenticationFilter.class)
            .antMatcher("/**")
                .authenticationProvider(tokenAuthenticationProvider)
                .authorizeRequests()
                .anyRequest().authenticated();
}

注意/**代替/*。然而,这基本上与省略相同antMatcher并简单地写

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .addFilterBefore(authenticationTokenFilter, BasicAuthenticationFilter.class)
            .authenticationProvider(tokenAuthenticationProvider)
            .authorizeRequests()
            .anyRequest().authenticated();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring Security 配置 anyRequest().authenticated() 未按预期工作 的相关文章

随机推荐

  • 如何转换字符串中的 Word 智能引号和破折号?

    我有一个带有文本区域的表单 用户输入存储在数据库中的文本块 有时 用户会从 Word 粘贴包含智能引号或破折号的文本 这些字符在数据库中显示为 我应该对输入字符串调用什么函数将智能引号转换为常规引号 将破折号转换为常规破折号 我正在使用 P
  • 在JAVAFX中扩展CSS样式

    我正在尝试在 JAVAFX 应用程序中使用 CSS CSS 文件中有没有办法利用某种继承 例如 我有一种名为 redline 的样式 redline fx stroke red fx stroke width 5px 我可以创建第二种样式
  • 如何保持 Mercurial 图“平坦”

    我有一个 主 存储库 我克隆它并在克隆中进行一些更改 与此同时 main 中还有其他更改 因此我将它们拉出并将它们合并到我的克隆中 我在克隆中进行了更多更改 并合并了 主 中的任何其他新更改 这给了我这个图表 当我在克隆中完成工作时 我将其
  • 在close()之前使用flush()

    根据 java 文档 在任何 java io Streams 上调用 close 都会自动调用lush 但我在很多例子中看到 即使在生产代码中 开发人员也在 close 之前显式使用了flush 在什么情况下我们需要在 close 之前使用
  • 轮询 Jenkins 作业中的条件 svn 签出

    我试图根据条件阻止 svn 轮询 Jenkins 作业上的 svn 签出 前置步骤在结账后执行 该作业通过 svn 轮询触发 请指教 有一个插件允许在 SVN 结账之前执行一个步骤 https wiki jenkins ci org dis
  • 注释 @EnableSpringDataWebSupport 不适用于 WebMvcConfigurationSupport?

    我已经使用 WebMvcConfigurerAdapter 一段时间了 由于我无法使用 getInterceptors 方法获取所有已注册的拦截器 因此我已切换到 WebMvcConfigurationSupport 它有很多默认注册的 S
  • SSL 安全 SaaS 应用程序的 URL 设计

    我正在使用 ASP NET MVC 平台开发一个应用程序 它将作为一项服务通过网络公开 SaaS http en wikipedia org wiki Software as a Service模型 我正在尝试确定为每个用户帐户划分 URL
  • 将静态库添加到同一解决方案中的项目 (Visual Studio 2012)

    我正在尝试创建一个将在项目中使用的静态库 该库编译良好并为其创建 lib 文件 为了测试我的代码 我向同一解决方案添加了另一个项目 当我尝试构建测试项目时 出现以下错误 错误 LNK1120 4 个未解析的外部 错误 LNK2019 函数
  • Mysql 慢查询日志正在记录更快的查询

    我在数据库服务器上设置了mysql慢查询日志 并将长查询时间设置为5 刚刚检查了日志及其日志查询 只需几毫秒 有人知道为什么会这样吗 这是一些日志 最后一个查询并不是最优化的 它说它检查了 450000 行 所以我在日志中看到它不会感到惊讶
  • 为 Jboss 启用 gzip 压缩

    Jboss 5 1 0 的 gzip 压缩是如何启用的 在 tomcat http 连接器内对吗 我不记得这个文件存储在哪里 server xml 编辑 jboss server default deploy jbossweb sar se
  • Go Flush() 不起作用

    请检查一下这个要点并告诉我 出了什么问题 为什么我看不到我的消息 要旨 https gist github com cnaize 895f61b762a9f5ee074c https gist github com cnaize 895f6
  • 如何在 Android 中使用 Intent 拨打电话?

    我正在使用以下代码在 Android 中拨打电话 但它给了我安全异常 请帮助 posted by 111 333 222 4 String uri tel posted by trim Intent intent new Intent In
  • 如何在 SQL 中找到连续的活跃周?

    我想做的是找到某人在周日活跃的连续周数并为其分配一个值 他们每天必须参加至少 2 场比赛才能算作本周的活跃比赛 如果它们连续 2 周活跃 我想指定值为 100 连续 3 周指定为 200 连续 4 周指定为 300 并持续最多连续 9 周
  • JUunit 测试用例中内部图形尚未初始化

    我正在使用 JavaFx 制作一个轻量级绘画应用程序 我的一些问题LayerController类及其方法addLayer所以我认为编写一些 JUnit 测试用例来检查我的方法的正确性是一个好主意 简而言之 我正在画一个Canvas使用它的
  • 如何创建具有格式的字符串?

    我需要创建一个String具有可以转换的格式Int Int64 Double等输入String 使用 Objective C 我可以通过以下方式做到这一点 NSString str NSString stringWithFormat d f
  • MySQL 类似断言的约束

    我是MySQL新手 我刚刚发现它不支持断言 我得到了这张表 CREATE TABLE guest ssn varchar 16 NOT NULL name varchar 200 NOT NULL surname varchar 200 N
  • UIImage 上的圆角

    我正在尝试使用圆角在 iPhone 上绘制图像 就像联系人应用程序中的联系人图像一样 我的代码通常可以工作 但它偶尔会在 UIImage 绘图例程中崩溃EXEC BAD ACCESS KERN INVALID ADDRESS 我认为这可能与
  • 为什么新的 AKSequencer 不发出任何声音?

    我有一个相当复杂的应用程序 到目前为止一直在使用 AKAppleSequencer 但由于该定序器时不时出现一些奇怪的行为和错误 我一直希望迁移到较新的 AKSequencer 不幸的是 新的音序器似乎没有在 Playgrounds 或太多
  • On Key Down 限制用户输入一些特殊字符

    我想限制用户在工具栏搜索中不允许他 她使用一些特殊字符 例如 gt tblFundComp bind keydown function e if e keyCode gt 48 e keyCode lt 57 return false el
  • Spring Security 配置 anyRequest().authenticated() 未按预期工作

    我对spring security配置的理解http anyRequest authenticated 是任何请求都必须经过身份验证 否则我的 Spring 应用程序将返回 401 响应 不幸的是 我的 spring 应用程序不会以这种方式