Annotation Interface WithMockOAuth2


@Retention(RUNTIME) @Documented @WithSecurityContext(factory=WithMockOAuth2SecurityContextFactory.class) public @interface WithMockOAuth2

Simulates OAuth2 scopes to help with endpoint testing with Spring Security. This is designed to mimic exactly what the OAuth2ResourceWebSecurityConfiguration does through its autoconfiguration, along with OAuth token expectations that come back from the auth server, from the ScopeFilteringAccessTokenConverter.

If you are using MockMvc this will not work if OAuth is configured to run stateless (the default). In that case, you must instead inject the MockMvcOAuth2AuthenticationUtil instead.

Author:
Phillip Verheyden (phillipuniverse)
See Also:
  • Element Details

    • value

      @AliasFor("scopes") String[] value
      Alias for scopes().

      This attribute may not be used in conjunction with scopes(), but it may be used instead of scopes().

      See Also:
      Default:
      {}
    • scopes

      @AliasFor("value") String[] scopes
      The scopes that should be mocked on the authentication
      Returns:
      scopes on the security context
      Default:
      {}
    • principal

      String principal
      The user principal.
      Default:
      ""
    • authorities

      String[] authorities
      Additional security authorities assigned to the token
      Returns:
      security roles for the token
      Default:
      {}
    • authDetails

      AuthDetail[] authDetails
      Additional auth details assigned to the token beyond any default values. For example, setting a detail with the value, 'customer_id'
       
       @WithMockOAuth2(scopes = "USER",
         authorities = {"READ_CUSTOMER", "UPDATE_CUSTOMER"},
         authDetails = {
           @AuthDetail(key = "customer_id", value = "1234")
         })
       
       
      Returns:
      Auth details to set
      Default:
      {}