Le test suivant échoue lorsque j'essaie d'intégrer une session de printemps.session de printemps n'est pas compatible avec MockHttpSession
class WeChatOAuth2AuthenticationFilterTest extends AbstractWebMvcTest {
@Test
void it_should_redirect_user_to_origin_uri_when_wechat_oauth_is_finished() throws Exception {
String code = "codeToExchangeWeChatUserAccessToken"
String plainUrl = "http://www.example.com/index.html?a=b#/route"
String state = Base64.getUrlEncoder().encodeToString(plainUrl.getBytes("UTF-8"))
WxMpOAuth2AccessToken accessToken = new WeChatUserOAuth2AccessTokenFixture().buildToken()
given(wxMpService.oauth2getAccessToken(code))
.willReturn(accessToken)
this.mockMvc.perform(get("/wechat/oauth/token")
.param("state", state)
.param("code", code))
.andDo(print())
.andExpect(status().is3xxRedirection())
.andExpect(redirectedUrl(plainUrl))
.andExpect(authenticated())
// throws Authentication should not be null
}
}
@Configuration
@EnableSpringHttpSession
public class HttpSessionConfig {
@Bean
protected SessionRepository sessionRepository() {
return new MapSessionRepository();
}
}
Après un certain débogage, je trouve qu'il est probablement dû à je ne peux pas HttpSession
// org.springframework.security.web.context.HttpSessionSecurityContextRepository
public SecurityContext loadContext(HttpRequestResponseHolder requestResponseHolder) {
HttpServletRequest request = requestResponseHolder.getRequest();
HttpServletResponse response = requestResponseHolder.getResponse();
HttpSession httpSession = request.getSession(false);
//returns null with spring-session,
//returns a MockHttpSession instance without spring-session
SecurityContext context = readSecurityContextFromSession(httpSession);
Actuellement, je fais la session de printemps désactivé pour les tests avec @ConditionalProperties. Toute meilleure idée est la bienvenue.