
文章插圖
背景分析傳統(tǒng)的登錄系統(tǒng)中,每個(gè)站點(diǎn)都實(shí)現(xiàn)了自己的專用登錄模塊 。各站點(diǎn)的登錄狀態(tài)相互不認(rèn)可,各站點(diǎn)需要逐一手工登錄 。例如:
這樣的系統(tǒng),我們又稱之為多點(diǎn)登陸系統(tǒng) 。應(yīng)用起來相對(duì)繁瑣(每次訪問資源服務(wù)都需要重新登陸認(rèn)證和授權(quán)) 。與此同時(shí),系統(tǒng)代碼的重復(fù)也比較高 。由此單點(diǎn)登陸系統(tǒng)誕生 。
單點(diǎn)登陸系統(tǒng)單點(diǎn)登錄,英文是 Single Sign On(縮寫為 SSO) 。即多個(gè)站點(diǎn)共用一臺(tái)認(rèn)證授權(quán)服務(wù)器,用戶在其中任何一個(gè)站點(diǎn)登錄后,可以免登錄訪問其他所有站點(diǎn) 。而且,各站點(diǎn)間可以通過該登錄狀態(tài)直接交互 。例如:
快速入門實(shí)踐工程結(jié)構(gòu)如下基于資源服務(wù)工程添加單點(diǎn)登陸認(rèn)證和授權(quán)服務(wù),工程結(jié)構(gòu)定義如下:
創(chuàng)建認(rèn)證授權(quán)工程添加項(xiàng)目依賴<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-oauth2</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency>構(gòu)建項(xiàng)目配置文件在sca-auth工程中創(chuàng)建bootstrap.yml文件,例如:
server:port: 8071spring:application:name: sca-authcloud:nacos:discovery:server-addr: localhost:8848config:server-addr: localhost:8848添加項(xiàng)目啟動(dòng)類package com.jt;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class ResourceAuthApplication {public static void main(String[] args) {SpringApplication.run(ResourceAuthApplication.class, args);}}啟動(dòng)并訪問項(xiàng)目項(xiàng)目啟動(dòng)時(shí),系統(tǒng)會(huì)默認(rèn)生成一個(gè)登陸密碼,例如:
其中,默認(rèn)用戶名為user,密碼為系統(tǒng)啟動(dòng)時(shí),在控制臺(tái)呈現(xiàn)的密碼 。執(zhí)行登陸測(cè)試,登陸成功進(jìn)入如下界面(因?yàn)闆]有定義登陸頁面,所以會(huì)出現(xiàn)404):
自定義登陸邏輯業(yè)務(wù)描述我們的單點(diǎn)登錄系統(tǒng)最終會(huì)按照如下結(jié)構(gòu)進(jìn)行設(shè)計(jì)和實(shí)現(xiàn),例如:
我們?cè)趯?shí)現(xiàn)登錄時(shí),會(huì)在UI工程中,定義登錄頁面(login.html),然后在頁面中輸入自己的登陸賬號(hào),登陸密碼,將請(qǐng)求提交給網(wǎng)關(guān),然后網(wǎng)關(guān)將請(qǐng)求轉(zhuǎn)發(fā)到auth工程,登陸成功和失敗要返回json數(shù)據(jù),在這個(gè)章節(jié)我們會(huì)按這個(gè)業(yè)務(wù)逐步進(jìn)行實(shí)現(xiàn)
定義安全配置類修改SecurityConfig配置類,添加登錄成功或失敗的處理邏輯,例如:
package com.jt.auth.config;import com.fasterxml.jackson.databind.ObjectMapper;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;import org.springframework.security.web.authentication.AuthenticationFailureHandler;import org.springframework.security.web.authentication.AuthenticationSuccessHandler;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.io.PrintWriter;import java.util.HashMap;import java.util.Map;@Configurationpublic class SecurityConfig extends WebSecurityConfigurerAdapter {/**初始化密碼加密對(duì)象*/@Beanpublic BCryptPasswordEncoder passwordEncoder(){return new BCryptPasswordEncoder();}/**在這個(gè)方法中定義登錄規(guī)則* 1)對(duì)所有請(qǐng)求放行(當(dāng)前工程只做認(rèn)證)* 2)登錄成功信息的返回* 3)登錄失敗信息的返回* */@Overrideprotected void configure(HttpSecurity http) throws Exception {//關(guān)閉跨域工具h(yuǎn)ttp.csrf().disable();//放行所有請(qǐng)求http.authorizeRequests().anyRequest().permitAll();//登錄成功與失敗的處理http.formLogin().successHandler(successHandler()).failureHandler(failureHandler());}@Beanpublic AuthenticationSuccessHandler successHandler(){//return new AuthenticationSuccessHandler() {//@Override//public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {////}//}return (request,response,authentication) ->{//1.構(gòu)建map對(duì)象,封裝響應(yīng)數(shù)據(jù)Map<String,Object> map=new HashMap<>();map.put("state",200);map.put("message","login ok");//2.將map對(duì)象寫到客戶端writeJsonToClient(response,map);};}@Beanpublic AuthenticationFailureHandler failureHandler(){return (request,response, e)-> {//1.構(gòu)建map對(duì)象,封裝響應(yīng)數(shù)據(jù)Map<String,Object> map=new HashMap<>();map.put("state",500);map.put("message","login failure");//2.將map對(duì)象寫到客戶端writeJsonToClient(response,map);};}private void writeJsonToClient(HttpServletResponse response,Object object) throws IOException {//1.將對(duì)象轉(zhuǎn)換為json//將對(duì)象轉(zhuǎn)換為json有3種方案://1)Google的Gson-->toJson(需要自己找依賴)//2)阿里的fastjson-->JSON (spring-cloud-starter-alibaba-sentinel)//3)Springboot web自帶的jackson-->writeValueAsString (spring-boot-starter-web)//我們這里借助springboot工程中自帶的jackson//jackson中有一個(gè)對(duì)象類型為ObjectMapper,它內(nèi)部提供了將對(duì)象轉(zhuǎn)換為json的方法//例如:String jsonStr=new ObjectMapper().writeValueAsString(object);//3.將json字符串寫到客戶端PrintWriter writer = response.getWriter();writer.println(jsonStr);writer.flush();}}定義用戶信息處理對(duì)象在spring security應(yīng)用中底層會(huì)借助UserDetailService對(duì)象獲取數(shù)據(jù)庫信息,并進(jìn)行封裝,最后返回給認(rèn)證管理器,完成認(rèn)證操作,例如:
以上關(guān)于本文的內(nèi)容,僅作參考!溫馨提示:如遇健康、疾病相關(guān)的問題,請(qǐng)您及時(shí)就醫(yī)或請(qǐng)專業(yè)人士給予相關(guān)指導(dǎo)!
「愛刨根生活網(wǎng)」www.malaban59.cn小編還為您精選了以下內(nèi)容,希望對(duì)您有所幫助:- 快手小店怎么設(shè)置子賬號(hào)?如何設(shè)置分流? 快手小店電腦端怎么登錄
- 學(xué)信網(wǎng)登錄入口官網(wǎng) 研招網(wǎng)登錄入口官網(wǎng) 2021考研成績查詢官網(wǎng)地址入口
- 失敗的層次 寧北蘇清荷小說全文免費(fèi)閱讀
- 阿里云個(gè)人郵箱登錄 阿里企業(yè)版郵箱郵件延遲
- 解除微信登錄低版限制 微信開放者平臺(tái)第三方登錄
- 蘇炳添賽季首敗 蘇炳添為什么敗了?蘇炳添失敗原因是什么?
- 人臉識(shí)別一直失敗原因 王者人臉識(shí)別解決方案
- 夢(mèng)見失敗_周公解夢(mèng)夢(mèng)到失敗是什么意思_做夢(mèng)夢(mèng)見失敗好不好
- 找到健身計(jì)劃失敗的根源
- 教你零失敗的炸湯圓 油炸湯圓的做法
