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

認(rèn)證成功后

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

我接下來(lái)直接先去認(rèn)證中心登錄一下

此時(shí)訪問(wè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);
}
}
}
這樣的話,一個(gè)簡(jiǎn)單的單點(diǎn)登錄就完成了,至于擴(kuò)展優(yōu)化,自行發(fā)揮了
到此這篇關(guān)于springboot簡(jiǎn)單實(shí)現(xiàn)單點(diǎn)登錄的示例代碼的文章就介紹到這了,更多相關(guān)springboot 單點(diǎn)登錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot oauth2實(shí)現(xiàn)單點(diǎn)登錄實(shí)例
- springboot 集成cas5.3 實(shí)現(xiàn)sso單點(diǎn)登錄詳細(xì)流程
- springboot集成CAS實(shí)現(xiàn)單點(diǎn)登錄的示例代碼
- SpringBoot整合SSO(single sign on)單點(diǎn)登錄
- SpringBoot+Vue+Redis實(shí)現(xiàn)單點(diǎn)登錄(一處登錄另一處退出登錄)
- 使用springboot結(jié)合vue實(shí)現(xiàn)sso單點(diǎn)登錄
- 基于springboot和redis實(shí)現(xiàn)單點(diǎn)登錄
- 單點(diǎn)登錄的概念及SpringBoot實(shí)現(xiàn)單點(diǎn)登錄的操作方法
相關(guān)文章
Java項(xiàng)目實(shí)現(xiàn)模擬ATM機(jī)
這篇文章主要為大家詳細(xì)介紹了Java項(xiàng)目實(shí)現(xiàn)模擬ATM機(jī),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
Jmeter參數(shù)化實(shí)現(xiàn)原理及過(guò)程解析
這篇文章主要介紹了Jmeter參數(shù)化實(shí)現(xiàn)原理及過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
SpringBoot利用filter實(shí)現(xiàn)xss防御功能
Cross-Site?Scripting(跨站腳本攻擊)簡(jiǎn)稱?XSS,是一種代碼注入攻擊,攻擊者通過(guò)在目標(biāo)網(wǎng)站上注入惡意腳本,使之在用戶的瀏覽器上運(yùn)行,利用這些惡意腳本,攻擊者可獲取用戶的敏感信息,本文給大家介紹了SpringBoot利用filter實(shí)現(xiàn)xss防御功能,需要的朋友可以參考下2024-09-09
spring-data-elasticsearch @Field注解無(wú)效的完美解決方案
這篇文章主要介紹了spring-data-elasticsearch @Field注解無(wú)效的完美解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
springboot之如何同時(shí)連接多個(gè)redis
這篇文章主要介紹了springboot之如何同時(shí)連接多個(gè)redis問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
springboot實(shí)現(xiàn)通過(guò)路徑從磁盤直接讀取圖片
這篇文章主要介紹了springboot實(shí)現(xiàn)通過(guò)路徑從磁盤直接讀取圖片,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Springboot JPA打印SQL語(yǔ)句及參數(shù)的實(shí)現(xiàn)
在SpringBoot項(xiàng)目中調(diào)試和優(yōu)化數(shù)據(jù)庫(kù)操作是很常見(jiàn)的需求,本文主要介紹了Springboot JPA打印SQL語(yǔ)句及參數(shù)的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06

