国产chinesehdxxxx野外,国产av无码专区亚洲av琪琪,播放男人添女人下边视频,成人国产精品一区二区免费看,chinese丰满人妻videos

3.9.單點登錄相關

2023-07-03 17:08 更新
在BDF2-CORE當中,默認就提供了對CASSSO支持,如果我們已經有了現成的,并且已在CAS客戶端Server上配置好相關證書信息,那么就可以通過修改BDF2-CORE模塊中的相關屬性,快速將BDF2應用接入到當前的CAS Server當中,具體需要修改的屬性如下:
屬性名類型默認值描述示例
bdf2.casLoginUrlString/cas.login.d當采用CAS進行SSO登錄時,設置CASServer的登錄頁面的URL地址
bdf2.casLoginUrl=https://www.bstek.com:8443/cas-server/login
bdf2.casServerUrlString/cas.server設置CASServer的URL地址
bdf2.casServerUrl=https://www.bstek.com:8443/cas-server
bdf2.casClientServerUrlStringhttp://localhost:8080/bdf2-test
設置要采用CASSSO認證的客戶端應用的地址
bdf2.casClientServerUrl=http://localhost:8080/bdf2-test
bdf2.logoutSuccessURL
String
/bdf2.core.view.response.LogoutSuccess.d
主框架右上角退出系統(tǒng)快捷圖標點擊時,退出系統(tǒng)成功后跳轉的地址 ,這里設置為CASSSO的logout,表示在系統(tǒng)內部退出完成(銷毀Se
ssion之類操作完成)之后,再跳轉到CASSSO的logout進行SSO的登出操作。
bdf2.logoutSuccessURL=https://www.bstek.com:8443/cas-server/logout
bdf2.authenticationType
String
form
這個屬性目標支持兩個值,一個就是默認的form,表示采用BDF2系統(tǒng)提供的登錄表單登錄;另一個就是cas,表示采用CASSSO登錄。
bdf2.authenticationType=cas
這里需要強調的是BDF2中對于CASSSO的支持,我們做了完善的功能測試,以保證其不會有問題,所以如果您要使用這一功能,請確保您的CASServer配置正確,確保部署B(yǎng)DF2應用的客戶端Server對于 CAS Server證書配置正確,這樣才能保證能把BDF2中CASSSO支持用起來。一旦出現問題,多數都是您的環(huán)境配置問題,與BDF2對CAS SSO支持無關,一句話,您一定要能熟練使用CASServer來構建SSO環(huán)境,不能一知半解,邊猜邊做。
如果您需要采用其它的登錄方式,這種登錄既非系統(tǒng)提供的表單登錄,也非CAS的SSO(可能是其它類型的SSO),那么就需要通過下面的兩種方
式實現。 

第一種方法,就是從BDF2-CORE-1.0.1開始提供的通過實現IRetrivePreAuthenticatedUser方式實現,該接口的源碼如下所示:
IRetrivePreAuthenticatedUser接口源碼
package com.bstek.bdf2.core.security;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.bstek.bdf2.core.business.IUser;
/**
 * @author Jacky.gao
 * @since 2013年7月5日
 * 獲取通過其它方式已經登錄的用戶信息,比如通過SSO等
 */
public interface IRetrivePreAuthenticatedUser {
 /**
 *
根據給出的request與response對象,取出當前已通過其它途徑預認證的IUser對象,如果返回null表示預認證未通過
,系統(tǒng)將不會處理
 * @param request
 * @param response
 * @return 返回已被預認證通過的IUser對象
 * @throws ServletException
 */
 IUser retrive(HttpServletRequest request,HttpServletResponse response) throws ServletException;
}
可以看到,這個接口當中只有一個retrive方法,該方法的作用就是要接口實現類返回當前已預認證通過的IUser接口實現類對象。該接口編寫完成后需要配置到Spring環(huán)境當中,作為一個標準的SpringBean,系統(tǒng)會自動檢測到該接口實現類,這樣用戶未登錄的情況下訪問某個需要登錄才能訪問的頁面時會自動調用這個接口實現類,返回已認證用戶信息,從而完成用戶自動登錄的動作。下面的是一個非常簡單的IRetrivePreAuthenticatedUser接口實現類,其源碼如下:
測試IRetrivePreAuthenticatedUser接口實現類
package test;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import com.bstek.bdf2.core.business.IUser;
import com.bstek.bdf2.core.model.DefaultUser;
import com.bstek.bdf2.core.security.IRetrivePreAuthenticatedUser;
import com.bstek.bdf2.core.service.IDeptService;
import com.bstek.bdf2.core.service.IGroupService;
import com.bstek.bdf2.core.service.IPositionService;
@Component
public class TestRetrivePreAuthenticatedUser implements IRetrivePreAuthenticatedUser {
 @Autowired
 @Qualifier(IDeptService.BEAN_ID)
 private IDeptService deptService;
 @Autowired
 @Qualifier(IPositionService.BEAN_ID)
 private IPositionService positionService;
 @Autowired
 @Qualifier(IGroupService.BEAN_ID)
 private IGroupService groupService;
 public IUser retrive(HttpServletRequest request,
 HttpServletResponse response) throws ServletException {
 //從其它源讀取登錄信息,比如某些硬件卡中讀取登錄信息等
 DefaultUser user=new DefaultUser("admin");
 user.setCompanyId("bstek");
 //為登錄成功的用戶設置所在部門、崗位及群組信息
 user.setDepts(deptService.loadUserDepts(user.getUsername()));
 user.setPositions(positionService.loadUserPositions(user.getUsername()));
 user.setGroups(groupService.loadUserGroups(user.getUsername()));
 //為登錄成功的用戶設置所在部門、崗位及群組信息結束
 return user;
 }
}

第二種方法就是實現ISecurityInterceptor接口。
在BDF2當中,除了通過IRetrivePreAuthenticatedUser接口實現獲取預認證的登錄用戶外,還可以通過實現名為ISecurityInterceptor接口,獲取預認證的登錄用戶,完成用戶登錄認證。該接口的實現類,同樣也需要配置到Spring環(huán)境當中,該接口的源碼如下:
ISecurityInterceptor接口源碼
package com.bstek.bdf2.core.security;
import org.springframework.security.web.context.HttpRequestResponseHolder;
/**
 * 一個供開發(fā)人員使用的在登錄、認證之前或之后或失敗后需要進行業(yè)務處理的接口,<br>
 *
開發(fā)人員可以根據需要,有選擇的覆蓋該類中的某個方法,比如需要在用戶登錄前進行一些處理,那么就可覆蓋其中的
beforeLogin方法,<br>
 *
依次類推,使用時,將實現類配置到spring當中即可,系統(tǒng)運行時會自動掃描該抽象類實現的存在,如果有就會加載處
理
 * @author jacky.gao
 * @since 2013-1-22
 */
public interface ISecurityInterceptor {
 /**
 * 用戶登錄系統(tǒng)之前進行的處理動作
 * @param holder 一個用于包裝HttpRequest/HttpResponse的對象
 */
 void beforeLogin(HttpRequestResponseHolder holder);
 
 /**
 * 用戶登錄系統(tǒng)成功之后進行的處理動作
 * @param holder 一個用于包裝HttpRequest/HttpResponse的對象
 */
 void loginSuccess(HttpRequestResponseHolder holder);
 
 /**
 * 用戶登錄系統(tǒng)認證失敗時需要處理的動作
 * @param holder 一個用于包裝HttpRequest/HttpResponse的對象
 */
 void loginFailure(HttpRequestResponseHolder holder);
 
 /**
 * 用戶在訪問系統(tǒng)資源時(比如訪問某URL),系統(tǒng)安全模塊對用戶進行授權之前需要處理的動作
 * @param holder 一個用于包裝HttpRequest/HttpResponse的對象
 */
 void beforeAuthorization(HttpRequestResponseHolder holder);
 /**
 * 用戶在訪問系統(tǒng)資源時(比如訪問某URL),系統(tǒng)安全模塊對用戶進行授權成功之后需要處理的動作
 * @param holder 一個用于包裝HttpRequest/HttpResponse的對象
 */
 void authorizationSuccess(HttpRequestResponseHolder holder);
 /**
 * 用戶在訪問系統(tǒng)資源時(比如訪問某URL或某模塊),系統(tǒng)安全模塊對用戶進行授權失敗之后需要處理的動作
 * @param holder 一個用于包裝HttpRequest/HttpResponse的對象
 */
 void authorizationFailure(HttpRequestResponseHolder holder);
}
對于我們上述預認證需求,就可以通過編寫這個接口的實現類來實現。因為這個接口當中包含的方法較多,一般情況下,我們只需要實現這個接口當中的一個方法即可,所以我們可以通過擴展系統(tǒng)當中提供的已實現了ISecurityInterceptor接口的SecurityInterceptorAdapter類來實現,這個類中提供了關于ISecurityInterceptor接口所有方法的空實現,所以我們在擴展這個類時只需要覆蓋需要的方法即可。比如我們下面的實現類:
DemoSecurityInterceptor類源碼
package test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.web.context.HttpRequestResponseHolder;
import org.springframework.stereotype.Component;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.model.DefaultUser;
import com.bstek.bdf2.core.security.SecurityInterceptorAdapter;
import com.bstek.bdf2.core.service.IDeptService;
import com.bstek.bdf2.core.service.IGroupService;
import com.bstek.bdf2.core.service.IPositionService;
@Component
public class DemoSecurityInterceptor extends SecurityInterceptorAdapter {
 @Autowired
 @Qualifier(IDeptService.BEAN_ID)
 private IDeptService deptService;
 @Autowired
 @Qualifier(IPositionService.BEAN_ID)
 private IPositionService positionService;
 @Autowired
 @Qualifier(IGroupService.BEAN_ID)
 private IGroupService groupService;
 @Override
 public void beforeAuthorization(HttpRequestResponseHolder holder) {
 if(ContextHolder.getLoginUser()==null){
 //表示未登錄
 //從其它源讀取登錄信息,比如某些硬件卡中讀取登錄信息等
 DefaultUser user=new DefaultUser("admin");
 user.setCompanyId("bstek");
 //為登錄成功的用戶設置所在部門、崗位及群組信息
 user.setDepts(deptService.loadUserDepts(user.getUsername()));
 user.setPositions(positionService.loadUserPositions(user.getUsername()));
 user.setGroups(groupService.loadUserGroups(user.getUsername()));
 //為登錄成功的用戶設置所在部門、崗位及群組信息結束
 
 //這里的IUser應該是從其它源里讀取到的經過認證的合法的用戶對象,再轉換成IUser對象實例
 //接下來需要將這個user對象放置到session當中及Spring Security的環(huán)境當中,以告訴系統(tǒng)已成功登錄
 this.registerLoginInfo(user, holder);
 }
 }
}
上述代碼當中比較關鍵的是最后一句,這個registerLoginInfo方法位于SecurityInterceptorAdapter類當中,它可以把認證的用戶對象放到系統(tǒng)環(huán)境當中,用以標明用戶已登錄。

兩種獲取預認證用戶的方法比較綜合比較上述兩種方法,我們推薦使用第一種,第一種方法就是為獲取已認證的用戶,實現自動登錄而準備的,所以它看起來更加自然,與系統(tǒng)的結合性也更好。

還有一種情況,那就是可能我們的系統(tǒng)當中存在多種登錄方式,可能根據用戶訪問的URL后面的參數來決定跳轉到哪個登錄頁面(我也不清楚什么時候會有這種變態(tài)需求),如果是這樣,上面的代碼就需要調整成下面的樣子:
修改后的SecurityInterceptor實現類
package test;
import org.springframework.security.web.context.HttpRequestResponseHolder;
import org.springframework.stereotype.Component;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.security.SecurityInterceptorAdapter;
@Component
public class DemoSecurityInterceptor extends SecurityInterceptorAdapter {
 @Override
 public void beforeAuthorization(HttpRequestResponseHolder holder) {
 if(ContextHolder.getLoginUser()==null){
 //表示未登錄
 String loginType=holder.getRequest().getParameter("loginType");
 if(loginType!=null && loginType.equals("abc")){
 throw new MyLoginException();
 }
 if(loginType!=null && loginType.equals("def")){
 throw new MyLogin1Exception();
 }
 }
 }
}
這里的MyLoginException代碼如下:
MyLoginException
package test;
public class MyLoginException extends RuntimeException {
}
MyLogin1Exception代碼與上述基本一樣,這里不再羅列了。

從上述的代碼中可以看到,一旦發(fā)現要采用某種登錄方式,我們就拋一個特定異常(比如MyLoginException等),接下來我們就需要來編寫一個IExceptionHandler接口實現類,用于捕獲我們之前拋出的異常,一旦捕獲,我們可以進行頁面跳轉等相關我們需要的操作(比如跳轉到我們需要的登錄頁面等),我們的IExceptionHandler接口實現類代碼如下:
ExceptionHandler實現類
package test;
import org.springframework.security.web.context.HttpRequestResponseHolder;
import org.springframework.stereotype.Component;
import com.bstek.bdf2.core.exception.IExceptionHandler;
@Component
public class DemoExceptionHandler implements IExceptionHandler {
 public void handle(HttpRequestResponseHolder holder,
 Throwable exception) {
 try{
 if(exception instanceof MyLoginException){
 holder.getResponse().sendRedirect("/login.jsp");
 }
 if(exception instanceof MyLogin1Exception){
 holder.getResponse().sendRedirect("/login.html");
 }
 }catch(Exception ex){
 throw new RuntimeException(ex);
 }
 }
 public boolean support(Throwable exception) {
 return ((exception instanceof MyLoginException) || (exception instanceof MyLogin1Exception));
 }
}
上述代碼比較簡單,這里就不再解釋了。同樣這個實現編寫完成之后需要配置到Spring環(huán)境當中。利用這么一種機制,大家可以發(fā)揮想象,類似的需求都可以通過這一功能機制實現,具體就不再啰嗦了。


以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號