如何在 Spring Security 中的重定向 /cas/login 回调中设置位置

问题描述 投票:0回答:1

Spring CAS会重定向回被禁止的url, 作为前端发送rest api作为ajax请求,所以Spring Security默认重定向不起作用
我必须发送 SSO url

cas_login_url/service=?my_service_cas_login
作为响应。 前端会执行
window.location.href = cas_login_url/service=?my_service_cas_login
跳转到 SSO 登录页面。
它将重定向到
"/"
,因为我可以看到位置是Spring cas validate api中的
"/"
,即/cas/login?ticket=TS*******

如果我想将位置设置为自定义url,我该怎么做,我应该在哪里传递参数?

spring-security cas
1个回答
0
投票

这个问题已经很老了,但万一有人仍然想知道:您需要使用 AuthenticationSuccessHandler (像任何身份验证过滤器一样)配置您的 CasAuthenticationFilter

例如,如果您只想重定向到网址“/welcome”:

filter.setAuthenticationSuccessHandler(new SimpleUrlAuthenticationSuccessHandler("/welcome"));

您可以将 spring bean 添加到任何带有 @Configuration 注释的类文件中:

@Bean
public CasAuthenticationFilter casAuthenticationFilter(AuthenticationManager authenticationManager, ServiceProperties casServiceProperties) throws Exception {
        CasAuthenticationFilter filter = new CasAuthenticationFilter();
        filter.setFilterProcessesUrl("/login/cas");
        filter.setAuthenticationManager(authenticationManager);
        filter.setServiceProperties(casServiceProperties);
        filter.setAuthenticationSuccessHandler(new SimpleUrlAuthenticationSuccessHandler("/welcome"));
        return filter;
    }

虽然如果采用这条路线,您不应该在代码中硬编码值“/welcome”,您可以在 application.properties 中使用值

© www.soinside.com 2019 - 2024. All rights reserved.