![](/img/trans.png)
[英]OAuth2 with Spring Boot REST application - cannot access resource with token
[英]Integrate Spring SAML with Oauth2 , token for REST API access
將Spring SAML與Oauth2令牌集成以進行REST API訪問
使用Spring SAML擴展程序,我可以配置SAML身份驗證,並可以將Assertion返回到SP,然后單擊鏈接 ,
現在,它返回此“ /着陸”,並在SAMLUserDetailsService和SAMLAuthenticationProvider中獲取斷言和身份驗證對象,在SAMLUserDetailsService中填充UserDetails對象。
@Bean
public SavedRequestAwareAuthenticationSuccessHandler successRedirectHandler() {
SavedRequestAwareAuthenticationSuccessHandler successRedirectHandler =
new SavedRequestAwareAuthenticationSuccessHandler();
successRedirectHandler.setDefaultTargetUrl("/landing");
return successRedirectHandler;
}
現在我的問題是,我該如何生成Auth令牌? 從這一點來看,到目前為止,我還沒有太多可用的細節,我嘗試創建了自定義Filter,它攔截了“ / landing”並試圖將URL修改為/ oauth / token?grant_type = urn:ietf:params:oauth:grant-type :saml2-bearer&assertion = AssertionToBase64Url(已跟隨 ),但無法生成令牌。 我當前的配置,帶有oauth實現的Spring安全性和spring saml也可以獨立工作,因此現在我要結合使用這兩個功能。
我的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,將生成一個auth訪問,刷新令牌,該令牌用戶可以訪問api。 任何人都可以提供某種解決方案...
不久前,我遇到了同樣的挑戰,弄清楚了之后,我寫了一篇文章(不能在此處發布...)。 基本上,您的oAuth授權服務器是使您的SAML后端適應oAuth的“橋梁”,反之亦然...在這里找到它:
GitHub中還有一個包含所有來源的倉庫,例如:
https://github.com/OhadR/spring-oAuth2-SAML-integration
我知道它只是鏈接,但再次-文章太長且太詳細。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.