SpringBoot如何實(shí)現(xiàn)同域SSO(單點(diǎn)登錄)
單點(diǎn)登錄,其實(shí)看起來(lái)不是很復(fù)雜,只是細(xì)節(jié)上的處理,單點(diǎn)區(qū)分有三種
- 同域SSO
- 同父域SSO
- 跨域的SSO
如何實(shí)現(xiàn)同域SSO?
個(gè)人理解:當(dāng)用戶登錄訪問(wèn)demo1.lzmvlog.top
時(shí),同時(shí)具有訪問(wèn)demo2.lzmvlog.top
的能力,即認(rèn)證完成一次,可以訪問(wèn)所有系統(tǒng)。
實(shí)現(xiàn)方式:可以采用Cookie
實(shí)現(xiàn),即用戶在訪問(wèn)一個(gè)系統(tǒng)時(shí),攜帶認(rèn)證頒發(fā)的信息,系統(tǒng)響應(yīng)是否具有訪問(wèn)資格,否則跳轉(zhuǎn)認(rèn)證,也可以采用Session
,即Session
共享,校驗(yàn)訪問(wèn)用戶是否具有有效的信息,提供訪問(wèn)資格
代碼實(shí)現(xiàn)
依賴
<!--spring-data-jpa--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
配置
server: port: 8090 spring: application: name: authority datasource: url: jdbc:mysql://127.0.0.1:3306/SSO?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8 username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver
當(dāng)用戶訪問(wèn)除登錄界面時(shí),都需要提前認(rèn)證,認(rèn)證完成之后需要跳轉(zhuǎn)到之前訪問(wèn)的路徑上,并提供訪問(wèn)別的系統(tǒng)的權(quán)限。
實(shí)現(xiàn)邏輯,當(dāng)用戶訪問(wèn)任何路徑時(shí),都需要通過(guò)攔截器的校驗(yàn),確認(rèn)擁有訪問(wèn)的權(quán)限,才能放行通過(guò),不具有訪問(wèn)權(quán)限的,重定向到 登錄界面,并保存原有訪問(wèn)的頁(yè)面路徑,驗(yàn)證成功的時(shí)候跳轉(zhuǎn)到原有頁(yè)面
控制器
@Controller public class IndexController { @Autowired UserRepository userRepository; /** * 將要跳轉(zhuǎn)的路徑 */ public String url; /** * 登錄界面 * * @return */ @GetMapping("/index") public String index() { return "index"; } /** * 登錄界面 * * @return */ @GetMapping("/") public String index1() { return "index"; } /** * 登錄請(qǐng)求接口 * * @param username 賬號(hào) * @param password 密碼 * @param response * @return */ @PostMapping("login") public String login(String username, String password, HttpServletResponse response) { // 用戶登錄 boolean exists = userRepository.exists(Example.of(new User() .setUsername(username) .setPassword(password))); if (exists) { Cookie cookie = new Cookie("username", username); response.addCookie(cookie); // 如果正常訪問(wèn)即跳轉(zhuǎn)到正常頁(yè)面 if (StringUtils.isEmpty(url)) { return "demo1"; } // 如果之前存在訪問(wèn)的頁(yè)面,認(rèn)證完成即跳轉(zhuǎn)會(huì)原有的頁(yè)面 return url; } return "index"; } /** * 跳轉(zhuǎn)到 demo2 * * @return */ @GetMapping("demo2") public String demo2() { return "demo2"; } /** * 跳轉(zhuǎn)到 demo1 * * @return */ @GetMappi=ng("demo1") public String demo1() { return "demo1"; } }
攔截器實(shí)現(xiàn)
@Component public class CookieHandlerInterceptor implements HandlerInterceptor { @Autowired UserRepository userRepository; @Autowired IndexController indexController; /** * 執(zhí)行方法之前 * * @param request * @param response * @param handler * @return * @throws Exception */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // 獲取當(dāng)前請(qǐng)求得路徑 如果不是正常得登錄界面請(qǐng)求 登錄成功之后需要跳轉(zhuǎn)到原來(lái)請(qǐng)求得界面上 String servletPath = request.getServletPath(); // 對(duì)不需要攔截得路徑進(jìn)行放行 if ("/index".equals(servletPath) || "/".equals(servletPath) || "/login".equals(servletPath)) { return true; } if (!"/index".equals(servletPath) || !"/".equals(servletPath)) { indexController.url = servletPath; } Cookie[] cookies = request.getCookies(); boolean exists = false; if (cookies != null) { for (Cookie cookie : cookies) { String value = cookie.getValue(); if (!StringUtils.isEmpty(value)) { exists = userRepository.exists(Example.of(new User() .setUsername(value))); } } } if (exists) { return true; } else { response.sendRedirect("/index"); } return false; } }
在SpringBoot2.x
之后不能生效,需要將攔截器添加到攔截器鏈路中,即:
@Configuration public class WebMvcConfig extends WebMvcConfigurationSupport { /** * Session 攔截處理器 */ @Autowired private CookieHandlerInterceptor cookieHandlerInterceptor; /** * 添加攔截器 * * @param registry */ @Override protected void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(this.cookieHandlerInterceptor).addPathPatterns("/**"); super.addInterceptors(registry); } }
其實(shí)攔截器還有第二種實(shí)現(xiàn)方式,即通過(guò)Filter
接口實(shí)現(xiàn)
@Component public class CookieFilter extends OncePerRequestFilter { @Autowired UserRepository userRepository; @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { // 獲取當(dāng)前請(qǐng)求得路徑 如果不是正常得登錄界面請(qǐng)求 登錄成功之后需要跳轉(zhuǎn)到原來(lái)請(qǐng)求得界面上 String servletPath = request.getServletPath(); IndexController indexController = new IndexController(); // 對(duì)不需要攔截得路徑進(jìn)行放行 if ("/index".equals(servletPath) || "/".equals(servletPath) || "/login".equals(servletPath)) { filterChain.doFilter(request, response); } if (!"/index".equals(servletPath) || !"/".equals(servletPath)) { indexController.url = servletPath; } Cookie[] cookies = request.getCookies(); boolean exists = false; if (cookies != null) { for (Cookie cookie : cookies) { String value = cookie.getValue(); if (!StringUtils.isEmpty(value)) { exists = userRepository.exists(Example.of(new User() .setUsername(value))); } } } if (exists) { filterChain.doFilter(request, response); } else { response.sendRedirect("/"); } } }
其實(shí)也可以采用Session
的方式實(shí)現(xiàn),采用共享Session
的方式,我這里只是簡(jiǎn)單的實(shí)現(xiàn)一下,其實(shí)在認(rèn)證時(shí)可以結(jié)合SpringSecurity
或者Shiro
安全框架去整合JWT
以保證信息的安全
界面
index.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登錄</title> </head> <body> <div align="center"> <h1>請(qǐng)登錄</h1> <form action="/login" method="post"> <span>賬號(hào):</span><input name="username" type="text" value="zhang"><br> <span>密碼:</span><input name="password" type="password" value="123456"><br> <button type="submit" style="margin: 10px 0">登錄</button> </form> </div> </body> </html>
demo1.html
和demo2.html
只需要坐一下簡(jiǎn)單的區(qū)分,知道是哪個(gè)頁(yè)面就行了
同域SSO
其實(shí)不是很復(fù)雜,只是了解一下整個(gè)訪問(wèn)的過(guò)程,和需要做的一些限制即可,后續(xù)看看做后面兩種的實(shí)現(xiàn)
即同父域SSO
和跨域SSO
以上就是SpringBoot如何實(shí)現(xiàn)同域SSO(單點(diǎn)登錄)的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot 實(shí)現(xiàn)同域SSO的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
從application.properties配置文件獲取的漢字亂碼的解決方法
平時(shí)從配置文件各種讀取配置參數(shù)都正常,但是有時(shí)候放了個(gè)中文就亂碼,你肯定試過(guò)網(wǎng)上好多方法,都沒(méi)解決,那么來(lái)看下面,恭喜你終于找這里了,本文給大家介紹了從application.properties配置文件獲取的漢字亂碼的解決方法,需要的朋友可以參考下2024-03-03mybatis resultType自帶數(shù)據(jù)類型別名解讀
MyBatis為了簡(jiǎn)化開(kāi)發(fā),通過(guò)org.apache.ibatis.type.TypeAliasRegistry為常見(jiàn)類定義了別名,這些別名包括基本數(shù)據(jù)類型及其數(shù)組、集合類型等,如string對(duì)應(yīng)java.lang.String,int對(duì)應(yīng)java.lang.Integer等,此外,還有特殊前綴的別名如_int對(duì)應(yīng)int類型2024-10-10SpringBoot可視化接口開(kāi)發(fā)工具magic-api的簡(jiǎn)單使用教程
作為Java后端開(kāi)發(fā),平時(shí)開(kāi)發(fā)API接口的時(shí)候經(jīng)常需要定義Controller、Service、Dao、Mapper、XML、VO等Java對(duì)象。有沒(méi)有什么辦法可以讓我們不寫這些代碼,直接操作數(shù)據(jù)庫(kù)生成API接口呢?今天給大家推薦一款工具magic-api,來(lái)幫我們實(shí)現(xiàn)這個(gè)小目標(biāo)!2021-06-06MyBatis-Plus條件構(gòu)造器Wrapper應(yīng)用實(shí)例
QueryWrapper是用于查詢的Wrapper條件構(gòu)造器,可以通過(guò)它來(lái)構(gòu)建SELECT語(yǔ)句中的WHERE條件,這篇文章主要介紹了MyBatis-Plus數(shù)據(jù)表操作條件構(gòu)造器Wrapper,需要的朋友可以參考下2023-09-09Java中關(guān)鍵字synchronized的使用方法詳解
synchronized關(guān)鍵字可以作為函數(shù)的修飾符,也可作為函數(shù)內(nèi)的語(yǔ)句,也就是平時(shí)說(shuō)的同步方法和同步語(yǔ)句塊,下面這篇文章主要給大家介紹了關(guān)于Java中synchronized使用的相關(guān)資料,需要的朋友可以參考下2021-08-08Spring Security單項(xiàng)目權(quán)限設(shè)計(jì)過(guò)程解析
這篇文章主要介紹了Spring Security單項(xiàng)目權(quán)限設(shè)計(jì)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11ArrayList和LinkedList的區(qū)別、擴(kuò)容機(jī)制以及底層的實(shí)現(xiàn)方式
這篇文章主要介紹了ArrayList和LinkedList的區(qū)別、擴(kuò)容機(jī)制以及底層的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03