Created
May 20, 2022 13:03
-
-
Save rponte/99028026fd9bed56476d82f4c67af5b2 to your computer and use it in GitHub Desktop.
Spring Security: Adding Access Token in all requests via MockMvcBuilderCustomizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.zup.edu.livraria; | |
import org.springframework.boot.test.autoconfigure.web.servlet.MockMvcBuilderCustomizer; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.security.core.authority.SimpleGrantedAuthority; | |
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; | |
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | |
import org.springframework.test.web.servlet.setup.ConfigurableMockMvcBuilder; | |
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.jwt; | |
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | |
@Configuration | |
public class AddsAccessTokenHeaderMockMvcConfigurer implements MockMvcBuilderCustomizer { | |
@Override | |
public void customize(ConfigurableMockMvcBuilder<?> builder) { | |
builder.defaultRequest(get("/any-uri-because-it-will-be-overridden") // the URI does not matter | |
.with(jwt() | |
.authorities( | |
new SimpleGrantedAuthority("SCOPE_authors:read"), | |
new SimpleGrantedAuthority("SCOPE_authors:write") | |
) | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment