【问题标题】:JWT Springboot app doesn't work properly when integrating with Angular 11与 Angular 11 集成时,JWT Springboot 应用程序无法正常工作
【发布时间】:2023-03-25 11:05:02
【问题描述】:

基于此topic 似乎我发现了问题,但我真的不知道如何解决它。

我对 WebSecurityConfigurerAdapter 进行了以下配置,因为我正在使用 JWT 安全流程,例如 page 问题出在配置方面,而不是构建本身。

配置是这样的:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.cors().and().csrf().disable()
            .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
            .authorizeRequests().antMatchers("/login").permitAll()
            .antMatchers("/register/**").permitAll()
            .antMatchers("/").permitAll()
            .anyRequest().authenticated();
    http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
}

不幸的是,它不允许我访问主页。如果我这样做.antMatchers("/**").permitAll() 它会正常工作。 当我访问 http://localhost:8080 我得到:

2021-04-26 23:09:42.517 ERROR 10160 --- [nio-8080-exec-4] c.a.d.security.jwt.AuthEntryPointJwt     : Unauthorized error: Full authentication is required to access this resource

这来自 unauthorizedHandler,但在配置中,.antMatchers("/").permitAll() 应该让我访问索引但不是。

如果我以.antMatchers("/**").permitAll() 为例,它可以让我访问该页面,但总的来说它会破坏安全流程。

【问题讨论】:

    标签: spring-boot spring-security jwt


    【解决方案1】:

    我假设您希望在 URI GET / 上返回一个页面而不是简单的 JSON 响应。

    .antMatchers("/").permitAll() 配置让公众可以访问确切的资源/,但它不允许您匿名下载其他资源,例如/global.css/foo.js 等。

    将其更改为.antMatchers("/*").permitAll(),这样您就可以公开访问/ 路径下的所有资源。

    【讨论】:

      【解决方案2】:

      你有没有尝试过,这对我有用,我可以通过这个配置访问主页。

      http.authorizeRequests().mvcMatchers("/").permitAll()
      

      还有一件事不要使用蚂蚁匹配器,它不像 mvcMatcher 那样安全和可自定义

      【讨论】:

      • 问题远不止于此。刚拿出来。它无法访问 UI 资源。比如 .js .css 文件等等。真是一团糟:D
      • @DragosRoban Nice :) 很高兴你解决了问题:)
      猜你喜欢
      • 1970-01-01
      • 2019-01-04
      • 1970-01-01
      • 2012-01-02
      • 1970-01-01
      • 1970-01-01
      • 2019-01-10
      • 1970-01-01
      相关资源
      最近更新 更多