gpt4 book ai didi

java - 注销与 Spring 和 Thymeleaf 的链接;发布错误

转载 作者:行者123 更新时间:2023-11-30 10:10:20 25 4
gpt4 key购买 nike

以下问题:

我用 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();
}

Controller :

//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 中删除了所有内容,除了表单,因为似乎有些脚本阻止了整个事情(这是后来的问题)。我还尝试删除 Controller 并仅使用 logout().logoutUrl().logoutSuccessUrl() 但这也不起作用。

最佳答案

SecurityContextLogoutHandler 是默认添加的,因此您无需为其实现自定义注销。

如果你想添加其他LogoutHandler,你可以在你的配置中这样做:

@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-custom")
.logoutSuccessUrl("/login")
.addLogoutHandler(new CustomLogoutHandler())
.permitAll()

.and()
.csrf()
.disable();
}

logoutSuccessUrl("/login") 将在成功注销后将用户重定向到登录页面。

更新:此外,删除 th: 并使用纯 HTML 即可。

关于java - 注销与 Spring 和 Thymeleaf 的链接;发布错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52868271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com