以下问题:
我用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(),但这也没有用。
在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即可达到目的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句