gpt4 book ai didi

java - 如何使用 AuthenticationSuccessHandler 将用户重定向到页面?

转载 作者:行者123 更新时间:2023-11-30 06:51:33 24 4
gpt4 key购买 nike

我实现了一个成功处理程序,如果用户是管理员用户,该处理程序会将用户重定向到特定页面。

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 方法也被调用。但用户不会被重定向。相反,由于我的 Controller 中的 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>

如何实现重定向,以便 Controller 中的 index 方法不会被调用?

最佳答案

由于您已经在重定向 url 中提供了请求上下文。您需要在重定向策略中设置 contextRelative = true,以便它忽略最终重定向 url 中的请求上下文。

DefaultRedirectStrategy REDIRECT_STRATEGY = new DefaultRedirectStrategy();
REDIRECT_STRATEGY.setContextRelative(true);

关于java - 如何使用 AuthenticationSuccessHandler 将用户重定向到页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42676961/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com