我試圖在用戶在Spring Security中登錄後實施所需的操作屏幕?我要求用戶必須執行以完成表單(更改密碼,接受使用條款等),然後一旦用戶完成該操作,他就可以使用應用程序的其餘部分。我在使用Spring Security流的登錄屏幕上使用Spring OAuth2。Spring安全和登錄後需要採取措施
到目前爲止,我已嘗試使用http.formLogin().successHandler()
自定義實現SavedRequestAwareAuthenticationSuccessHandler
,它檢測用戶是否有必要的操作,然後當用戶填寫表單時將用戶重定向到頁面,但問題在於如果用戶導航離開該頁面,他將登錄到應用程序,並可以在不跳過表單的情況下使用它。但是我想要做的是阻止用戶建立會話,直到完成「需要操作」表單爲止。一旦完成,用戶應該自動登錄(例如,如果用戶要求只同意使用條款,他應該在第二次輸入密碼的情況下登錄)
這是我的代碼到目前爲止,自定義處理程序:
public class CustomLoginSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
@Autowired
UserService userService;
public final static String TARGET_URL_SESSION_ATTR_NAME = "target-url";
public CustomLoginSuccessHandler(String defaultTargetUrl) {
setDefaultTargetUrl(defaultTargetUrl);
}
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
HttpSession session = request.getSession();
AuthorityUser authorityUser = (AuthorityUser)authentication.getPrincipal();
String userId = authorityUser.getUserId();
User u = userService.getById(userId);
Boolean changeRequiredDob = u.getChangeRequiredDob();
Boolean changeRequiredPwd = u.getChangeRequiredPwd();
Boolean changeRequiredTou = u.getChangeRequiredTou();
if(changeRequiredDob || changeRequiredPwd || changeRequiredTou){
String targetUrl = determineTargetUrl(request, response);
session.setAttribute(TARGET_URL_SESSION_ATTR_NAME, targetUrl);
getRedirectStrategy().sendRedirect(request, response, "/action-required");
} else {
super.onAuthenticationSuccess(request, response, authentication);
}
}
}
然後,一旦它成功地完成我重定向用戶TARGET_URL_SESSION_ATTR_NAME
已存儲到會話中。
這將是也有利於知道如何建立的會話期間檢測和重定向用戶到所需的操作屏幕(如果用戶登錄,後來當他登錄管理員設置動作所需的標誌在他的賬戶)。
在一個普通的過濾器實現這個邏輯(映射到/ *)同時滿足您的requrements:1)用戶將無法導航遠離它,以及b)用戶將自動「登錄」,或而是通過了。只有您可能需要執行的其他事情是成功重定向。 – chimmi
是否有一個實際的原因,您不想馬上創建會話? – chimmi