這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)springboot中怎么利用vue實現(xiàn)sso單點登錄,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)是專業(yè)的無為網(wǎng)站建設(shè)公司,無為接單;提供成都網(wǎng)站設(shè)計、成都網(wǎng)站制作,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行無為網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊,希望更多企業(yè)前來合作!
開發(fā)工具:idea, maven3靜態(tài)文件下載地址
1.pom文件:
2.MVC頁面跳轉(zhuǎn)配置類,如果請求直接跳轉(zhuǎn)頁面,采用該配置可以減少controller中代碼的編寫
package com.xicheng.ssodemo.common;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;/** * @author xichengxml * @date 2019/2/13 16:56 */@Configurationpublic class SpringMvcConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("login").setViewName("login"); registry.addViewController("success").setViewName("success"); registry.addViewController("error").setViewName("error"); }}
3.核心校驗類編寫
package com.xicheng.ssodemo.common;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;/** * @author xichengxml * @date 2019/2/13 16:19 * 登錄校驗工具類 */public class LoginCheck { /** 用戶名 */ private static final String USERNAME = "uname"; /** 密碼 */ private static final String PASSWORD = "pwd"; /** cookie鍵 */ public static final String COOKIE_KEY = "sso"; /** cookie值 */ public static final String COOKIE_VAL = "ssocookie"; /** * 校驗用戶名和密碼 * @param userName 登錄用戶名 * @param password 登錄密碼 * @return 成功返回true; 失敗返回false */ public static boolean checkLogin(String userName, String password) { return USERNAME.equals(userName) && PASSWORD.equals(password); } /** * 校驗cookie * @param request * @return */ public static boolean checkCookie(HttpServletRequest request) { Cookie[] cookies = request.getCookies(); if (cookies != null && cookies.length != 0) { for (Cookie cookie : cookies) { if (COOKIE_KEY.equals(cookie.getName()) && COOKIE_VAL.equals(cookie.getValue())) { return true; } } } return false; }}
4.核心登錄controller編寫
package com.xicheng.ssodemo.controller;import com.xicheng.ssodemo.common.LoginCheck;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletResponse;/** * @author xichengxml * @date 2019/2/13 16:31 */@Controllerpublic class LoginController { /** * 登錄成功后保存cookie在域下,返回登錄成功頁 * @param uname * @param pwd * @param response * @return */ @RequestMapping("/do-login") public String doLogin(String uname, String pwd, HttpServletResponse response) { if (LoginCheck.checkLogin(uname, pwd)) { Cookie cookie = new Cookie(LoginCheck.COOKIE_KEY, LoginCheck.COOKIE_VAL); cookie.setPath("/"); response.addCookie(cookie); return "success"; } return "error"; }}
5.登錄controller編寫
package com.xicheng.ssodemo.controller;import com.xicheng.ssodemo.common.LoginCheck;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.HttpServletRequest;/** * @author xichengxml * @date 2019/2/13 17:53 */@Controllerpublic class SameOriginController { /** * cookie存在,登錄成功;否則返回登錄頁 * @param request * @return */ @RequestMapping("/login01") public String demo01(HttpServletRequest request) { return LoginCheck.checkCookie(request) ? "success" : "login"; } @RequestMapping("/login02") public String demo02(HttpServletRequest request) { return LoginCheck.checkCookie(request) ? "success" : "login"; }}
6.登錄頁面及成功失敗頁面編寫
上述就是小編為大家分享的springboot中怎么利用vue實現(xiàn)sso單點登錄了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。