HTTP 405方法不允许

时间:2015-12-04 03:47:23

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

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

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

这是我的SecurityConfig:

.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;
}

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

1 个答案:

答案 0 :(得分:0)

由于某种原因,问题已经解决了。 我已经将我的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更改为注销时,问题似乎解决了。也许我以编程方式注销无法注销并使会话无效。 感谢帮助。