我不太明白ServerAuthenticationConverter
和ReactiveAuthenticationManager
是什么
ServerAuthenticationConverter
Spring启动代码是:
@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
@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>
的方法。
他们代表什么?
目前,我的实现是:
@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)));
}
}
并真正直截了当地实施:
public class GitJwtReactiveAuthenticationManager implements ReactiveAuthenticationManager {
@Override
public Mono<Authentication> authenticate(Authentication authentication) {
return Mono.justOrEmpty(authentication);
}
}
我的安全实现是:
@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
~ ❯ 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%
有什么想法吗?
发布于 2022-06-27 17:37:48
ServerAuthenticationConverter
将{@链接ServerWebExchange}转换为{@链接身份验证}
ServerWebExchange
包含您的请求/响应。它是客户机和服务器之间的交换。例如,您必须在一个ServerWebExchange
中使用WebFilter
。
并且,当文档化状态时,您可以(很可能)将ServerRequest
转换为Authentication
对象。
Authentication
对象表示将要进行身份验证的内容,或者表示经过身份验证的内容。
例如,您将一个身份验证对象传递给AuthenticationManager.authenticate(Authentication)
,在那里您可以实现自己的AuthenticationManager
。
ReactiveAuthenticationManager
使用此接口,您将实现实际的身份验证。例如,这就是您检查用户名和密码是否正确的地方。或者检查您的JWT的签名和有效性。
这是实现最重要的细节实体的地方,也是不应该有bug的地方。
一个典型的流动是
将请求转换为身份验证对象的authentication.
中设置它。
所有这些都记录在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
https://stackoverflow.com/questions/72773889
复制相似问题