【发布时间】:2018-07-31 17:10:34
【问题描述】:
我有使用 Spring Security Basic Authentication 和 thymeleaf 的登录和注销表单。
我的问题是我的注销链接显示为按钮。但我想将它包含在<a href=""> 标记中。但是当我在<a href=""> 标签中包含我的注销链接时,它不起作用。这是我的代码。
注销控制器 - Spring Boot
@RequestMapping(value = {"/logout"}, method = RequestMethod.POST)
public String logoutDo(HttpServletRequest request,HttpServletResponse response){
HttpSession session= request.getSession(false);
SecurityContextHolder.clearContext();
session= request.getSession(false);
if(session != null) {
session.invalidate();
}
for(Cookie cookie : request.getCookies()) {
cookie.setMaxAge(0);
}
return "redirect:/login?logout";
}
注销表单 - 前端
<form th:action="@{/logout}" method="POST">
<input type="submit" value="Logout"/>
</form>
安全配置
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.requestMatchers()
.antMatchers("/login", "/", "/customer/**", "/registration", "/admin/**")
.and()
.authorizeRequests()
.antMatchers("/admin/**").hasAuthority("ADMIN").antMatchers("/customer/**").hasAuthority("CUSTOMER")
.antMatchers("/login", "/registration", "/").permitAll()
.anyRequest().authenticated()
.and()
.httpBasic().and().csrf().disable()
.formLogin() //login configuration
.loginPage("/login")
.loginProcessingUrl("/login")
.usernameParameter("email")
.passwordParameter("password")
.defaultSuccessUrl("/customer/home")
.and().logout() //logout configuration
.logoutUrl("/logout")
.logoutSuccessUrl("/login")
.and().exceptionHandling() //exception handling configuration
.accessDeniedPage("/error");
}
【问题讨论】:
标签: spring-boot spring-mvc thymeleaf