簡體   English   中英

使用Spring Security進行特殊登錄

[英]Special Login with Spring Security

我目前工作的公司有一種特殊的身份驗證過程。

實際上,一些用戶可以擁有相同的登錄名和密碼。 因此,為了識別它們,用戶必須第二次發送他的電子郵件。 但同樣,在少數情況下,幾個用戶可能會擔心,然后,用戶必須給他的公司ID進行完全身份驗證。

所以,我的問題是,在某些情況下,身份驗證過程無法在一個步驟中完成 ,這是Spring Security處理的大多數應用程序的默認行為。

所以我的問題是:使用Spring Security實現這種特殊登錄過程的最簡單方法是什么?

提前致謝。

我不得不做一個類似的兩步認證過程。 這聽起來很簡單,但找到合適的注射位置和正確的覆蓋方法並不容易。 基本思想是為中間身份驗證(電子郵件檢查)提供另一個角色,然后在確認電子郵件后授予完全訪問權限。

希望下面的代碼提供了一些關於如何為您的場景解決它的好提示。

創建自定義UserDetailsChecker以處理身份驗證后檢查。 這是確定用戶角色的位置。

public class CustomPostAuthenticationChecks implements UserDetailsChecker {

        public void check(UserDetails userDetails) {

            CustomUser customUser = (CustomUser) userDetails;
            if (customUser.needsEmailAuthentication()) {
                // Get rid of any authorities the user currently has
                userDetails.getAuthorities().clear();
                // Set the new authority, only allowing access to the 
                // email authentication page.
                userDetails.getAuthorities().add(new GrantedAuthorityImpl("ROLE_NEEDS_EMAIL_AUTH"));
            } else {
                userDetails.getAuthorities().add(new GrantedAuthorityImpl("ROLE_AUTHORIZED_USER"));
            }
    }

創建自定義AuthenticationSuccessHandler。 此類根據用戶的角色將用戶發送到正確的URL。

public class CustomAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

@Override
protected String determineTargetUrl(HttpServletRequest request, HttpServletResponse response) {

    String targetUrl = null;

    SecurityContext securityContext = SecurityContextHolder.getContext();

    Collection<GrantedAuthority> authorities = securityContext.getAuthentication().getAuthorities();

    if (authorities.contains(new GrantedAuthorityImpl("ROLE_NEEDS_EMAIL_AUTH"))) {
        targetUrl = "/authenticate";
    } else if (authorities.contains(new GrantedAuthorityImpl("ROLE_AUTHORIZED_USER"))) {
        targetUrl = "/authorized_user_url";
    } else {
        targetUrl = super.determineTargetUrl(request, response);
    }

    return targetUrl;
}

在對用戶的電子郵件地址進行身份驗證后,您需要授予用戶對該應用程序的完全訪問權限:

public void grantUserAccess(User user) {
    SecurityContext securityContext = SecurityContextHolder.getContext();
    Authentication auth = securityContext.getAuthentication();
    user.getAuthorities().clear();
    user.getAuthorities().add(new GrantedAuthorityImpl("ROLE_AUTHORIZED_USER"));
    Authentication newAuth = new UsernamePasswordAuthenticationToken(user, auth.getCredentials(), user.getAuthorities());
    securityContext.setAuthentication(newAuth);
}

定義自定義身份驗證提供程序以注冊CustomPostAuthenticationChecks:

<security:authentication-manager>
    <security:authentication-provider ref="customAuthenticationProvider" />
</security:authentication-manager>

<bean id="customAuthenticationProvider" class="YourAuthenticationProvider">
    <property name="postAuthenticationChecks">
        <bean class="CustomPostAuthenticationChecks"/>
    </property>
</bean>

如果您使用標准的form-login標記,則可以輕松定義自定義AuthenticationSuccessHandler:

<security:form-login authentication-success-handler-ref="customAuthenticationSuccessHandler">

...

<bean id="customAuthenticationSuccessHandler" class="CustomAuthenticationSuccessHandler"/>

/ authenticate URL添加新的角色攔截規則,因此只有需要更多身份驗證的用戶才能訪問該頁面。

<security:intercept-url pattern="/authenticate/**" access="hasRole('ROLE_NEEDS_EMAIL_AUTH')" />
<security:intercept-url pattern="/authorized_user_url/**" access="hasRole('ROLE_AUTHORIZED_USER')" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM