首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >弹簧反应安全性: ServerAuthenticationConverter和ReactiveAuthenticationManager代表什么?

弹簧反应安全性: ServerAuthenticationConverter和ReactiveAuthenticationManager代表什么?
EN

Stack Overflow用户
提问于 2022-06-27 14:36:41
回答 1查看 305关注 0票数 -1

我不太明白ServerAuthenticationConverterReactiveAuthenticationManager是什么

ServerAuthenticationConverter Spring启动代码是:

代码语言:javascript
运行
复制
@FunctionalInterface
public interface ServerAuthenticationConverter {

    /**
     * Converts a {@link ServerWebExchange} to an {@link Authentication}
     * @param exchange The {@link ServerWebExchange}
     * @return A {@link Mono} representing an {@link Authentication}
     */
    Mono<Authentication> convert(ServerWebExchange exchange);

}

ReactiveAuthenticationManager

代码语言:javascript
运行
复制
@FunctionalInterface
public interface ReactiveAuthenticationManager {

    /**
     * Attempts to authenticate the provided {@link Authentication}
     * @param authentication the {@link Authentication} to test
     * @return if authentication is successful an {@link Authentication} is returned. If
     * authentication cannot be determined, an empty Mono is returned. If authentication
     * fails, a Mono error is returned.
     */
    Mono<Authentication> authenticate(Authentication authentication);

}

两者都有一个返回Mono<Authentication>的方法。

他们代表什么?

目前,我的实现是:

代码语言:javascript
运行
复制
@Component
public class GitJwtServerAuthenticationConverter implements ServerAuthenticationConverter {

    @Override
    public Mono<Authentication> convert(ServerWebExchange exchange) {
        return Mono.justOrEmpty(exchange)
            .flatMap((it) -> Mono.justOrEmpty(it.getRequest().getHeaders()))
            .map((headers) -> headers.get(HttpHeaders.AUTHORIZATION))
            .map((header) -> new GitBearerTokenAuthenticationToken(header.get(0)));
    }

}

并真正直截了当地实施:

代码语言:javascript
运行
复制
public class GitJwtReactiveAuthenticationManager implements ReactiveAuthenticationManager {

    @Override
    public Mono<Authentication> authenticate(Authentication authentication) {
        return Mono.justOrEmpty(authentication);
    }
    
}

我的安全实现是:

代码语言:javascript
运行
复制
@Configuration
@EnableWebFluxSecurity
public class SecurityConfiguration {

    @Bean
    ReactiveAuthenticationManager reactiveAuthenticationManager() {
        return new GitJwtReactiveAuthenticationManager();
    }

    @Bean
    AuthenticationWebFilter authenticationWebFilter(
        ReactiveAuthenticationManager reactiveAuthenticationManager,
        ServerAuthenticationConverter serverAuthenticationConverter
    ) {
        AuthenticationWebFilter authenticationWebFilter = new AuthenticationWebFilter(reactiveAuthenticationManager);
        authenticationWebFilter.setServerAuthenticationConverter(serverAuthenticationConverter);

        return authenticationWebFilter;
    }

    @Bean
    SecurityWebFilterChain springSecurityFilterChain(
        ServerHttpSecurity http,
        AuthenticationWebFilter authenticationWebFilter
    ) {
        return http
            .httpBasic(HttpBasicSpec::disable)
            .csrf(CsrfSpec::disable)
            .formLogin(FormLoginSpec::disable)
            .anonymous(AnonymousSpec::disable)
            .logout(LogoutSpec::disable)
            .authorizeExchange((authorize) -> authorize
                .pathMatchers("/actuator/**").permitAll()
                .pathMatchers("/login/**").permitAll()
                .anyExchange().authenticated()
            )
            .addFilterAt(authenticationWebFilter, SecurityWebFiltersOrder.AUTHENTICATION)
            // .oauth2ResourceServer((resourceServer) -> resourceServer.jwt(withDefaults()))
            .build();
    }

}

我已经测试过了,我得到了一个403 forbidden

代码语言:javascript
运行
复制
~ ❯ curl -i -X POST localhost:8080/me -H "Authorization: $JWT_TOKEN" -H "GICAR_ID: foo"
HTTP/1.1 403 Forbidden
Content-Type: text/plain
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1 ; mode=block
Referrer-Policy: no-referrer
content-length: 13

Access Denied%

有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2022-06-27 17:37:48

ServerAuthenticationConverter

将{@链接ServerWebExchange}转换为{@链接身份验证}

ServerWebExchange包含您的请求/响应。它是客户机和服务器之间的交换。例如,您必须在一个ServerWebExchange中使用WebFilter

并且,当文档化状态时,您可以(很可能)将ServerRequest转换为Authentication对象。

Authentication对象表示将要进行身份验证的内容,或者表示经过身份验证的内容。

例如,您将一个身份验证对象传递给AuthenticationManager.authenticate(Authentication),在那里您可以实现自己的AuthenticationManager

ReactiveAuthenticationManager

使用此接口,您将实现实际的身份验证。例如,这就是您检查用户名和密码是否正确的地方。或者检查您的JWT的签名和有效性。

这是实现最重要的细节实体的地方,也是不应该有bug的地方。

一个典型的流动是

将请求转换为身份验证对象的authentication.

  • the身份验证管理器将身份验证对象传递给身份验证管理器,object

  • create身份验证管理器返回经过完全身份验证的object

  • create--用户详细信息对象,并在安全上下文

中设置它。

所有这些都记录在spring安全文档https://docs.spring.io/spring-security/reference/servlet/authentication/architecture.html中。

此外,在spring安全中已经有了jwt实现。

https://docs.spring.io/spring-security/reference/servlet/oauth2/resource-server/jwt.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72773889

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档