![](/img/trans.png)
[英]Using Spring, how can I manually fire AuthenticationSuccessHandler
[英]How to redirect the user to a page using 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
方法不會被調用?
由於您已經在重定向 url 中提供了請求上下文。您需要在重定向策略中設置 contextRelative = true,以便它忽略最終重定向 url 中的請求上下文。
DefaultRedirectStrategy REDIRECT_STRATEGY = new DefaultRedirectStrategy();
REDIRECT_STRATEGY.setContextRelative(true);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.