2017-03-08 35 views
1

私は管理者ユーザーの場合、特定のページにユーザーをリダイレクトする成功ハンドラを実装しました。AuthenticationSuccessHandlerを使用してユーザーをページにリダイレクトする方法はありますか?

public class MaunaKeaAuthenticationSuccessHandler implements AuthenticationSuccessHandler { 

    private static final RedirectStrategy REDIRECT_STRATEGY = new DefaultRedirectStrategy(); 

    @Override 
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { 
    if (isMkeaAdmin(authentication)) { 
     REDIRECT_STRATEGY.sendRedirect(request, response, "/admin/submission-queue.html"); 
    } 
    } 

    private static boolean isMkeaAdmin(Authentication authentication) { 
    if (!(authentication.getPrincipal() instanceof AccountUserDetailsAdapter)) { 
     return false; 
    } 
    AccountUserDetailsAdapter userDetails = (AccountUserDetailsAdapter) authentication.getPrincipal(); 
    return userDetails.getAccount().getRoles().contains("MKEA_Admin"); 
    } 

} 

onAuthenticationSuccessメソッドが呼び出され、そしてsendRedirect方法はあまりにも呼び出されます。しかし、ユーザーはリダイレクトされません。代わりに、コントローラのindexメソッドのおかげで、デフォルトページの/index.htmlに送信されます。

@PreAuthorize("hasAnyAuthority('PERM_CAN_LOGIN')") 
@RequestMapping(value="/index.html") 
public String index(HttpServletRequest request, Model model) { 
    WebUtils.activeNav(NAV_HOME); 
    return viewRegistry.getPath("main.index"); 
} 

ここは私のspring-security.xmlの一部です。

<beans:bean id="casFilter" class="org.springframework.security.cas.web.CasAuthenticationFilter" 
    p:authenticationManager-ref="authenticationManager"> 
    <beans:property name="authenticationSuccessHandler"> 
    <beans:bean class="gov.ehawaii.maunakea.security.MaunaKeaAuthenticationSuccessHandler"/> 
    </beans:property> 
</beans:bean> 

私のコントローラでindexメソッドが呼び出されないようにどのように私は、リダイレクトを実装していますか?

答えて

1

リダイレクトURLにリクエストコンテキストを既に指定しているため、リダイレクト戦略でcontextRelative = trueを設定して、最終リダイレクトURLのリクエストコンテキストを無視する必要があります。

DefaultRedirectStrategy REDIRECT_STRATEGY = new DefaultRedirectStrategy(); 
REDIRECT_STRATEGY.setContextRelative(true); 
関連する問題