【发布时间】:2021-07-14 05:15:11
【问题描述】:
我创建了一个简单的例子,只充当资源服务器为客户端提供API。
完整的代码可以在Github - hantsy/spring-webmvc-auth0-sample找到。
我浏览过Spring security samples,它使用了jwk-set-uri,在我的应用程序中,我使用了issuer-uri。
security:
oauth2:
resourceserver:
jwt:
issuer-uri: <auth0 provided issuer uri>
我关注了Auth0 Spring security 5 API Guide ,添加了观众声明验证。
我尝试使用 MockMVC 添加ApplicationTests。
@Test
public void testGetById() throws Exception {
Post post = Post.builder().title("test").content("test content").build();
post.setId(1L);
given(this.posts.findById(anyLong())).willReturn(Optional.of(post));
this.mockMvc
.perform(
get("/posts/{id}", 1L)
.accept(MediaType.APPLICATION_JSON)
)
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value("test"));
verify(this.posts, times(1)).findById(any(Long.class));
verifyNoMoreInteractions(this.posts);
}
我的安全配置与此类似。
@Bean
SecurityFilterChain springWebFilterChain(HttpSecurity http) throws Exception {
return http
.httpBasic(AbstractHttpConfigurer::disable)
.csrf(AbstractHttpConfigurer::disable)
.sessionManagement(c -> c.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeRequests(c -> c
.antMatchers("/", "/info").permitAll()
.antMatchers(HttpMethod.GET, "/posts/**").permitAll()//.hasAuthority("SCOPE_read:posts")
.antMatchers(HttpMethod.POST, "/posts/**").hasAuthority("SCOPE_write:posts")
.antMatchers(HttpMethod.PUT, "/posts/**").hasAuthority("SCOPE_write:posts")
.antMatchers(HttpMethod.DELETE, "/posts/**").hasAuthority("SCOPE_delete:posts")
.anyRequest().authenticated()
)
.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt)
.cors().and().build();
}
运行测试时。
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException: Cannot invoke "org.springframework.test.web.servlet.DefaultMvcResult.setHandler(Object)" because "mvcResult" is null
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
我不知道哪里错了?我查看了官方示例,它包含一个 spring.factories file 来启用模拟服务器环境,它是 Spring Boot 中的要求吗?
【问题讨论】:
标签: spring-boot spring-mvc oauth-2.0 openid-connect auth0