【问题标题】:HTTP 405 Method Not Allowed不允许使用 HTTP 405 方法
【发布时间】:2016-03-08 22:17:17
【问题描述】:

我想知道为什么我的代码在我尝试登录时无法随机运行,但是当我关闭浏览器并重新编译代码时,它会正常运行。

这个错误通常是在我成功登录后注销时出现的,然后当我尝试重新登录时这个错误出现在 chrome 中:

Request method 'POST' not supported

当我尝试在 IE 中打开时,会出现这样的结果:

(HTTP 405 Method Not Allowed)

在控制台错误:

org.springframework.web.servlet.PageNotFound - Request method 'POST' not supported
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Handler execution resulted in exception: Request method 'POST' not supported

这是我的安全配置:

.formLogin()
            .loginProcessingUrl( "/login" )
            .loginPage( "/login.do" )
            .defaultSuccessUrl( "/",true )
            .failureUrl( "/login.do?error" )
            .usernameParameter( "username" )
            .passwordParameter( "password" )
            .permitAll()
            .and()

如果您需要任何代码,请告诉我。

添加了@RequestMapping:

private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

@RequestMapping(value = {"/login.do" }, method ={ RequestMethod.GET,RequestMethod.POST})
public ModelAndView login(
        @RequestParam(value = "error", required = false) String error,
        @RequestParam(value = "logout", required = false) String logout,
        @RequestParam(value = "timeout", required = false) String timeout,
        @RequestParam(value = "expired", required = false) String expired,
        @RequestParam(value = "invalid", required = false) String invalid, 
        HttpServletRequest request,
        HttpServletResponse response) {

    HttpSession session = request.getSession(false); 
    if (session != null) { 
        sessionClear(request, response);
    }

    Context context;
    try {
        context = new InitialContext();
        DataSource ds = (DataSource) context.lookup("jndi/CMS");
        Connection c = ds.getConnection();
        Statement stmt = c.createStatement();
    } catch (NamingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    ModelAndView model = new ModelAndView();

    if (error != null) {
        model.addObject("error", "Invalid username and password!");

    }
    if (logout != null) {
        model.addObject("msg", "You've been logged out successfully.");
    }
    if (expired != null) {
        model.addObject("exp", "You've been logged out due to expired.");

    }

    model.setViewName("log_in");
    logger.trace("this is trace");
    logger.debug("This is a debug");
    logger.info("this is info");
    logger.warn("This is warn");
    logger.error("This is error");



    return model;
}

更新: 我意识到如果我在新窗口中编译并登录,它总是可以正常工作,但是如果我重新编译代码并在新选项卡中执行,就会触发错误。

【问题讨论】:

  • 您看到的日志表明错误是由于您在 Spring MVC 控制器中的@RequestMapping 造成的(与 Spring Security 无关)。请提供所请求的 URL 以及该请求的完整日志。
  • 包含请求和完整日志的 URL 是什么意思?
  • 当您收到错误消息时,您发布到哪个 URL?您可以从控制台发布其余日志吗?
  • 错误总是在进入 /j_spring_security_check 或 /login 时触发。日志只显示 2 行 org.springframework.web.servlet.PageNotFound 和 org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver
  • 不知何故我已经解决了错误,但出现了新错误:Expected CSRF token not found. Has your session expired?

标签: java spring spring-security spring-java-config


【解决方案1】:

由于某种原因,问题已经解决。 我已经改回我的 Spring Security 以使用 /logout URL 使 Spring Security 中的会话无效。在此之前我使用:

SecurityContextHolder.getContext().setAuthentication(null);
    try {
        request.logout();
    } catch (ServletException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    request.getSession().invalidate();

要使会话无效,然后当我更改为基于 /j_spring_security_logout/logout 的 spring 安全性注销时,问题似乎解决了。也许我以编程方式注销无法正确注销并使会话无效。 感谢帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-20
    • 2016-08-05
    • 1970-01-01
    • 2020-02-13
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    相关资源
    最近更新 更多