【发布时间】:2019-03-22 22:09:50
【问题描述】:
以下问题:
我用 Spring 创建了一个简单的注销:
<form th:action="@{/logout-custom}" method="POST" name="logoutForm"
id="logout">
<input type="submit"/>
</form>
安全性:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/cont/**").access("hasRole('USER')")
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/login-success", true)
.failureUrl("/failLogin.html")
.permitAll()
.and()
.logout().logoutUrl("/logout").permitAll()
.and()
.csrf()
.disable();
}
控制器:
//Logout
@RequestMapping(value="/logout-custom", method = RequestMethod.POST)
public RedirectView logoutPage (HttpServletRequest request,
HttpServletResponse response) {
Authentication auth =
SecurityContextHolder.getContext().getAuthentication();
if (auth != null){
new SecurityContextLogoutHandler().logout(request, response, auth);
}
return new RedirectView("/loginForm.html");
}
// redirecting to login Page
依赖关系:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
当我单击注销按钮时,它显示“不支持请求方法'POST'”错误。
使用方法 GET 它只是添加了一个“?”在我的网址后面签名没有显示错误也没有重定向。 我从我的 html 中删除了所有内容,除了表单,因为似乎有些脚本阻止了整个事情(这是后来的问题)。 我还尝试删除控制器并仅使用 logout().logoutUrl().logoutSuccessUrl() 但这也不起作用。
【问题讨论】:
-
我知道。我认为这不必指向与控制器和注销表单相同的路径。可以?我只是试图将所有 3 点都指向注销自定义。同样的错误..
-
日志中的确切异常是什么,根本原因?此消息可能属于 stacktrace 的最后一个异常。
-
@benjaminc 堆栈跟踪是:请求方法 'POST' 不支持 [nio-8080-exec-7] .wsmsDefaultHandlerExceptionResolver :由处理程序执行引起的已解决异常:org.springframework.web.HttpRequestMethodNotSupportedException:请求不支持方法“POST”
-
你能发布你的 spring pom 依赖项吗?
-
@CrazySabbath 当然。