SpringBoot如何實(shí)現(xiàn)同域SSO(單點(diǎn)登錄)
單點(diǎn)登錄,其實(shí)看起來不是很復(fù)雜,只是細(xì)節(jié)上的處理,單點(diǎn)區(qū)分有三種
同域SSO 同父域SSO 跨域的SSO 如何實(shí)現(xiàn)同域SSO?個(gè)人理解:當(dāng)用戶登錄訪問demo1.lzmvlog.top時(shí),同時(shí)具有訪問demo2.lzmvlog.top的能力,即認(rèn)證完成一次,可以訪問所有系統(tǒng)。
實(shí)現(xiàn)方式:可以采用Cookie實(shí)現(xiàn),即用戶在訪問一個(gè)系統(tǒng)時(shí),攜帶認(rèn)證頒發(fā)的信息,系統(tǒng)響應(yīng)是否具有訪問資格,否則跳轉(zhuǎn)認(rèn)證,也可以采用Session,即Session共享,校驗(yà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: 8090spring: 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)用戶訪問除登錄界面時(shí),都需要提前認(rèn)證,認(rèn)證完成之后需要跳轉(zhuǎn)到之前訪問的路徑上,并提供訪問別的系統(tǒng)的權(quán)限。
實(shí)現(xiàn)邏輯,當(dāng)用戶訪問任何路徑時(shí),都需要通過攔截器的校驗(yàn),確認(rèn)擁有訪問的權(quán)限,才能放行通過,不具有訪問權(quán)限的,重定向到 登錄界面,并保存原有訪問的頁面路徑,驗(yàn)證成功的時(shí)候跳轉(zhuǎn)到原有頁面
控制器@Controllerpublic 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'; } /** * 登錄請求接口 * * @param username 賬號 * @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); // 如果正常訪問即跳轉(zhuǎn)到正常頁面 if (StringUtils.isEmpty(url)) {return 'demo1'; } // 如果之前存在訪問的頁面,認(rèn)證完成即跳轉(zhuǎn)會原有的頁面 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)
@Componentpublic 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)前請求得路徑 如果不是正常得登錄界面請求 登錄成功之后需要跳轉(zhuǎn)到原來請求得界面上String servletPath = request.getServletPath();// 對不需要攔截得路徑進(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之后不能生效,需要將攔截器添加到攔截器鏈路中,即:
@Configurationpublic 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)方式,即通過Filter接口實(shí)現(xiàn)
@Componentpublic class CookieFilter extends OncePerRequestFilter { @Autowired UserRepository userRepository; @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {// 獲取當(dāng)前請求得路徑 如果不是正常得登錄界面請求 登錄成功之后需要跳轉(zhuǎn)到原來請求得界面上String servletPath = request.getServletPath();IndexController indexController = new IndexController();// 對不需要攔截得路徑進(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的方式,我這里只是簡單的實(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>請登錄</h1> <form action='/login' method='post'><span>賬號:</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只需要坐一下簡單的區(qū)分,知道是哪個(gè)頁面就行了
同域SSO其實(shí)不是很復(fù)雜,只是了解一下整個(gè)訪問的過程,和需要做的一些限制即可,后續(xù)看看做后面兩種的實(shí)現(xiàn)
即同父域SSO和跨域SSO
以上就是SpringBoot如何實(shí)現(xiàn)同域SSO(單點(diǎn)登錄)的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot 實(shí)現(xiàn)同域SSO的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)2. XML入門的常見問題(三)3. Vue3使用JSX的方法實(shí)例(筆記自用)4. vue實(shí)現(xiàn)將自己網(wǎng)站(h5鏈接)分享到微信中形成小卡片的超詳細(xì)教程5. 詳解CSS偽元素的妙用單標(biāo)簽之美6. 不要在HTML中濫用div7. 利用CSS3新特性創(chuàng)建透明邊框三角8. 多級聯(lián)動下拉選擇框,動態(tài)獲取下一級9. Vue3獲取DOM節(jié)點(diǎn)的3種方式實(shí)例10. 前端html+css實(shí)現(xiàn)動態(tài)生日快樂代碼
