首页 > 解决方案 > 如何使用 Spring 和 SAP Cloud Platform 执行 SAML 身份验证

问题描述

我正在尝试按照此处所述实施程序化身份验证https://help.sap.com/viewer/65de2977205c403bbc107264b8eccf4b/Cloud/en-US/e637f62abb571014857cb0232adc43a7.html#loio778d8987e7714376977c190f6df379ad

但是,我不想手动实现 servlet,我想改用 Spring。

这是我的身份验证提供程序:

import com.sap.security.auth.login.LoginContextFactory;

@Component
public class FormAuthenticationProvider extends AbstractJaasAuthenticationProvider {

    private final static String FORM = "FORM";
    {
        setAuthorityGranters(new AuthorityGranter[] {
            new AuthorityGranter() {

                @Override
                public Set<String> grant(Principal principal) {
                    // TODO Auto-generated method stub
                    return null;
                }
            }
        });
    }

    @Override
    protected LoginContext createLoginContext(CallbackHandler handler) throws LoginException {
        // TODO Auto-generated method stub
        return LoginContextFactory.createLoginContext(FORM);
    }

}

和 web 安全配置器适配器:

public static class FormWebSecurityConfig extends WebSecurityConfigurerAdapter{

    @Autowired
    private FormAuthenticationProvider formAuthenticationProvider;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(formAuthenticationProvider);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable() //HTTP with Disable CSRF
                .antMatcher("/form")
                .authorizeRequests()
                    .anyRequest().authenticated()
                .and()
                .formLogin();
    }
}

通过这个实现,当我想要重定向到 SAP ID 服务时,我会被重定向到 Java 登录表单。

成功重定向到 SAP ID 服务的经典web.xml方法<auth-method>FORM</auth-method>,我认为 SpringformLogin()是等价的。

我究竟做错了什么?

标签: javaspringspring-bootsamlsap-cloud-platform

解决方案


推荐阅读