[英]Full authentication is required to access this resource when trying to get authentication token for REST service
[英]Authentication is required to obtain an access token (anonymous not allowed)
我嘗試修改現有示例-Tonr2和Sparklr2 。 我也查看了基於Spring Boot Spring Boot OAuth2的本教程。 我嘗試構建類似Tonr2示例中的應用程序,但沒有首先登錄(在tonr2上)。 我只需要在Sparklr2一側進行身份驗證即可。 我這樣做:
@Bean
public OAuth2ProtectedResourceDetails sparklr() {
AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails();
details.setId("sparklr/tonr");
details.setClientId("tonr");
details.setTokenName("oauth_token");
details.setClientSecret("secret");
details.setAccessTokenUri(accessTokenUri);
details.setUserAuthorizationUri(userAuthorizationUri);
details.setScope(Arrays.asList("openid"));
details.setGrantType("client_credentials");
details.setAuthenticationScheme(AuthenticationScheme.none);
details.setClientAuthenticationScheme(AuthenticationScheme.none);
return details;
}
但是我有Authentication is required to obtain an access token (anonymous not allowed)
。 我檢查了這個問題 。 當然,我的用戶是匿名的-我想登錄Sparklr2。 另外,我嘗試了此bean設置的不同組合,但沒有任何好處。 如何解決? 如何使其如我所願?
該職位將近遲到了兩年。
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth instanceof AnonymousAuthenticationToken) {
if (!resource.isClientOnly()) {
throw new InsufficientAuthenticationException(
"Authentication is required to obtain an access token (anonymous not allowed)");
}
}
你要么
ClientCredentialsResourceDetails
中使用OAuth2RestTemplate
,或者 AuthorizationCodeResourceDetails
訪問外部資源之前,對用戶進行身份驗證 實際上,在tonr2 and sparklr2
示例中(我個人覺得這個名稱很混亂),要訪問sparklr2
資源,用戶必須首先在tonr2
上進行身份驗證。 如oauth2 / tonr所示 :
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("marissa").password("wombat").roles("USER").and().withUser("sam")
.password("kangaroo").roles("USER");
}
如果您的用戶是匿名用戶,則可能要檢查Single Sign On 。
對於只想快速嘗試Oauth2集成的用戶,請向您的應用程序添加基本身份驗證:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated().and().httpBasic();
}
application.properties:
spring.security.user.password=password
spring.security.user.name=user
不要忘記為您的項目添加spring-boot-starter-security
。
例如在gradle中: compile 'org.springframework.boot:spring-boot-starter-security'
或者,您也可以通過以下方式禁止AnonymousAuthenticationToken
創建:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.anonymous().disable();
}
舊帖子...
確實確實會從AccessTokenProviderChain引發異常,但是當Spring安全過濾器調用錯誤順序時會發生此異常。 確保您的OpenIdAuthenticationFilter在OAuth2ClientContextFilter之后調用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.