springboot簡單實現(xiàn)單點登錄的示例代碼
什么是單點登錄就不用再說了,今天通過自定義sessionId來實現(xiàn)它,想了解的可以參考https://www.xuxueli.com/xxl-sso/
講一下大概的實現(xiàn)思路吧:這里有一個認證中心,兩個單獨的服務(wù)。每個服務(wù)去請求的 時候都要經(jīng)過一個過濾器,首先判斷該請求地址中有沒有sessionid,有的話則寫入cookie ,如果請求地址中沒有sessionid那么從cookie中去獲取,如果cookie中獲取到了則證明登錄了,放行即可。否則跳轉(zhuǎn)到認證中心,此時把請求地址當(dāng)做參數(shù)帶到認證中,認證中心認證成功后把sessionid寫入cookie,同時重定向帶過來的地址,并且把sessionid拼接上。一般直接在認證中心認證過后,直接訪問其他系統(tǒng)地址是不會攔截該請求的,所以這個時候可以在跳轉(zhuǎn)到認證頁面的時候先判斷認證中心這邊有沒有sessionid,有的話,直接返回,否則再進入認證頁面。
語言表達不清楚,下面先來幾張圖壓壓驚,看看效果。
沒有登錄前我訪問:http://127.0.0.1:2000/client1
直接跳轉(zhuǎn)到認證頁面了
認證成功后
由于我這client系統(tǒng)中登陸過了,所以我直接訪問client2,不用登錄直接出現(xiàn)了效果
我接下來直接先去認證中心登錄一下
此時訪問:http://127.0.0.1:2000/client1
效果演示完畢了,下面上代碼
Client1代碼:
@Controller public class Client1Controller { @GetMapping("/client1") public String client1(){ return "client1"; } }
import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; import javax.servlet.*; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @Component public class Client1Filter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest)servletRequest; HttpServletResponse response = (HttpServletResponse)servletResponse; String parameter = request.getParameter("sso-sessionId"); if(StringUtils.isNotEmpty(parameter)){ Cookie cookie = new Cookie("sso-sessionId", parameter); cookie.setPath("/"); response.addCookie(cookie); } String token=""; Cookie[] cookies = request.getCookies(); if(cookies!=null){ for(Cookie cookie:cookies){ String name = cookie.getName(); if(name.equals("sso-sessionId")){ token = cookie.getValue(); } } } if(StringUtils.isEmpty(token)){ response.sendRedirect("http://127.0.0.1:4000/login?return_url="+request.getRequestURL()); return; } chain.doFilter(servletRequest,servletResponse); } }
由于Client2代碼和Client1代碼基本一直,所以就不貼上了
認證中心代碼
import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.UUID; @Controller public class ServerController { @GetMapping("/login") public String login(String return_url, HttpServletRequest request,HttpServletResponse response) throws IOException { String token=null; Cookie[] cookies = request.getCookies(); if(cookies!=null){ for(Cookie cookie:cookies){ String name = cookie.getName(); if(name.equals("sso-sessionId")){ System.out.println(cookie); token = cookie.getValue(); } } } if(StringUtils.isNotEmpty(return_url)){ if(StringUtils.isNotEmpty(token)){ return_url=return_url+"?sso-sessionId="+token; response.sendRedirect(return_url); } } request.setAttribute("return_url",return_url); return "login"; } @GetMapping("/success") public String success(){ return "success"; } @PostMapping("/doLogin") //@ResponseBody public void doLogin(String userName, String passWord, String return_url, HttpServletResponse response) throws IOException { String token = UUID.randomUUID().toString().replace("-",""); Cookie cookie = new Cookie("sso-sessionId", token); cookie.setPath("/"); response.addCookie(cookie); if(StringUtils.isEmpty(return_url)) { response.sendRedirect("http://127.0.0.1:4000/success"); }else{ return_url=return_url+"?sso-sessionId="+token; response.sendRedirect(return_url); } } }
這樣的話,一個簡單的單點登錄就完成了,至于擴展優(yōu)化,自行發(fā)揮了
到此這篇關(guān)于springboot簡單實現(xiàn)單點登錄的示例代碼的文章就介紹到這了,更多相關(guān)springboot 單點登錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Spring?Security?捕獲?filter?層面異常返回我們自定義的內(nèi)容
Spring?的異常會轉(zhuǎn)發(fā)到?BasicErrorController?中進行異常寫入,然后才會返回客戶端。所以,我們可以在?BasicErrorController?對?filter異常進行捕獲并處理,下面通過本文給大家介紹Spring?Security?捕獲?filter?層面異常,返回我們自定義的內(nèi)容,感興趣的朋友一起看看吧2022-05-05Java調(diào)用ChatGPT(基于SpringBoot和Vue)實現(xiàn)可連續(xù)對話和流式輸出的ChatGPT API
這篇文章主要介紹了Java調(diào)用ChatGPT(基于SpringBoot和Vue),實現(xiàn)可連續(xù)對話和流式輸出的ChatGPT API(可自定義實現(xiàn)AI助手),文中代碼示例介紹的非常詳細,感興趣的朋友可以參考下2023-04-04java線程之使用Runnable接口創(chuàng)建線程的方法
本篇文章介紹了,java中使用Runnable接口創(chuàng)建線程的方法。需要的朋友參考下2013-05-05Java圖形化編程之JFrame疫苗接種系統(tǒng)詳解
GUI圖形界面設(shè)計是用戶和程序交互的工具,用戶通過圖形界面控制程序事件的發(fā)生。首先介紹Swing的基本體系結(jié)構(gòu),這是底層2021-09-09Java Volatile關(guān)鍵字實現(xiàn)原理過程解析
這篇文章主要介紹了Java Volatile關(guān)鍵字實現(xiàn)原理過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03java使用mysql預(yù)編譯語句查詢優(yōu)勢及示例詳解
這篇文章主要為大家介紹了java使用mysql預(yù)編譯語句的優(yōu)勢特點及示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06idea?maven依賴引入失效無法正常導(dǎo)入依賴問題的解決方法
有時候idea導(dǎo)入一個新項目,或者pom文件修改(新增)了依賴,pom文件和代碼會報紅,提示依賴包不存在,下面這篇文章主要給大家介紹了關(guān)于idea?maven依賴引入失效無法正常導(dǎo)入依賴問題的解決方法,需要的朋友可以參考下2023-04-04