【问题标题】:Migration to Spring Boot 2 from 1.5.7 - Request method POST not supported - csrf already disabled从 1.5.7 迁移到 Spring Boot 2 - 不支持请求方法 POST - csrf 已禁用
【发布时间】:2019-01-17 18:13:59
【问题描述】:

我们已将软件从 spring boot 1.5.7 迁移到 spring boot 2。 我们通过在 pom.xml 中包含 joinfaces-parent 来使用 JSF。

在启动时,一切正常,但登录调用不起作用:

Request method 'POST' not supported

这可能是 Spring Security 问题? CSRF 已被禁用。

这是我们的 SecurityConfig 文件:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    ...
    @Override
    protected void configure(HttpSecurity http) {
        try {

            http.csrf().disable().authorizeRequests()
                    .antMatchers("/javax.faces.resource/**", Page.LOGIN.getUrlForSecurityContext())
                    .permitAll()
                    .and()

                    ........

                    // *** login configuration
                    .formLogin()
                    .loginPage(Page.LOGIN.getUrlForSecurityContext()).permitAll()
                    .failureUrl(Page.LOGIN.getUrlForSecurityContext() + "?error=true")
                    .usernameParameter("username")
                    .passwordParameter("password")
                    .successHandler(authenticationSuccessHandler)
                    .and()

             ...........

            // @formatter:on
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    .......

}

登录请求没有到达我们的后端。 我发现这个错误是从 xhtml 调用的dispatcher.forward 函数生成的。这里的功能:

public void login() throws ServletException, IOException {
    final ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();

    final RequestDispatcher dispatcher = ((ServletRequest) context.getRequest()).getRequestDispatcher("/login");

    dispatcher.forward((ServletRequest) context.getRequest(), (ServletResponse) context.getResponse());

    FacesContext.getCurrentInstance().responseComplete();
}

出现错误消息时的更多日志:

[io.undertow.servlet] (default task-3) Initializing Spring FrameworkServlet 'dispatcherServlet'
16:02:20,926 INFO  [org.springframework.web.servlet.DispatcherServlet] (default task-3) FrameworkServlet 'dispatcherServlet': initialization started
16:02:20,938 INFO  [org.springframework.web.servlet.DispatcherServlet] (default task-3) FrameworkServlet 'dispatcherServlet': initialization completed in 12 ms
16:02:20,949 WARN  [org.springframework.web.servlet.PageNotFound] (default task-3) Request method 'POST' not supported
16:02:20,973 ERROR [org.springframework.boot.web.servlet.support.ErrorPageFilter] (default task-3) Cannot forward to error page for request [/login] as the response has already been committed. As a result, the response may have the wrong status code. If your application is running on WebSphere Application Server you may be able to resolve this problem by setting com.ibm.ws.webcontainer.invokeFlushAfterService to false

感谢您的建议!

【问题讨论】:

标签: spring-boot jsf spring-security undertow wildfly-12


【解决方案1】:

Spring Security 配置对我来说看起来不错。您的登录控制器有问题。我想你的 login 方法是为了响应来自客户端的 POST 请求而被调用的。然后它尝试转发这个 POST 以呈现登录页面,最后抛出异常。显然它应该是 GET 请求而不是 POST。

【讨论】:

    猜你喜欢
    • 2018-06-03
    • 2020-03-31
    • 2016-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多