带有弹簧和网络通量的身份验证

发布于 2025-02-09 20:57:31 字数 1778 浏览 0 评论 0原文

我有一个有关春季和网络通量的问题。 我有一个带有春季安全性的春季项目,将MVC作为依赖项。 此应用程序接受请求并使用会话cookie检查身份验证。 对于以“/api/”开头的所有请求,失败的身份验证会导致401响应,因此可以被前端拦截。 对于所有与“/api/**”不同的请求,失败的身份验证会导致服务器返回登录页面,以便用户可以登录。

这是SecuritConfig类:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

 @Override
 protected void configure(HttpSecurity http) throws Exception {
     http
     .authorizeRequests()
     .anyRequest()
     .authenticated()
     .and()
     .formLogin()
     .and()
     .csrf()
     .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
     .and()
     .exceptionHandling()
     .defaultAuthenticationEntryPointFor(new
      HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED),
                     new AntPathRequestMatcher("/api/**"))
     .and()
     .cors();
 }

}

现在,我正在尝试使用Web Flux实现同一件事。 在旧类中拥有的所有配置

defaultAuthenticationEntryPointFor(new
HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED),
                     new AntPathRequestMatcher("/api/**"))

使用

@Configuration
@EnableWebFluxSecurity
public class SecurityConfig {

 @Bean
 public SecurityWebFilterChain filterChain(ServerHttpSecurity http)  {
     return http
             .authorizeExchange()
             .pathMatchers("/login/**")
             .permitAll()
             .anyExchange()
             .authenticated()
             .and()
             .formLogin()
             .and()
             .csrf()
             .disable()
             .exceptionHandling()
             .authenticationEntryPoint(new
HttpStatusServerEntryPoint(HttpStatus.UNAUTHORIZED))
             .and()
             .build();
 }

SecurityConfig是不同的,我几乎可以设置我

Web磁通, 失败身份验证。 是否有人知道如何使用Web通量实现相同的行为? 谢谢

I have a question concerning spring and web flux.
I have a spring project with spring security and MVC as dependencies.
This application accepts requests and check authentication using the session cookie.
For all the requests starting with "/api/" a failed authentication results in a 401 response, so that can be intercepted by the frontend as such.
For all the requests different from "/api/**" a failed authentication results in the server returning a login page so that the user can login.

This is the SecuritConfig class:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

 @Override
 protected void configure(HttpSecurity http) throws Exception {
     http
     .authorizeRequests()
     .anyRequest()
     .authenticated()
     .and()
     .formLogin()
     .and()
     .csrf()
     .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
     .and()
     .exceptionHandling()
     .defaultAuthenticationEntryPointFor(new
      HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED),
                     new AntPathRequestMatcher("/api/**"))
     .and()
     .cors();
 }

}

Now, I am trying to achieve the same thing using web flux. With web flux the SecurityConfig is different, I can setup almost all the configs that I have in the old class but there is no equivalent for:

defaultAuthenticationEntryPointFor(new
HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED),
                     new AntPathRequestMatcher("/api/**"))

My new security config look like:

@Configuration
@EnableWebFluxSecurity
public class SecurityConfig {

 @Bean
 public SecurityWebFilterChain filterChain(ServerHttpSecurity http)  {
     return http
             .authorizeExchange()
             .pathMatchers("/login/**")
             .permitAll()
             .anyExchange()
             .authenticated()
             .and()
             .formLogin()
             .and()
             .csrf()
             .disable()
             .exceptionHandling()
             .authenticationEntryPoint(new
HttpStatusServerEntryPoint(HttpStatus.UNAUTHORIZED))
             .and()
             .build();
 }

}

But in this case I only get 401 for all the requests that fail authentication.
Does anybody know how to achieve the same behavior with web flux?
Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文