-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: basic auth issue that not set Authentication in SecurityContextH…
…older (#643)
- Loading branch information
ChiveHao
committed
Aug 8, 2024
1 parent
78e6532
commit 299b8b2
Showing
4 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
version=0.15.0 | ||
version=0.15.1 |
10 changes: 9 additions & 1 deletion
10
...va/run/ikaros/server/security/authentication/basicauth/BasicAuthenticationConfigurer.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,22 @@ | ||
package run.ikaros.server.security.authentication.basicauth; | ||
|
||
import org.springframework.security.config.Customizer; | ||
import org.springframework.security.config.web.server.SecurityWebFiltersOrder; | ||
import org.springframework.security.config.web.server.ServerHttpSecurity; | ||
import org.springframework.stereotype.Component; | ||
import run.ikaros.server.security.authentication.SecurityConfigurer; | ||
|
||
@Component | ||
public class BasicAuthenticationConfigurer implements SecurityConfigurer { | ||
private final BasicAuthenticationFilter basicAuthenticationFilter; | ||
|
||
public BasicAuthenticationConfigurer(BasicAuthenticationFilter basicAuthenticationFilter) { | ||
this.basicAuthenticationFilter = basicAuthenticationFilter; | ||
} | ||
|
||
@Override | ||
public void configure(ServerHttpSecurity http) { | ||
http.httpBasic(Customizer.withDefaults()); | ||
http.httpBasic(Customizer.withDefaults()) | ||
.addFilterAfter(basicAuthenticationFilter, SecurityWebFiltersOrder.AUTHENTICATION); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...n/java/run/ikaros/server/security/authentication/basicauth/BasicAuthenticationFilter.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package run.ikaros.server.security.authentication.basicauth; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.context.ReactiveSecurityContextHolder; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.core.userdetails.ReactiveUserDetailsService; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.server.ServerWebExchange; | ||
import org.springframework.web.server.WebFilter; | ||
import org.springframework.web.server.WebFilterChain; | ||
import reactor.core.publisher.Mono; | ||
|
||
@Slf4j | ||
@Component | ||
public class BasicAuthenticationFilter implements WebFilter { | ||
private final ReactiveUserDetailsService userDetailsService; | ||
|
||
public BasicAuthenticationFilter(ReactiveUserDetailsService userDetailsService) { | ||
this.userDetailsService = userDetailsService; | ||
} | ||
|
||
|
||
@Override | ||
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { | ||
String basic64 = | ||
extractBasic64(exchange.getRequest().getHeaders().getFirst(HttpHeaders.AUTHORIZATION)); | ||
if (basic64 != null) { | ||
String decoder = | ||
new String(Base64.getDecoder().decode(basic64), StandardCharsets.UTF_8); | ||
if (!decoder.contains(":")) { | ||
return Mono.empty(); | ||
} | ||
String[] split = decoder.split(":"); | ||
String username = split[0]; | ||
String password = split[1]; | ||
return userDetailsService.findByUsername(username) | ||
.map(userDetails -> new UsernamePasswordAuthenticationToken( | ||
userDetails, null, userDetails.getAuthorities())) | ||
.map(authenticationToken -> { | ||
SecurityContextHolder.getContext().setAuthentication(authenticationToken); | ||
return authenticationToken; | ||
}) | ||
.map(ReactiveSecurityContextHolder::withAuthentication) | ||
.flatMap(context -> chain.filter(exchange).contextWrite(context)); | ||
} | ||
return chain.filter(exchange); | ||
} | ||
|
||
private String extractBasic64(String bearerToken) { | ||
if (bearerToken != null && bearerToken.startsWith("Basic ")) { | ||
return bearerToken.substring(6); | ||
} | ||
return null; | ||
} | ||
} |