Java 不允许使用HTTP 405方法
我想知道为什么当我尝试登录时,我的代码不会随机工作,但是当我关闭浏览器并重新编译代码时,它会正常工作 该错误通常发生在我成功登录后注销时,然后当我尝试重新登录时,该错误在chrome中出现:Java 不允许使用HTTP 405方法,java,spring,spring-security,spring-java-config,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 meth
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安全性更改为使用/logout URL使spring安全性中的会话无效。在此之前,我使用:
SecurityContextHolder.getContext().setAuthentication(null);
try {
request.logout();
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.getSession().invalidate();
若要使会话无效,则当我更改为基于spring security的注销时,即/j_spring\u security\u注销
或/logout
,问题似乎已解决。可能我的程序注销无法正确注销和使会话无效。
谢谢你的帮助 您看到的日志表明错误是由Spring MVC控制器中的
@RequestMapping
引起的(与Spring安全性无关)。请包括所请求的URL和该请求的完整日志。包含所请求的URL和完整日志是什么意思?出现错误时,您将发布到哪个URL?您可以从控制台发布其余日志吗?当转到/j_spring_security_check或/login时,总是会触发该错误。日志仅显示两行,org.springframework.web.servlet.PageNotFound和org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolversome我已经解决了错误,但出现了新错误:未找到预期的CSRF令牌。您的会话是否已过期?