当前位置: 首页 > 知识库问答 >
问题:

集成Spring SAML和Oauth2,用于REST API访问的令牌

羊舌源
2023-03-14

为REST API访问集成Spring SAML和Oauth2令牌

使用Spring SAML扩展,我能够配置SAML身份验证并能够将断言返回给SP,请点击链接,

现在,它返回到这个“/登陆”,并在SAMLUserDetailsService和SAMLAuthenticationProvider中获取断言和身份验证对象,在SAMLUserDetailsService中填充用户详细信息对象。

@Bean
    public SavedRequestAwareAuthenticationSuccessHandler successRedirectHandler() {
        SavedRequestAwareAuthenticationSuccessHandler successRedirectHandler =
                new SavedRequestAwareAuthenticationSuccessHandler();
        successRedirectHandler.setDefaultTargetUrl("/landing");
        return successRedirectHandler;
    }

现在我的问题是,如何生成Auth token?从这一点来说,没有太多的细节可用,到目前为止我尝试,创建自定义过滤器,拦截“/landing”并尝试修改URL到/oauth/token?grant _ type = urn:IETF:params:oauth:grant-type:Sam L2-bearer

我的 ResourceServerConfiguration.java

@Configuration
@EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

    private static final String RESOURCE_ID = "my_rest_api";

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
        resources.resourceId(RESOURCE_ID).stateless(false);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.
        anonymous().disable()
        .requestMatchers().antMatchers("/user/**")
        .and().authorizeRequests()
        .antMatchers("/user/**").access("hasRole('ADMIN')")
        .and().exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler());
    }

}

我的AuthorizationServerConfiguration.java

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

    private static String REALM="MY_OAUTH_REALM";

    @Autowired
    private TokenStore tokenStore;

    @Autowired
    private UserApprovalHandler userApprovalHandler;

    @Autowired
    @Qualifier("authenticationManagerBean")
    private AuthenticationManager authenticationManager;

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

        clients.inMemory()
            .withClient("my-trusted-client")
            .authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit")
            .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
            .scopes("read", "write", "trust")
            .secret("secret")
            .accessTokenValiditySeconds(120).//Access token is only valid for 2 minutes.
            refreshTokenValiditySeconds(600);//Refresh token is only valid for 10 minutes.
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.tokenStore(tokenStore).userApprovalHandler(userApprovalHandler)
                .authenticationManager(authenticationManager);
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
        oauthServer.realm(REALM+"/client");
    }

}

谁能建议一下,如何集成它,以便在IDP中完成身份验证,并在IDP成功声明(完成授权部分)后返回到SP,这将生成一个授权访问、刷新令牌,该令牌用户能够访问api。有人能提供某种解决方案吗...

共有1个答案

游鸣
2023-03-14

不久前,我遇到了同样的挑战,在弄清楚之后,我写了一篇文章(不能在这里发表…)。基本上,您的oAuth授权服务器是一个“桥梁”,它可以使您的SAML后端适应oAuth,反之亦然。。。在此处查找:

如何将Spring oAuth与Spring SAML集成

GitHub中还有一个包含所有源代码的repo,例如:

https://github.com/OhadR/spring-oAuth2-SAML-integration

我知道它只是链接,但文章太长太详细了。

 类似资料:
  • 我得到的令牌是: http://localhost:8080/servicesmem/oauth/token?username=myuser&password=mypassword&grant_type=password&scope=read,write,trust 我得到: 我得到: 我使用头:Authorization Bearer ACCESS_TOKEN,但我得到了同样的错误。我错过了什么

  • 我有一些问题,当我想要获得“访问令牌”,我使用Spring Security在我的项目 WebSecurity配置 我在内存中设置了user以便在该类中进行身份验证,并且我使用的是内存令牌 我正确地发送了grant_type,client_id,client_secret,username和password,但没有在API上响应“访问令牌

  • 我感到困惑的是,在向授权服务器发送授权请求时,似乎没有标准的方法来指定访问令牌的受众。 OAuth2将访问令牌指定为不透明字符串;规范中只有一处提到了“观众”,即访问令牌可以是“观众限制的”。许多最近的授权服务器实现似乎产生了JWT访问令牌,JWT指定了受众(aud)声明。 据我所知:-Auth0使用“audience”参数-Connect2id使用“resource”参数-Identity Se

  • 我想使用上传一个pdf文件到Google Drive,用于自动测试目的。 我已经在Google云平台上创建了一个帐户(获得了客户端ID和机密),并启用了Google Drive API。 谢了。

  • 当且仅当用户在发出OAuth2请求时登录到LinkedIn时,它才起作用。 如果用户未登录,则会遇到错误。 我们的行动顺序: 成功获取新的访问令牌 使用访问令牌,发布到apiendpoint 之后,我们将收到一份401,内容如下: 有时,经过一段时间后,使用相同的访问令牌重试会得到200。有时不会。 如果用户在“401期间”登录LinkedIn,那么之前获取的访问令牌就会神奇地开始工作。 我不知道

  • 需要对Oauth2客户端的集成测试的帮助。 设置: 具有受保护UI和API的客户端 完成所有密码验证并检索访问令牌的身份验证服务器 集成测试: 放心用于终点测试 在实现Oauth2之前,测试工作良好 Ole测试示例: 问题: 如何使此测试再次工作? 应如何更改res-assured设置以支持OAuth2? 是否需要模拟身份验证服务器,或者是否可以注入/mock安全上下文?

  • 我正在使用一个Spring Boot+Spring Security OAuth2应用程序,我相信它的灵感来自Dave Syer的示例。应用程序被配置为OAuth2授权服务器,具有使用资源所有者密码凭据流的单个公共客户端。成功的令牌被配置为JWT。 公共Angular客户机向/oauth/token发送一个POST请求,该请求带有包含客户机id和秘密的基本身份验证头(这是让客户机进行身份验证的最简