【问题标题】:Spring Security - Request Method 'POST' Not SupportedSpring Security - 不支持请求方法“POST”
【发布时间】:2021-06-24 01:15:10
【问题描述】:

从 Spring Security 4 升级到 5 后(仅涉及更新 POM 中的版本),身份验证现在被破坏。 Spring 安全版本为5.5.1。它曾经在 Spring Security 4 中运行良好。下面是 spring 配置。

@Override
  public void configure(WebSecurity web) {
    web.ignoring().antMatchers("/app*/**", "/resources/**");
  }
http.headers().frameOptions().disable();

http.formLogin()
        .loginPage("/login")
        .loginProcessingUrl("/j_spring_security_check")
        .usernameParameter("j_username").passwordParameter("j_password")
        .permitAll()
        .successHandler(authenticationSuccessHandler())
        .failureHandler(authenticationFailureHandler());

http.logout()
            .logoutUrl("/logout")
            .logoutSuccessUrl("/login")
            .deleteCookies("JSESSIONID" )
            .invalidateHttpSession(true)
            .permitAll();

http.csrf().disable();

http.requestCache().disable();

//Custom filters

Spring Security 调试日志 -

************************************************************

Request received for POST '/j_spring_security_check':

org.apache.catalina.connector.RequestFacade@fda4338

servletPath:/j_spring_security_check
pathInfo:null
headers: 
host: localhost:8080
connection: keep-alive
content-length: 77
cache-control: max-age=0
sec-ch-ua: " Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"
sec-ch-ua-mobile: ?0
upgrade-insecure-requests: 1
origin: http://localhost:8080
.
.
.


Security filter chain: [
  WebAsyncManagerIntegrationFilter
  SecurityContextPersistenceFilter
  HeaderWriterFilter
  LogoutFilter
  UsernamePasswordAuthenticationFilter
  ConcurrentSessionFilter
  SpnegoAuthenticationProcessingFilter
  BasicAuthenticationFilter
  LogbackMappedDiagnosticContextSecurityContextFilter
  RequestLoggingFilter
  SecurityContextHolderAwareRequestFilter
  AnonymousAuthenticationFilter
  SessionManagementFilter
  ExceptionTranslationFilter
  FilterSecurityInterceptor
]


************************************************************

2021-06-23 21:56:14,908 [http-nio-8080-exec-9] WARN  o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]

我不确定导致这种情况的版本之间发生了哪些变化。 post URL是一个spring security URL,不需要额外的配置。

【问题讨论】:

    标签: spring spring-security


    【解决方案1】:

    Spring Login 将 POST 以触发身份验证过程的默认 URL 是 /login,以前是 /j_spring_security_check

    如果你要使用默认值,你可以省略这两行

    .loginProcessingUrl("/login")
    .usernameParameter("username").passwordParameter("password")
    

    注意现在是usernamepassword

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-12
      • 2017-02-25
      • 2016-06-16
      • 2015-05-01
      相关资源
      最近更新 更多