gpt4 book ai didi

java - 由于缺少 CSRF 'preserved state',Spring-Oauth2 访问 token 请求从未成功

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:17:18 25 4
gpt4 key购买 nike

过去几天我一直在努力实现 spring boot/spring security/和 java 配置与 spring-security-oauth2 一起工作。我已经设法解决了大部分困难,但我对现在出了什么问题感到困惑。

我正在成功完成以下步骤:

  • 将用户发送给提供商以授权应用程序代表他们行事
  • 提示用户根据安全性登录提供商
  • 用户授权应用,重定向 url 将它们连同查询字符串中的 ?code=asdfa&state=asdfasf 以原始 url 发送回客户端应用

在这一点上,我相信使用 AuthorizationCodeResourceDetails 的任何东西都应该交换授权代码和客户端应用凭据以获取访问 token 。这是进程失败的地方,并显示以下堆栈跟踪。

 Caused by: org.springframework.security.oauth2.common.exceptions.InvalidRequestException: Possible CSRF detected - state parameter was present but no state could be found
at org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider.getParametersForTokenRequest(AuthorizationCodeAccessTokenProvider.java:246)
at org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider.obtainAccessToken(AuthorizationCodeAccessTokenProvider.java:198)
at org.springframework.security.oauth2.client.token.AccessTokenProviderChain.obtainNewAccessTokenInternal(AccessTokenProviderChain.java:142)
at org.springframework.security.oauth2.client.token.AccessTokenProviderChain.obtainAccessToken(AccessTokenProviderChain.java:118)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.acquireAccessToken(OAuth2RestTemplate.java:221)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.getAccessToken(OAuth2RestTemplate.java:173)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.createRequest(OAuth2RestTemplate.java:105)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:564)
at org.springframework.security.oauth2.client.OAuth2RestTemplate.doExecute(OAuth2RestTemplate.java:128)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:529)
at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:261)
at com.pvr.apps.admin.user.UserServiceImpl.getAllUsers(UserServiceImpl.java:51)
at com.pvr.apps.admin.web.IndexController.serveUserList(IndexController.java:35)

客户端上的东西看起来像(我在主配置上也有一个 @EnableOAuth2Client 注释)。

@Component
public class UserServiceImpl implements UserService {

@Resource
@Qualifier("accessTokenRequest")
private AccessTokenRequest accessTokenRequest;

public OAuth2ProtectedResourceDetails createResource() {
AuthorizationCodeResourceDetails resourceDetails = new AuthorizationCodeResourceDetails();
resourceDetails.setScope(Lists.newArrayList("read", "write"));
resourceDetails.setClientId("admin");
resourceDetails.setClientSecret("password");
resourceDetails.setAuthenticationScheme(AuthenticationScheme.query);
resourceDetails.setAccessTokenUri("http://provider.com:8080/oauth/token");
resourceDetails.setUserAuthorizationUri("http://provider.com:8080/oauth/authorize");
return resourceDetails;
}

@Override
public List<User> getAllUsers() {

RestTemplate template = new OAuth2RestTemplate(createResource(), new DefaultOAuth2ClientContext(accessTokenRequest));

ResponseEntity<User[]> responseEntity = template.getForEntity("http://provider.com:8080/users/", User[].class);
return Lists.newArrayList(responseEntity.getBody());
}
}

在提供者方面:

授权服务器配置:

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter{

@Autowired
private LoginUrlAuthenticationEntryPoint authenticationEntryPoint;

@Autowired
private AuthenticationManager authenticationManager;


@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
oauthServer
.authenticationEntryPoint(authenticationEntryPoint)
.tokenKeyAccess("isAnonymous() || hasAuthority('ROLE_TRUSTED_CLIENT')")
.checkTokenAccess("hasAuthority('ROLE_TRUSTED_CLIENT')");
}

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {

JwtAccessTokenConverter converter = new JwtAccessTokenConverter();

endpoints
.authenticationManager(authenticationManager)
.accessTokenConverter(converter)
.tokenStore(new JwtTokenStore(converter));
}


// TODO: this should read from a db
public void configure(ClientDetailsServiceConfigurer clientConfigurer) throws Exception {
clientConfigurer.inMemory()
.withClient("admin").secret("password")
.authorizedGrantTypes(
GrantType.PASSWORD.type,
GrantType.AUTHORIZATION_CODE.type,
GrantType.IMPLICIT.type,
GrantType.REFRESH_TOKEN.type
)
.authorities("ROLE_TRUSTED_CLIENT")
.scopes("read", "write", "trust")
.accessTokenValiditySeconds(60);
}
}

和资源服务器配置:

@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MySecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
UserDetailsService userDetailService;

@Autowired
AuthenticationProvider authenticationProvider;

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

@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginProcessingUrl("/login.do")
.usernameParameter("uid")
.passwordParameter("pwd")
.loginPage("/login")
.failureUrl("/login?error=true")
.and()
.userDetailsService(userDetailService);
}

}

最佳答案

它正在寻找的状态将在 OAuth2ClientContext 中,但由于您刚刚创建了一个新状态,因此在需要时它超出了范围。如果您注入(inject)来自 @EnableOAuth2Client 的那个,它将位于 @Scope("session") 中,因此它将能够为您解析状态。 GitHub 中的所有示例都是这样工作的。或者您可以自己管理持久性,我想。

关于java - 由于缺少 CSRF 'preserved state',Spring-Oauth2 访问 token 请求从未成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27629373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com