日本免费全黄少妇一区二区三区-高清无码一区二区三区四区-欧美中文字幕日韩在线观看-国产福利诱惑在线网站-国产中文字幕一区在线-亚洲欧美精品日韩一区-久久国产精品国产精品国产-国产精久久久久久一区二区三区-欧美亚洲国产精品久久久久

單點登錄失敗解決措施 單點登錄框架有哪些

背景分析傳統(tǒng)的登錄系統(tǒng)中,每個站點都實現(xiàn)了自己的專用登錄模塊 。各站點的登錄狀態(tài)相互不認可,各站點需要逐一手工登錄 。例如:

單點登錄失敗解決措施 單點登錄框架有哪些


【單點登錄失敗解決措施 單點登錄框架有哪些】這樣的系統(tǒng),我們又稱之為多點登陸系統(tǒng) 。應用起來相對繁瑣(每次訪問資源服務都需要重新登陸認證和授權(quán)) 。與此同時,系統(tǒng)代碼的重復也比較高 。由此單點登陸系統(tǒng)誕生 。
單點登陸系統(tǒng)單點登錄,英文是 Single Sign On(縮寫為 SSO) 。即多個站點共用一臺認證授權(quán)服務器,用戶在其中任何一個站點登錄后,可以免登錄訪問其他所有站點 。而且,各站點間可以通過該登錄狀態(tài)直接交互 。例如:

單點登錄失敗解決措施 單點登錄框架有哪些


快速入門實踐工程結(jié)構(gòu)如下基于資源服務工程添加單點登陸認證和授權(quán)服務,工程結(jié)構(gòu)定義如下:


單點登錄失敗解決措施 單點登錄框架有哪些


創(chuàng)建認證授權(quán)工程
單點登錄失敗解決措施 單點登錄框架有哪些


添加項目依賴<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)建項目配置文件在sca-auth工程中創(chuàng)建bootstrap.yml文件,例如:
server:port: 8071spring:application:name: sca-authcloud:nacos:discovery:server-addr: localhost:8848config:server-addr: localhost:8848添加項目啟動類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);}}啟動并訪問項目項目啟動時,系統(tǒng)會默認生成一個登陸密碼,例如:

單點登錄失敗解決措施 單點登錄框架有哪些



單點登錄失敗解決措施 單點登錄框架有哪些


其中,默認用戶名為user,密碼為系統(tǒng)啟動時,在控制臺呈現(xiàn)的密碼 。執(zhí)行登陸測試,登陸成功進入如下界面(因為沒有定義登陸頁面,所以會出現(xiàn)404):

單點登錄失敗解決措施 單點登錄框架有哪些


自定義登陸邏輯業(yè)務描述我們的單點登錄系統(tǒng)最終會按照如下結(jié)構(gòu)進行設計和實現(xiàn),例如:

單點登錄失敗解決措施 單點登錄框架有哪些


我們在實現(xiàn)登錄時,會在UI工程中,定義登錄頁面(login.html),然后在頁面中輸入自己的登陸賬號,登陸密碼,將請求提交給網(wǎng)關(guān),然后網(wǎng)關(guān)將請求轉(zhuǎn)發(fā)到auth工程,登陸成功和失敗要返回json數(shù)據(jù),在這個章節(jié)我們會按這個業(yè)務逐步進行實現(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 {/**初始化密碼加密對象*/@Beanpublic BCryptPasswordEncoder passwordEncoder(){return new BCryptPasswordEncoder();}/**在這個方法中定義登錄規(guī)則* 1)對所有請求放行(當前工程只做認證)* 2)登錄成功信息的返回* 3)登錄失敗信息的返回* */@Overrideprotected void configure(HttpSecurity http) throws Exception {//關(guān)閉跨域工具http.csrf().disable();//放行所有請求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對象,封裝響應數(shù)據(jù)Map<String,Object> map=new HashMap<>();map.put("state",200);map.put("message","login ok");//2.將map對象寫到客戶端writeJsonToClient(response,map);};}@Beanpublic AuthenticationFailureHandler failureHandler(){return (request,response, e)-> {//1.構(gòu)建map對象,封裝響應數(shù)據(jù)Map<String,Object> map=new HashMap<>();map.put("state",500);map.put("message","login failure");//2.將map對象寫到客戶端writeJsonToClient(response,map);};}private void writeJsonToClient(HttpServletResponse response,Object object) throws IOException {//1.將對象轉(zhuǎn)換為json//將對象轉(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中有一個對象類型為ObjectMapper,它內(nèi)部提供了將對象轉(zhuǎn)換為json的方法//例如:String jsonStr=new ObjectMapper().writeValueAsString(object);//3.將json字符串寫到客戶端PrintWriter writer = response.getWriter();writer.println(jsonStr);writer.flush();}}

推薦閱讀