Spring Webflux的WebClient的用户名和密码放在哪里?

我尝试使用路由器和处理程序类制作 Spring WebFlux 安全应用程序。首先,下面的代码是WebFlux安全的配置代码。


@Configuration

@EnableWebFluxSecurity

public class BlogWebFluxSecurityConfig {

    

    @Bean

    public MapReactiveUserDetailsService userDetailsService() {

        

        UserDetails userWebFlux = User.withUsername("joseph").password("password").roles("USER").build();

        return new MapReactiveUserDetailsService(userWebFlux);

    }

    

    @Bean

    public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {

        http

        .authorizeExchange()

        .pathMatchers("/route/user/all", "/route/post/all").permitAll()

        .pathMatchers(HttpMethod.GET, "/route/user/**", "/route/post/**").hasRole("USER")

        .anyExchange().authenticated()

        .and()

        .httpBasic();

        

        return http.build();

    } 

}

接下来的代码是关于路由器类的。


@Configuration

@EnableWebFlux

public class BlogWebFluxEndpointRouter {


    @Bean

    public RouterFunction<ServerResponse> routesUser(UserHandler handler) {

        

        return RouterFunctions.route(RequestPredicates.GET("/route/user/all"), handler::findAll)

                    .andRoute(RequestPredicates.GET("/route/user/id/{id}"), handler::findById)

                    .andRoute(RequestPredicates.GET("/route/user/username/{username}"), handler::findByUsername)

                    .andRoute(RequestPredicates.GET("/route/user/email/{email}"), handler::findByEmail)

                    .andRoute(RequestPredicates.POST("/route/user/create"), handler::register)

                    .andRoute(RequestPredicates.GET("/route/user/login/{username}/{password}"), handler::authenticate);

    }

    

因为我做了WebFlux安全配置,肯定有一些WebClient不能执行和禁止的,如下图,


Login : Unauthorized

User Creation: Forbidden

我不使用 cURL。所以我想知道的是我的WebClient方法是什么,必须在其中找到用户名和密码并将其转移到WebClient课堂上。任何答复将不胜感激。


慕村225694
浏览 161回答 3
3回答

MMTTMM

从 spring 5.1 开始,您应该使用 设置基本身份验证HttpHeaders#setBasicAuth,如下所示:webClient &nbsp;&nbsp;&nbsp;&nbsp;.get() &nbsp;&nbsp;&nbsp;&nbsp;.uri("https://example.com") &nbsp;&nbsp;&nbsp;&nbsp;.headers(headers&nbsp;->&nbsp;headers.setBasicAuth("username",&nbsp;"password")) &nbsp;&nbsp;&nbsp;&nbsp;.exchange() &nbsp;&nbsp;&nbsp;&nbsp;....以前使用 的方法.filter(basicAuthentication("user", "password")现在已弃用。

侃侃尔雅

HTTP 基本身份验证需要在Authorization标头中以 Base64 格式编码的用户名和密码。此外,您不需要登录端点,因为此信息应随每个请求一起发送。将 Basic Auth 标头添加到客户端中的每个调用,如下所示:String&nbsp;basicAuthHeader&nbsp;=&nbsp;"basic&nbsp;"&nbsp;+&nbsp;Base64Utils.encodeToString((username&nbsp;+&nbsp;":"&nbsp;+&nbsp;password).getBytes()) client.get().uri("/route/user/all") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.accept(MediaType.APPLICATION_JSON) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.header(HttpHeaders.AUTHORIZATION,&nbsp;basicAuthHeader) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.exchange() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.flatMapMany(response&nbsp;->&nbsp;response.bodyToFlux(User.class)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.subscribe(u&nbsp;->&nbsp;System.out.println("All&nbsp;Users&nbsp;:&nbsp;"&nbsp;+&nbsp;u.getUsername()&nbsp;+&nbsp;":"&nbsp;+&nbsp;u.getEmail()&nbsp;+&nbsp;":"&nbsp;+&nbsp;u.getFullname()));

GCT1015

Spring 提供了 API,用于通过ClientFilters向 WebClient 提供基本的身份验证参数。Authorization您可以使用较少的自定义编码设置标头来获得相同的结果。请从 spring 文档中查看下面的代码片段:import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;&nbsp; &nbsp; WebClient client = WebClient.builder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(basicAuthentication("user", "password"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java