春季启动OAuth2RestTemplate客户端设置,authorization_request_not_found错误

时间:2020-05-06 22:17:10

标签: spring-boot spring-security-oauth2 oauth2resttemplate

我尝试在春季启动项目中设置OAuth2RestTemplate。我尝试遵循自定义设置,但是由于某些原因,尝试访问受保护的资源时总是会出现authorization_request_not_found错误。

我的AppConfig:

@EnableOAuth2Client
@SpringBootApplication
public class AppConfig {

public static void main(String[] args) {
    SpringApplication.run(AppConfig.class, args);
}

@Bean
RestTemplate restTemplate(OAuth2ProtectedResourceDetails oauth2RemoteResource) {
    return new OAuth2RestTemplate(oauth2RemoteResource);
}

}

SecurityConfig:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .mvcMatchers("/", "/login/**").permitAll()
                .mvcMatchers("/secure").authenticated()
                .anyRequest().permitAll()
                .and()
                .oauth2Login()
                .and()
                .oauth2Client();
    }
}

控制器:

@GetMapping("/secure")
public String secure() {
    Object result = restTemplate.getForObject("https://esi.evetech.net/latest/alliances", Object.class);
    return "/";
}

application.yml:

 security:
  oauth2:
    client:
      id: eve
      client-id: clientId
      client-secret: secret
      grant-type: authorization_code
      user-authorization-uri: https://login.eveonline.com/v2/oauth/authorize
      access-token-uri: https://login.eveonline.com/v2/oauth/token
      pre-established-redirect-uri: http://localhost:8080/login/oauth2/code/eve
      use-current-uri: false
      scope:
        - esi-characters.read_blueprints.v1
    resource:
      user-info-uri: https://login.eveonline.com/oauth/verify
spring:
  security:
    oauth2:
      client:
        registration:
          eve:
            authorization-grant-type: authorization_code
            client-id: clientId
            client-secret: secret
            redirect-uri: http://localhost:8080/login/oauth2/code/eve
            client-authentication-method: basic
            scope:
              - esi-characters.read_blueprints.v1
        provider:
          eve:
            authorization-uri: https://login.eveonline.com/v2/oauth/authorize
            token-uri: https://login.eveonline.com/v2/oauth/token
            user-info-uri: https://login.eveonline.com/oauth/verify
            user-name-attribute: CharacterID

现在我发现这也很有趣,因为我不确定为什么必须两次声明这些属性。 我的意思是OAuth2RestTemplateAuthorizationCodeResourceDetails的构造函数中采用AppConfig实例,该实例读取security.oatuh2.client属性,而DefaultOAuth2ClientContext构造函数中的OAuth2RestTemplate读另一个,我只是不明白为什么会这样?有简单的设置吗?

无论如何,回到最初的问题,当我访问/secure时,它会将我重定向到登录页面,我在此成功登录,然后将我重定向回到/secure页面,到目前为止,一切都很好。

在调用restTemplate之前,我在日志中有此内容:

2020-05-06 22:55:31.528 DEBUG 29177 --- [nio-8080-exec-4] o.s.s.w.a.i.FilterSecurityInterceptor    : Secure object: FilterInvocation: URL: /secure; Attributes: [authenticated]
2020-05-06 22:55:31.528 DEBUG 29177 --- [nio-8080-exec-4] o.s.s.w.a.i.FilterSecurityInterceptor    : Previously Authenticated: org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken@3bb07c9: Principal: Name: [<ID>], Granted Authorities: [[ROLE_USER, SCOPE_esi-characters.read_blueprints.v1]], User Attributes: [{CharacterID=<ID>, CharacterName=<Name>, ExpiresOn=2020-05-06T22:15:31, Scopes=esi-characters.read_blueprints.v1, TokenType=Character, CharacterOwnerHash=<Hash>, IntellectualProperty=EVE}]; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@0: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: 472A39D41FE34AC90781A80E39E4A52D; Granted Authorities: ROLE_USER, SCOPE_esi-characters.read_blueprints.v1
2020-05-06 22:55:31.529 DEBUG 29177 --- [nio-8080-exec-4] o.s.s.access.vote.AffirmativeBased       : Voter: org.springframework.security.web.access.expression.WebExpressionVoter@47a65378, returned: 1

但是在调用restTemplate之后:

2020-05-06 23:00:31.482 DEBUG 29177 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Failed to complete request: org.springframework.security.oauth2.client.resource.UserRedirectRequiredException: A redirect is required to get the users approval
2020-05-06 23:00:31.483 DEBUG 29177 --- [nio-8080-exec-4] o.s.s.w.header.writers.HstsHeaderWriter  : Not injecting HSTS header since it did not match the requestMatcher org.springframework.security.web.header.writers.HstsHeaderWriter$SecureRequestMatcher@205366de
2020-05-06 23:00:31.483 DEBUG 29177 --- [nio-8080-exec-4] s.s.w.c.SecurityContextPersistenceFilter : SecurityContextHolder now cleared, as request processing completed
2020-05-06 23:00:31.484 DEBUG 29177 --- [nio-8080-exec-4] o.s.s.web.DefaultRedirectStrategy        : Redirecting to 'https://login.eveonline.com/v2/oauth/authorize?client_id=<clientId>&redirect_uri=http://localhost:8080/login/oauth2/code/eve&response_type=code&scope=esi-characters.read_blueprints.v1&state=<state>'

如您所见,它带我回到登录页面,所以我再次登录,这一次出现错误:[authorization_request_not_found]

日志:

2020-05-06 23:02:27.285 DEBUG 29177 --- [nio-8080-exec-6] o.s.security.web.FilterChainProxy        : /login/oauth2/code/eve?code=<code>&state=<state> at position 8 of 17 in additional filter chain; firing Filter: 'OAuth2LoginAuthenticationFilter'
2020-05-06 23:02:27.285 DEBUG 29177 --- [nio-8080-exec-6] o.s.s.w.u.matcher.AntPathRequestMatcher  : Checking match of request : '/login/oauth2/code/eve'; against '/login/oauth2/code/*'
2020-05-06 23:02:27.285 DEBUG 29177 --- [nio-8080-exec-6] .s.o.c.w.OAuth2LoginAuthenticationFilter : Request is to process authentication
2020-05-06 23:02:27.287 DEBUG 29177 --- [nio-8080-exec-6] .s.o.c.w.OAuth2LoginAuthenticationFilter : Authentication request failed: org.springframework.security.oauth2.core.OAuth2AuthenticationException: [authorization_request_not_found] 

org.springframework.security.oauth2.core.OAuth2AuthenticationException: [authorization_request_not_found] 
    at org.springframework.security.oauth2.client.web.OAuth2LoginAuthenticationFilter.attemptAuthentication(OAuth2LoginAuthenticationFilter.java:163) ~[spring-security-oauth2-client-5.2.2.RELEASE.jar:5.2.2.RELEASE]

这时我迷路了,不知道为什么得到这个。

我还将该项目上传到了github,以方便查明问题所在。

谢谢

0 个答案:

没有答案