在Spring Boot中实现“注销”function

为了使基本安全function正常工作,我将以下启动包添加到我的pom.xml中

 org.springframework.boot spring-boot-starter-security  

并在application.properties中添加了以下两个属性:

security.user.name =客人
security.user.password =虎

现在,当我点击我的主页时,我收到了登录框,登录按预期工作。

现在我想实现’注销’function。 基本上,当用户点击链接时,她会被注销。 我注意到登录不会在我的浏览器中添加任何cookie。 我假设Spring Security为用户创建了一个HttpSession对象。 真的吗? 我是否需要“使此会话无效”并将用户重定向到其他页面? 在基于Sprint Boot的应用程序中实现“注销”function的最佳方法是什么?

迟到总比没有好。 Spring Boot默认为您提供许多安全组件,包括CSRF保护。 其中一件事就是强制POST注销,请看这里: http : //docs.spring.io/spring-security/site/docs/3.2.4.RELEASE/reference/htmlsingle/#csrf-logout

这表明你可以使用以下内容来覆盖:

 http.authorizeRequests() .antMatchers("/admin/**").hasRole("ADMIN") .anyRequest().fullyAuthenticated() .and() .formLogin().loginPage("/login").failureUrl("/login?error").permitAll() .and() .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/login"); 

最后一行是重要的一行。