WebSessionServerSecurityContextRepository Polish

- map(WebSession::getAttributes)
- use Mono.justOrEmpty

Issue: gh-4843
This commit is contained in:
Rob Winch 2017-11-16 13:23:38 -06:00
parent 8d30d6110b
commit be397b8b33
2 changed files with 13 additions and 4 deletions

View File

@ -19,6 +19,7 @@ import org.springframework.security.core.context.SecurityContext;
import org.springframework.util.Assert;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebSession;
import reactor.core.publisher.Mono;
/**
@ -61,9 +62,11 @@ public class WebSessionServerSecurityContextRepository
}
public Mono<SecurityContext> load(ServerWebExchange exchange) {
return exchange.getSession().flatMap( session -> {
SecurityContext context = (SecurityContext) session.getAttributes().get(this.springSecurityContextAttrName);
return context == null ? Mono.empty() : Mono.just(context);
});
return exchange.getSession()
.map(WebSession::getAttributes)
.flatMap( attrs -> {
SecurityContext context = (SecurityContext) attrs.get(this.springSecurityContextAttrName);
return Mono.justOrEmpty(context);
});
}
}

View File

@ -80,4 +80,10 @@ public class WebSessionServerSecurityContextRepositoryTests {
WebSession session = this.exchange.getSession().block();
assertThat(session.getId()).isNotEqualTo(originalSessionId);
}
@Test
public void loadWhenNullThenNull() {
SecurityContext context = this.repository.load(this.exchange).block();
assertThat(context).isNull();
}
}