亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

springboot簡(jiǎn)單實(shí)現(xiàn)單點(diǎn)登錄的示例代碼

 更新時(shí)間:2022年01月14日 09:51:27   作者:lovely好詭異  
本文主要介紹了springboot簡(jiǎn)單實(shí)現(xiàn)單點(diǎn)登錄的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

什么是單點(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,有的話則寫(xiě)入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寫(xiě)入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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論