SpringSecurity注銷設(shè)置的方法
Spring Security中也提供了默認的注銷配置,在開發(fā)時也可以按照自己需求對注銷進行個性化定制
開啟注銷 默認開啟
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 {
? ? ? ? //【注意事項】放行資源要放在前面,認證的放在后面
? ? ? ? http.authorizeRequests()
? ? ? ? ? ? ? ? .mvcMatchers("/index").permitAll() //代表放行index的所有請求
? ? ? ? ? ? ? ? .mvcMatchers("/loginHtml").permitAll() //放行l(wèi)oginHtml請求
? ? ? ? ? ? ? ? .anyRequest().authenticated()//代表其他請求需要認證
? ? ? ? ? ? ? ? .and()
? ? ? ? ? ? ? ? .formLogin()//表示其他需要認證的請求通過表單認證
? ? ? ? ? ? ? ? //loginPage 一旦你自定義了這個登錄頁面,那你必須要明確告訴SpringSecurity日后哪個url處理你的登錄請求
? ? ? ? ? ? ? ? .loginPage("/loginHtml")//用來指定自定義登錄界面,不使用SpringSecurity默認登錄界面 ?注意:一旦自定義登錄頁面,必須指定登錄url
? ? ? ? ? ? ? ? //loginProcessingUrl ?這個doLogin請求本身是沒有的,因為我們只需要明確告訴SpringSecurity,日后只要前端發(fā)起的是一個doLogin這樣的請求,
? ? ? ? ? ? ? ? //那SpringSecurity應(yīng)該把你username和password給捕獲到
? ? ? ? ? ? ? ? .loginProcessingUrl("/doLogin")//指定處理登錄的請求url
? ? ? ? ? ? ? ? .usernameParameter("uname") //指定登錄界面用戶名文本框的name值,如果沒有指定,默認屬性名必須為username
? ? ? ? ? ? ? ? .passwordParameter("passwd")//指定登錄界面密碼密碼框的name值,如果沒有指定,默認屬性名必須為password
// ? ? ? ? ? ? ? ?.successForwardUrl("/index")//認證成功 forward 跳轉(zhuǎn)路徑,forward代表服務(wù)器內(nèi)部的跳轉(zhuǎn)之后,地址欄不變 始終在認證成功之后跳轉(zhuǎn)到指定請求
// ? ? ? ? ? ? ? ?.defaultSuccessUrl("/index")//認證成功 之后跳轉(zhuǎn),重定向 redirect 跳轉(zhuǎn)后,地址會發(fā)生改變 ?根據(jù)上一保存請求進行成功跳轉(zhuǎn)
? ? ? ? ? ? ? ? .successHandler(new MyAuthenticationSuccessHandler()) //認證成功時處理 ?前后端分離解決方案
// ? ? ? ? ? ? ? ?.failureForwardUrl("/loginHtml")//認證失敗之后 forward 跳轉(zhuǎn)
// ? ? ? ? ? ? ? ?.failureUrl("/login.html")//認證失敗之后 ?redirect 跳轉(zhuǎn)
? ? ? ? ? ? ? ? .failureHandler(new MyAuthenticationFailureHandler())//用來自定義認證失敗之后處理 ?前后端分離解決方案
? ? ? ? ? ? ? ? .and()
? ? ? ? ? ? ? ? .logout()
? ? ? ? ? ? ? ? .logoutUrl("/logout") //指定注銷登錄url ?默認請求方式必須:GET
? ? ? ? ? ? ? ? .invalidateHttpSession(true) //會話失效 默認值為true,可不寫
? ? ? ? ? ? ? ? .clearAuthentication(true) //清除認證標記 默認值為true,可不寫
? ? ? ? ? ? ? ? .logoutSuccessUrl("/loginHtml") //注銷成功之后跳轉(zhuǎn)頁面
? ? ? ? ? ? ? ? .and()
? ? ? ? ? ? ? ? .csrf().disable(); //禁止csrf 跨站請求保護
? ? }
}- 通過logout()方法開啟注銷配置
- logoutUrl指定退出登錄請求地址,默認是GET請求,路徑為/logout
- invalidateHttpSession 退出時是否是session失效,默認值為true
- clearAuthentication 退出時是否清除認證信息,默認值為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ù),表示登錄認證成功然后訪問http://localhost:8080/logout,會自動跳轉(zhuǎn)到http://localhost:8080/loginHtml登錄頁面,然后在訪問http://localhost:8080/hello,發(fā)現(xiàn)會跳轉(zhuǎn)到http://localhost:8080/loginHtml登錄界面,表示后端認定你未登錄了,需要你登錄認證
配置多個注銷登錄請求
如果項目中也有需要,開發(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 {
? ? ? ? //【注意事項】放行資源要放在前面,認證的放在后面
? ? ? ? http.authorizeRequests()
? ? ? ? ? ? ? ? .mvcMatchers("/index").permitAll() //代表放行index的所有請求
? ? ? ? ? ? ? ? .mvcMatchers("/loginHtml").permitAll() //放行l(wèi)oginHtml請求
? ? ? ? ? ? ? ? .anyRequest().authenticated()//代表其他請求需要認證
? ? ? ? ? ? ? ? .and()
? ? ? ? ? ? ? ? .formLogin()//表示其他需要認證的請求通過表單認證
? ? ? ? ? ? ? ? //loginPage 一旦你自定義了這個登錄頁面,那你必須要明確告訴SpringSecurity日后哪個url處理你的登錄請求
? ? ? ? ? ? ? ? .loginPage("/loginHtml")//用來指定自定義登錄界面,不使用SpringSecurity默認登錄界面 ?注意:一旦自定義登錄頁面,必須指定登錄url
? ? ? ? ? ? ? ? //loginProcessingUrl ?這個doLogin請求本身是沒有的,因為我們只需要明確告訴SpringSecurity,日后只要前端發(fā)起的是一個doLogin這樣的請求,
? ? ? ? ? ? ? ? //那SpringSecurity應(yīng)該把你username和password給捕獲到
? ? ? ? ? ? ? ? .loginProcessingUrl("/doLogin")//指定處理登錄的請求url
? ? ? ? ? ? ? ? .usernameParameter("uname") //指定登錄界面用戶名文本框的name值,如果沒有指定,默認屬性名必須為username
? ? ? ? ? ? ? ? .passwordParameter("passwd")//指定登錄界面密碼密碼框的name值,如果沒有指定,默認屬性名必須為password
// ? ? ? ? ? ? ? ?.successForwardUrl("/index")//認證成功 forward 跳轉(zhuǎn)路徑,forward代表服務(wù)器內(nèi)部的跳轉(zhuǎn)之后,地址欄不變 始終在認證成功之后跳轉(zhuǎn)到指定請求
// ? ? ? ? ? ? ? ?.defaultSuccessUrl("/index")//認證成功 之后跳轉(zhuǎn),重定向 redirect 跳轉(zhuǎn)后,地址會發(fā)生改變 ?根據(jù)上一保存請求進行成功跳轉(zhuǎn)
? ? ? ? ? ? ? ? .successHandler(new MyAuthenticationSuccessHandler()) //認證成功時處理 ?前后端分離解決方案
// ? ? ? ? ? ? ? ?.failureForwardUrl("/loginHtml")//認證失敗之后 forward 跳轉(zhuǎn)
// ? ? ? ? ? ? ? ?.failureUrl("/login.html")//認證失敗之后 ?redirect 跳轉(zhuǎn)
? ? ? ? ? ? ? ? .failureHandler(new MyAuthenticationFailureHandler())//用來自定義認證失敗之后處理 ?前后端分離解決方案
? ? ? ? ? ? ? ? .and()
? ? ? ? ? ? ? ? .logout()
// ? ? ? ? ? ? ? ?.logoutUrl("/logout") //指定注銷登錄url ?默認請求方式必須:GET
? ? ? ? ? ? ? ? .logoutRequestMatcher(new OrRequestMatcher(
? ? ? ? ? ? ? ? ? ? ? ? new AntPathRequestMatcher("/aa","GET"),
? ? ? ? ? ? ? ? ? ? ? ? new AntPathRequestMatcher("/bb","POST")
? ? ? ? ? ? ? ? ? ? ? ? )
? ? ? ? ? ? ? ? )
? ? ? ? ? ? ? ? .invalidateHttpSession(true) //會話失效 默認值為true,可不寫
? ? ? ? ? ? ? ? .clearAuthentication(true) //清除認證標記 默認值為true,可不寫
? ? ? ? ? ? ? ? .logoutSuccessUrl("/loginHtml") //注銷成功之后跳轉(zhuǎn)頁面
? ? ? ? ? ? ? ? .and()
? ? ? ? ? ? ? ? .csrf().disable(); //禁止csrf 跨站請求保護
? ? }
}測試
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ù),表示登錄認證成功然后訪問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","注銷成功,當前認證對象為:"+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 {
? ? ? ? //【注意事項】放行資源要放在前面,認證的放在后面
? ? ? ? http.authorizeRequests()
? ? ? ? ? ? ? ? .mvcMatchers("/index").permitAll() //代表放行index的所有請求
? ? ? ? ? ? ? ? .mvcMatchers("/loginHtml").permitAll() //放行l(wèi)oginHtml請求
? ? ? ? ? ? ? ? .anyRequest().authenticated()//代表其他請求需要認證
? ? ? ? ? ? ? ? .and()
? ? ? ? ? ? ? ? .formLogin()//表示其他需要認證的請求通過表單認證
? ? ? ? ? ? ? ? //loginPage 一旦你自定義了這個登錄頁面,那你必須要明確告訴SpringSecurity日后哪個url處理你的登錄請求
? ? ? ? ? ? ? ? .loginPage("/loginHtml")//用來指定自定義登錄界面,不使用SpringSecurity默認登錄界面 ?注意:一旦自定義登錄頁面,必須指定登錄url
? ? ? ? ? ? ? ? //loginProcessingUrl ?這個doLogin請求本身是沒有的,因為我們只需要明確告訴SpringSecurity,日后只要前端發(fā)起的是一個doLogin這樣的請求,
? ? ? ? ? ? ? ? //那SpringSecurity應(yīng)該把你username和password給捕獲到
? ? ? ? ? ? ? ? .loginProcessingUrl("/doLogin")//指定處理登錄的請求url
? ? ? ? ? ? ? ? .usernameParameter("uname") //指定登錄界面用戶名文本框的name值,如果沒有指定,默認屬性名必須為username
? ? ? ? ? ? ? ? .passwordParameter("passwd")//指定登錄界面密碼密碼框的name值,如果沒有指定,默認屬性名必須為password
// ? ? ? ? ? ? ? ?.successForwardUrl("/index")//認證成功 forward 跳轉(zhuǎn)路徑,forward代表服務(wù)器內(nèi)部的跳轉(zhuǎn)之后,地址欄不變 始終在認證成功之后跳轉(zhuǎn)到指定請求
// ? ? ? ? ? ? ? ?.defaultSuccessUrl("/index")//認證成功 之后跳轉(zhuǎn),重定向 redirect 跳轉(zhuǎn)后,地址會發(fā)生改變 ?根據(jù)上一保存請求進行成功跳轉(zhuǎn)
? ? ? ? ? ? ? ? .successHandler(new MyAuthenticationSuccessHandler()) //認證成功時處理 ?前后端分離解決方案
// ? ? ? ? ? ? ? ?.failureForwardUrl("/loginHtml")//認證失敗之后 forward 跳轉(zhuǎn)
// ? ? ? ? ? ? ? ?.failureUrl("/login.html")//認證失敗之后 ?redirect 跳轉(zhuǎn)
? ? ? ? ? ? ? ? .failureHandler(new MyAuthenticationFailureHandler())//用來自定義認證失敗之后處理 ?前后端分離解決方案
? ? ? ? ? ? ? ? .and()
? ? ? ? ? ? ? ? .logout()
// ? ? ? ? ? ? ? ?.logoutUrl("/logout") //指定注銷登錄url ?默認請求方式必須:GET
? ? ? ? ? ? ? ? .logoutRequestMatcher(new OrRequestMatcher(
? ? ? ? ? ? ? ? ? ? ? ? new AntPathRequestMatcher("/aa","GET"),
? ? ? ? ? ? ? ? ? ? ? ? new AntPathRequestMatcher("/bb","POST")
? ? ? ? ? ? ? ? ? ? ? ? )
? ? ? ? ? ? ? ? )
? ? ? ? ? ? ? ? .invalidateHttpSession(true) //會話失效 默認值為true,可不寫
? ? ? ? ? ? ? ? .clearAuthentication(true) //清除認證標記 默認值為true,可不寫
// ? ? ? ? ? ? ? ?.logoutSuccessUrl("/loginHtml") //注銷成功之后跳轉(zhuǎn)頁面
? ? ? ? ? ? ? ? .logoutSuccessHandler(new MyLogoutSuccessHandler()) //注銷成功之后處理 ?前后端分離解決方案
? ? ? ? ? ? ? ? .and()
? ? ? ? ? ? ? ? .csrf().disable(); //禁止csrf 跨站請求保護
? ? }
}測試
跟第一個測試方法是一樣的

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot使用filter獲取自定義請求頭的實現(xiàn)代碼
這篇文章主要介紹了springboot使用filter獲取自定義請求頭的實例代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05
超簡單的java獲取鼠標點擊位置坐標的實例(鼠標在Jframe上的坐標)
在Java窗體Jframe上獲取鼠標點擊的坐標,其中使用了匿名內(nèi)部類,實例代碼非常簡單易懂,大家可以學(xué)習(xí)一下2018-03-03
Spring+MyBatis實現(xiàn)數(shù)據(jù)庫讀寫分離方案
本文主要介紹了Spring+MyBatis實現(xiàn)數(shù)據(jù)庫讀寫分離方案。具有一定的參考價值,下面跟著小編一起來看下吧2017-01-01
Java輸出通過InetAddress獲得的IP地址數(shù)組詳細解析
由于byte被認為是unsigned byte,所以最高位的1將會被解釋為符號位,另外Java中存儲是按照補碼存儲,所以1000 0111會被認為是補碼形式,轉(zhuǎn)換成原碼便是1111 0001,轉(zhuǎn)換成十進制數(shù)便是-1212013-09-09
java商城項目實戰(zhàn)之購物車功能實現(xiàn)
這篇文章主要為大家詳細介紹了java商城項目實戰(zhàn)之購物車功能實現(xiàn),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04

