springboot簡單實現(xiàn)單點登錄的示例代碼
什么是單點登錄就不用再說了,今天通過自定義sessionId來實現(xiàn)它,想了解的可以參考https://www.xuxueli.com/xxl-sso/
講一下大概的實現(xiàn)思路吧:這里有一個認(rèn)證中心,兩個單獨的服務(wù)。每個服務(wù)去請求的 時候都要經(jīng)過一個過濾器,首先判斷該請求地址中有沒有sessionid,有的話則寫入cookie ,如果請求地址中沒有sessionid那么從cookie中去獲取,如果cookie中獲取到了則證明登錄了,放行即可。否則跳轉(zhuǎn)到認(rèn)證中心,此時把請求地址當(dāng)做參數(shù)帶到認(rèn)證中,認(rèn)證中心認(rèn)證成功后把sessionid寫入cookie,同時重定向帶過來的地址,并且把sessionid拼接上。一般直接在認(rèn)證中心認(rèn)證過后,直接訪問其他系統(tǒng)地址是不會攔截該請求的,所以這個時候可以在跳轉(zhuǎn)到認(rèn)證頁面的時候先判斷認(rèn)證中心這邊有沒有sessionid,有的話,直接返回,否則再進(jìn)入認(rèn)證頁面。
語言表達(dá)不清楚,下面先來幾張圖壓壓驚,看看效果。
沒有登錄前我訪問:http://127.0.0.1:2000/client1 直接跳轉(zhuǎn)到認(rèn)證頁面了

認(rèn)證成功后

由于我這client系統(tǒng)中登陸過了,所以我直接訪問client2,不用登錄直接出現(xiàn)了效果

我接下來直接先去認(rè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代碼基本一直,所以就不貼上了
認(rèn)證中心代碼
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);
}
}
}
這樣的話,一個簡單的單點登錄就完成了,至于擴(kuò)展優(yōu)化,自行發(fā)揮了
到此這篇關(guān)于springboot簡單實現(xiàn)單點登錄的示例代碼的文章就介紹到這了,更多相關(guān)springboot 單點登錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot利用filter實現(xiàn)xss防御功能
Cross-Site?Scripting(跨站腳本攻擊)簡稱?XSS,是一種代碼注入攻擊,攻擊者通過在目標(biāo)網(wǎng)站上注入惡意腳本,使之在用戶的瀏覽器上運行,利用這些惡意腳本,攻擊者可獲取用戶的敏感信息,本文給大家介紹了SpringBoot利用filter實現(xiàn)xss防御功能,需要的朋友可以參考下2024-09-09
spring-data-elasticsearch @Field注解無效的完美解決方案
這篇文章主要介紹了spring-data-elasticsearch @Field注解無效的完美解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
springboot實現(xiàn)通過路徑從磁盤直接讀取圖片
這篇文章主要介紹了springboot實現(xiàn)通過路徑從磁盤直接讀取圖片,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Springboot JPA打印SQL語句及參數(shù)的實現(xiàn)
在SpringBoot項目中調(diào)試和優(yōu)化數(shù)據(jù)庫操作是很常見的需求,本文主要介紹了Springboot JPA打印SQL語句及參數(shù)的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-06-06

