Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Recently I met some trouble things, this problem bothered me for a month, I don't know how to solve, online also can't find any relevant information, I'm going crazy.
I didn't set the http header, it may be spring security default filter make settings?
org.springframework.web.reactive.function.server.DefaultServerResponseBuilder.AbstractServerResponse#writeTo
org.springframework.web.reactive.function.server.DefaultServerResponseBuilder.AbstractServerResponse#writeStatusAndHeaders
org.springframework.web.reactive.function.server.DefaultServerResponseBuilder.AbstractServerResponse#copy
Here is my code sample.
code sample
java.lang.UnsupportedOperationException: null
at org.springframework.http.ReadOnlyHttpHeaders.putAll(ReadOnlyHttpHeaders.java:138) ~[spring-web-6.0.11.jar:6.0.11]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: ...
@Override
public Mono<Void> filter(@NonNull ServerWebExchange exchange, WebFilterChain chain) {
return chain.filter(exchange)
.contextWrite(context -> context.put(ServerHttpRequest.class, exchange.getRequest()));
@Bean
public SecurityWebFilterChain serverHttpSecurity(ServerHttpSecurity security,
ReactiveAuthenticationManager authenticationManager,
JwtConfigProperties jwtConfigProperties) {
return security
.headers(ServerHttpSecurity.HeaderSpec::disable)
.csrf(ServerHttpSecurity.CsrfSpec::disable)
.httpBasic(ServerHttpSecurity.HttpBasicSpec::disable)
.formLogin(ServerHttpSecurity.FormLoginSpec::disable)
.logout(ServerHttpSecurity.LogoutSpec::disable)
.authenticationManager(authenticationManager)
.securityContextRepository(NoOpServerSecurityContextRepository.getInstance())
.addFilterAt(new JwtAuthenticationFilter(jwtConfigProperties), SecurityWebFiltersOrder.FIRST)
.authorizeExchange(authorize -> authorize
.anyExchange().permitAll()
.build();
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
return Mono.fromCallable(exchange::getRequest)
.mapNotNull(sink -> sink.getHeaders().getFirst(HttpHeaders.AUTHORIZATION))
.filter(sink -> sink.startsWith("Bearer "))
.map(sink -> sink.substring("Bearer ".length()))
.filter(StringUtils::hasText)
.filter(sink -> JWTUtil.verify(sink, jwtConfigProperties.getSecret().getBytes()))
.map(JWT::of)
.map(sink -> {
var username = sink.getPayload(RegisteredPayload.SUBJECT).toString();
var principal = User.withUsername(username)
.password("rob")
.build();
return new UsernamePasswordAuthenticationToken(principal, sink);
.flatMap(sink -> chain.filter(exchange)
.contextWrite(ReactiveSecurityContextHolder.withAuthentication(sink))
.switchIfEmpty(chain.filter(exchange));
@PreAuthorize("isFullyAuthenticated()")
public Mono<String> test2() {
return Mono.just("test2");
–
–
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.