簡體   English   中英

在 Spring Boot 中實現“注銷”功能

[英]Implement 'logout' functionality in Spring Boot

為了獲得基本的安全功能,我在 pom.xml 中添加了以下啟動包

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

並向 application.properties 添加以下兩個屬性:

security.user.name=訪客
security.user.password=tiger

現在,當我點擊主頁時,我得到了登錄框並且登錄按預期工作。

現在我想實現“注銷”功能。 當用戶點擊鏈接時,他/她會被注銷。 我注意到登錄沒有在我的瀏覽器中添加任何 cookie。 我假設 Spring Security 為用戶創建了一個 HttpSession 對象。 真的嗎? 我是否需要“無效”此會話並將用戶重定向到其他頁面? 在基於 Spring Boot 的應用程序中實現“注銷”功能的最佳方法是什么?

遲到總比不到好。 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");

最后一行是重要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM