HTTP 405 方法不允许

HTTP 405 Method Not Allowed

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

错误通常在我成功登录后注销时出现,然后当我尝试重新登录时出现此错误 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;
}

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

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

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


    request.getSession().invalidate();

使会话无效,然后当我更改为基于 spring 安全性(/j_spring_security_logout/logout 的注销时,问题似乎是 solve.Maybe 我以编程方式注销无法正确注销和使会话无效。 感谢帮助。