SpringSecurity注銷設(shè)置的方法
Spring Security中也提供了默認(rèn)的注銷配置,在開發(fā)時也可以按照自己需求對注銷進(jìn)行個性化定制
開啟注銷 默認(rèn)開啟
package com.example.config; import com.example.handler.MyAuthenticationFailureHandler; import com.example.handler.MyAuthenticationSuccessHandler; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { ? ? @Override ? ? public void configure(HttpSecurity http) throws Exception { ? ? ? ? //【注意事項】放行資源要放在前面,認(rèn)證的放在后面 ? ? ? ? http.authorizeRequests() ? ? ? ? ? ? ? ? .mvcMatchers("/index").permitAll() //代表放行index的所有請求 ? ? ? ? ? ? ? ? .mvcMatchers("/loginHtml").permitAll() //放行l(wèi)oginHtml請求 ? ? ? ? ? ? ? ? .anyRequest().authenticated()//代表其他請求需要認(rèn)證 ? ? ? ? ? ? ? ? .and() ? ? ? ? ? ? ? ? .formLogin()//表示其他需要認(rèn)證的請求通過表單認(rèn)證 ? ? ? ? ? ? ? ? //loginPage 一旦你自定義了這個登錄頁面,那你必須要明確告訴SpringSecurity日后哪個url處理你的登錄請求 ? ? ? ? ? ? ? ? .loginPage("/loginHtml")//用來指定自定義登錄界面,不使用SpringSecurity默認(rèn)登錄界面 ?注意:一旦自定義登錄頁面,必須指定登錄url ? ? ? ? ? ? ? ? //loginProcessingUrl ?這個doLogin請求本身是沒有的,因為我們只需要明確告訴SpringSecurity,日后只要前端發(fā)起的是一個doLogin這樣的請求, ? ? ? ? ? ? ? ? //那SpringSecurity應(yīng)該把你username和password給捕獲到 ? ? ? ? ? ? ? ? .loginProcessingUrl("/doLogin")//指定處理登錄的請求url ? ? ? ? ? ? ? ? .usernameParameter("uname") //指定登錄界面用戶名文本框的name值,如果沒有指定,默認(rèn)屬性名必須為username ? ? ? ? ? ? ? ? .passwordParameter("passwd")//指定登錄界面密碼密碼框的name值,如果沒有指定,默認(rèn)屬性名必須為password // ? ? ? ? ? ? ? ?.successForwardUrl("/index")//認(rèn)證成功 forward 跳轉(zhuǎn)路徑,forward代表服務(wù)器內(nèi)部的跳轉(zhuǎn)之后,地址欄不變 始終在認(rèn)證成功之后跳轉(zhuǎn)到指定請求 // ? ? ? ? ? ? ? ?.defaultSuccessUrl("/index")//認(rèn)證成功 之后跳轉(zhuǎn),重定向 redirect 跳轉(zhuǎn)后,地址會發(fā)生改變 ?根據(jù)上一保存請求進(jìn)行成功跳轉(zhuǎn) ? ? ? ? ? ? ? ? .successHandler(new MyAuthenticationSuccessHandler()) //認(rèn)證成功時處理 ?前后端分離解決方案 // ? ? ? ? ? ? ? ?.failureForwardUrl("/loginHtml")//認(rèn)證失敗之后 forward 跳轉(zhuǎn) // ? ? ? ? ? ? ? ?.failureUrl("/login.html")//認(rèn)證失敗之后 ?redirect 跳轉(zhuǎn) ? ? ? ? ? ? ? ? .failureHandler(new MyAuthenticationFailureHandler())//用來自定義認(rèn)證失敗之后處理 ?前后端分離解決方案 ? ? ? ? ? ? ? ? .and() ? ? ? ? ? ? ? ? .logout() ? ? ? ? ? ? ? ? .logoutUrl("/logout") //指定注銷登錄url ?默認(rèn)請求方式必須:GET ? ? ? ? ? ? ? ? .invalidateHttpSession(true) //會話失效 默認(rèn)值為true,可不寫 ? ? ? ? ? ? ? ? .clearAuthentication(true) //清除認(rèn)證標(biāo)記 默認(rèn)值為true,可不寫 ? ? ? ? ? ? ? ? .logoutSuccessUrl("/loginHtml") //注銷成功之后跳轉(zhuǎn)頁面 ? ? ? ? ? ? ? ? .and() ? ? ? ? ? ? ? ? .csrf().disable(); //禁止csrf 跨站請求保護(hù) ? ? } }
- 通過logout()方法開啟注銷配置
- logoutUrl指定退出登錄請求地址,默認(rèn)是GET請求,路徑為/logout
- invalidateHttpSession 退出時是否是session失效,默認(rèn)值為true
- clearAuthentication 退出時是否清除認(rèn)證信息,默認(rèn)值為true
- logoutSuccessUrl 退出登錄時跳轉(zhuǎn)地址
測試
先訪問http://localhost:8080/hello,會自動跳轉(zhuǎn)到http://localhost:8080/loginHtml登錄界面,輸入用戶名和密碼,地址欄會變化為:http://localhost:8080/doLogin,然后重新訪問http://localhost:8080/hello,此時可以看到hello的數(shù)據(jù),表示登錄認(rèn)證
成功然后訪問http://localhost:8080/logout,會自動跳轉(zhuǎn)到http://localhost:8080/loginHtml登錄頁面,然后在訪問http://localhost:8080/hello,發(fā)現(xiàn)會跳轉(zhuǎn)到http://localhost:8080/loginHtml登錄界面,表示后端認(rèn)定你未登錄了,需要你登錄認(rèn)證
配置多個注銷登錄請求
如果項目中也有需要,開發(fā)者還可以配置多個注銷登錄的請求,同時還可以指定請求的方法
新增退出controller
package com.example.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class LogoutController { ? ? @RequestMapping("/logoutHtml") ? ? public String logout(){ ? ? ? ? return "logout"; ? ? } }
新增退出界面
logout.html
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org/" lang="en"> <head> ? ? <meta charset="UTF-8"> ? ? <title>注銷</title> </head> <body> ? ? <h1>注銷</h1> ? ? <form th:action="@{/bb}" method="post"> ? ? ? ? <input type="submit" value="注銷"> ? ? </form> </body> </html>
修改配置信息
package com.example.config; import com.example.handler.MyAuthenticationFailureHandler; import com.example.handler.MyAuthenticationSuccessHandler; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.web.util.matcher.AntPathRequestMatcher; import org.springframework.security.web.util.matcher.OrRequestMatcher; @Configuration public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { ? ? @Override ? ? public void configure(HttpSecurity http) throws Exception { ? ? ? ? //【注意事項】放行資源要放在前面,認(rèn)證的放在后面 ? ? ? ? http.authorizeRequests() ? ? ? ? ? ? ? ? .mvcMatchers("/index").permitAll() //代表放行index的所有請求 ? ? ? ? ? ? ? ? .mvcMatchers("/loginHtml").permitAll() //放行l(wèi)oginHtml請求 ? ? ? ? ? ? ? ? .anyRequest().authenticated()//代表其他請求需要認(rèn)證 ? ? ? ? ? ? ? ? .and() ? ? ? ? ? ? ? ? .formLogin()//表示其他需要認(rèn)證的請求通過表單認(rèn)證 ? ? ? ? ? ? ? ? //loginPage 一旦你自定義了這個登錄頁面,那你必須要明確告訴SpringSecurity日后哪個url處理你的登錄請求 ? ? ? ? ? ? ? ? .loginPage("/loginHtml")//用來指定自定義登錄界面,不使用SpringSecurity默認(rèn)登錄界面 ?注意:一旦自定義登錄頁面,必須指定登錄url ? ? ? ? ? ? ? ? //loginProcessingUrl ?這個doLogin請求本身是沒有的,因為我們只需要明確告訴SpringSecurity,日后只要前端發(fā)起的是一個doLogin這樣的請求, ? ? ? ? ? ? ? ? //那SpringSecurity應(yīng)該把你username和password給捕獲到 ? ? ? ? ? ? ? ? .loginProcessingUrl("/doLogin")//指定處理登錄的請求url ? ? ? ? ? ? ? ? .usernameParameter("uname") //指定登錄界面用戶名文本框的name值,如果沒有指定,默認(rèn)屬性名必須為username ? ? ? ? ? ? ? ? .passwordParameter("passwd")//指定登錄界面密碼密碼框的name值,如果沒有指定,默認(rèn)屬性名必須為password // ? ? ? ? ? ? ? ?.successForwardUrl("/index")//認(rèn)證成功 forward 跳轉(zhuǎn)路徑,forward代表服務(wù)器內(nèi)部的跳轉(zhuǎn)之后,地址欄不變 始終在認(rèn)證成功之后跳轉(zhuǎn)到指定請求 // ? ? ? ? ? ? ? ?.defaultSuccessUrl("/index")//認(rèn)證成功 之后跳轉(zhuǎn),重定向 redirect 跳轉(zhuǎn)后,地址會發(fā)生改變 ?根據(jù)上一保存請求進(jìn)行成功跳轉(zhuǎn) ? ? ? ? ? ? ? ? .successHandler(new MyAuthenticationSuccessHandler()) //認(rèn)證成功時處理 ?前后端分離解決方案 // ? ? ? ? ? ? ? ?.failureForwardUrl("/loginHtml")//認(rèn)證失敗之后 forward 跳轉(zhuǎn) // ? ? ? ? ? ? ? ?.failureUrl("/login.html")//認(rèn)證失敗之后 ?redirect 跳轉(zhuǎn) ? ? ? ? ? ? ? ? .failureHandler(new MyAuthenticationFailureHandler())//用來自定義認(rèn)證失敗之后處理 ?前后端分離解決方案 ? ? ? ? ? ? ? ? .and() ? ? ? ? ? ? ? ? .logout() // ? ? ? ? ? ? ? ?.logoutUrl("/logout") //指定注銷登錄url ?默認(rèn)請求方式必須:GET ? ? ? ? ? ? ? ? .logoutRequestMatcher(new OrRequestMatcher( ? ? ? ? ? ? ? ? ? ? ? ? new AntPathRequestMatcher("/aa","GET"), ? ? ? ? ? ? ? ? ? ? ? ? new AntPathRequestMatcher("/bb","POST") ? ? ? ? ? ? ? ? ? ? ? ? ) ? ? ? ? ? ? ? ? ) ? ? ? ? ? ? ? ? .invalidateHttpSession(true) //會話失效 默認(rèn)值為true,可不寫 ? ? ? ? ? ? ? ? .clearAuthentication(true) //清除認(rèn)證標(biāo)記 默認(rèn)值為true,可不寫 ? ? ? ? ? ? ? ? .logoutSuccessUrl("/loginHtml") //注銷成功之后跳轉(zhuǎn)頁面 ? ? ? ? ? ? ? ? .and() ? ? ? ? ? ? ? ? .csrf().disable(); //禁止csrf 跨站請求保護(hù) ? ? } }
測試
GET /aa 跟上面測試方法一樣
POST /bb
先訪問http://localhost:8080/hello,會自動跳轉(zhuǎn)到http://localhost:8080/loginHtml登錄界面,輸入用戶名和密碼,地址欄會變化為:http://localhost:8080/doLogin,然后重新訪問http://localhost:8080/hello,此時可以看到hello的數(shù)據(jù),表示登錄認(rèn)證成功
然后訪問http://localhost:8080/logoutHtml,會跳出注銷頁面,點擊“注銷”按鈕,會返回到http://localhost:8080/loginHtml登錄界面,再重新訪問http://localhost:8080/hello,發(fā)現(xiàn)會跳轉(zhuǎn)到http://localhost:8080/loginHtml登錄界面,表示注銷成功,需要重新登錄。
前后端分離注銷配置
如果是前后端分離開發(fā),注銷成功之后就不需要頁面跳轉(zhuǎn)了,只需要將注銷成功的信息返回前端即可,此時我們可以通過自定義LogoutSuccessHandler實現(xiàn)來返回內(nèi)容注銷之后信息
添加handler
package com.example.handler; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.security.core.Authentication; import org.springframework.security.web.authentication.logout.LogoutSuccessHandler; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.HashMap; import java.util.Map; /** ?* 自定義注銷成功之后處理 ?*/ public class MyLogoutSuccessHandler implements LogoutSuccessHandler { ? ? @Override ? ? public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { ? ? ? ? Map<String,Object> result = new HashMap<>(); ? ? ? ? result.put("msg","注銷成功,當(dāng)前認(rèn)證對象為:"+authentication); ? ? ? ? result.put("status",200); ? ? ? ? result.put("authentication",authentication); ? ? ? ? response.setContentType("application/json;charset=UTF-8"); ? ? ? ? String s = new ObjectMapper().writeValueAsString(result); ? ? ? ? response.getWriter().println(s); ? ? } }
修改配置信息
logoutSuccessHandler
package com.example.config; import com.example.handler.MyAuthenticationFailureHandler; import com.example.handler.MyAuthenticationSuccessHandler; import com.example.handler.MyLogoutSuccessHandler; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.web.util.matcher.AntPathRequestMatcher; import org.springframework.security.web.util.matcher.OrRequestMatcher; @Configuration public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { ? ? @Override ? ? public void configure(HttpSecurity http) throws Exception { ? ? ? ? //【注意事項】放行資源要放在前面,認(rèn)證的放在后面 ? ? ? ? http.authorizeRequests() ? ? ? ? ? ? ? ? .mvcMatchers("/index").permitAll() //代表放行index的所有請求 ? ? ? ? ? ? ? ? .mvcMatchers("/loginHtml").permitAll() //放行l(wèi)oginHtml請求 ? ? ? ? ? ? ? ? .anyRequest().authenticated()//代表其他請求需要認(rèn)證 ? ? ? ? ? ? ? ? .and() ? ? ? ? ? ? ? ? .formLogin()//表示其他需要認(rèn)證的請求通過表單認(rèn)證 ? ? ? ? ? ? ? ? //loginPage 一旦你自定義了這個登錄頁面,那你必須要明確告訴SpringSecurity日后哪個url處理你的登錄請求 ? ? ? ? ? ? ? ? .loginPage("/loginHtml")//用來指定自定義登錄界面,不使用SpringSecurity默認(rèn)登錄界面 ?注意:一旦自定義登錄頁面,必須指定登錄url ? ? ? ? ? ? ? ? //loginProcessingUrl ?這個doLogin請求本身是沒有的,因為我們只需要明確告訴SpringSecurity,日后只要前端發(fā)起的是一個doLogin這樣的請求, ? ? ? ? ? ? ? ? //那SpringSecurity應(yīng)該把你username和password給捕獲到 ? ? ? ? ? ? ? ? .loginProcessingUrl("/doLogin")//指定處理登錄的請求url ? ? ? ? ? ? ? ? .usernameParameter("uname") //指定登錄界面用戶名文本框的name值,如果沒有指定,默認(rèn)屬性名必須為username ? ? ? ? ? ? ? ? .passwordParameter("passwd")//指定登錄界面密碼密碼框的name值,如果沒有指定,默認(rèn)屬性名必須為password // ? ? ? ? ? ? ? ?.successForwardUrl("/index")//認(rèn)證成功 forward 跳轉(zhuǎn)路徑,forward代表服務(wù)器內(nèi)部的跳轉(zhuǎn)之后,地址欄不變 始終在認(rèn)證成功之后跳轉(zhuǎn)到指定請求 // ? ? ? ? ? ? ? ?.defaultSuccessUrl("/index")//認(rèn)證成功 之后跳轉(zhuǎn),重定向 redirect 跳轉(zhuǎn)后,地址會發(fā)生改變 ?根據(jù)上一保存請求進(jìn)行成功跳轉(zhuǎn) ? ? ? ? ? ? ? ? .successHandler(new MyAuthenticationSuccessHandler()) //認(rèn)證成功時處理 ?前后端分離解決方案 // ? ? ? ? ? ? ? ?.failureForwardUrl("/loginHtml")//認(rèn)證失敗之后 forward 跳轉(zhuǎn) // ? ? ? ? ? ? ? ?.failureUrl("/login.html")//認(rèn)證失敗之后 ?redirect 跳轉(zhuǎn) ? ? ? ? ? ? ? ? .failureHandler(new MyAuthenticationFailureHandler())//用來自定義認(rèn)證失敗之后處理 ?前后端分離解決方案 ? ? ? ? ? ? ? ? .and() ? ? ? ? ? ? ? ? .logout() // ? ? ? ? ? ? ? ?.logoutUrl("/logout") //指定注銷登錄url ?默認(rèn)請求方式必須:GET ? ? ? ? ? ? ? ? .logoutRequestMatcher(new OrRequestMatcher( ? ? ? ? ? ? ? ? ? ? ? ? new AntPathRequestMatcher("/aa","GET"), ? ? ? ? ? ? ? ? ? ? ? ? new AntPathRequestMatcher("/bb","POST") ? ? ? ? ? ? ? ? ? ? ? ? ) ? ? ? ? ? ? ? ? ) ? ? ? ? ? ? ? ? .invalidateHttpSession(true) //會話失效 默認(rèn)值為true,可不寫 ? ? ? ? ? ? ? ? .clearAuthentication(true) //清除認(rèn)證標(biāo)記 默認(rèn)值為true,可不寫 // ? ? ? ? ? ? ? ?.logoutSuccessUrl("/loginHtml") //注銷成功之后跳轉(zhuǎn)頁面 ? ? ? ? ? ? ? ? .logoutSuccessHandler(new MyLogoutSuccessHandler()) //注銷成功之后處理 ?前后端分離解決方案 ? ? ? ? ? ? ? ? .and() ? ? ? ? ? ? ? ? .csrf().disable(); //禁止csrf 跨站請求保護(hù) ? ? } }
測試
跟第一個測試方法是一樣的
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于Spring MVC框架中攔截器Interceptor的使用解讀
這篇文章主要介紹了關(guān)于Spring MVC框架中攔截器Interceptor的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07java web監(jiān)聽器統(tǒng)計在線用戶及人數(shù)
本文主要介紹了java web監(jiān)聽器統(tǒng)計在線用戶及人數(shù)的方法解析。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04詳解SpringBoot開發(fā)案例之整合Dubbo分布式服務(wù)
這篇文章主要介紹了詳解SpringBoot開發(fā)案例之整合Dubbo分布式服務(wù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10@Scheduled定時器原理及@RefreshScope相互影響
這篇文章主要為大家介紹了@Scheduled定時器原理及@RefreshScope相互影響詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07SpringBoot2+Netty+WebSocket(netty實現(xiàn)websocket支持URL參數(shù))問題記錄
Netty?是一個利用?Java?的高級網(wǎng)絡(luò)的能力,隱藏其背后的復(fù)雜性而提供一個易于使用的?API?的客戶端/服務(wù)器框架,這篇文章主要介紹了SpringBoot2+Netty+WebSocket(netty實現(xiàn)websocket,支持URL參數(shù)),需要的朋友可以參考下2023-12-12Java 實現(xiàn)二叉搜索樹的查找、插入、刪除、遍歷
本文主要介紹了Java實現(xiàn)二叉搜索樹的查找、插入、刪除、遍歷等內(nèi)容。具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02JavaWeb詳細(xì)講述Cookie和Session的概念
web開發(fā)階段我們主要是瀏覽器和服務(wù)器之間來進(jìn)行交互。瀏覽器和服務(wù)器之間的交互就像人和人之間進(jìn)行交流一樣,但是對于機器來說,在一次請求之間只是會攜帶著本次請求的數(shù)據(jù)的,但是可能多次請求之間是會有聯(lián)系的,所以提供了會話機制2022-06-06