无法在AuthenticationFetcher micronaut中获取客户端证书

问题描述

我正在尝试在AuthenticationFetcher实现中获取客户端证书以具有自定义授权实现。

@Singleton
public class SslAuthenticationFetcher implements AuthenticationFetcher {
    private static final Logger LOGGER = LoggerFactory.getLogger(SslAuthenticationFetcher.class);
    private static final String AUTHENTICATION = "AUTHENTICATION";

    private final SessionStore<? extends Session> sessionStore;
    private final X509CertificateParser x509CertificateParser;

    @Inject
    public SslAuthenticationFetcher(X509CertificateParser x509CertificateParser,SessionStore<? extends Session> sessionStore) {
        this.x509CertificateParser = x509CertificateParser;
        this.sessionStore = sessionStore;
    }

    @Override
    public Publisher<Authentication> fetchAuthentication(HttpRequest<?> request) {
        
        // Code to check if user-session already exist
        
        UserDetails userDetails = getAuthentication(request);
        session.put(AUTHENTICATION,userDetails);    

        return Flowable.just(userDetails);
    }

    @NotNull
    private UserDetails getAuthentication(HttpRequest<?> request) {
        Optional<Certificate> certificate = request.getCertificate(); // This always returns empty
        if (!certificate.isPresent()) {
            throw new AuthenticationException("No certificate chain found");
        }
        
        // Code to parse the certificate to create UserDetails
    }
}

我有一个集成测试,该测试会触发对/hello端点的GET请求

@MicronautTest
class HelloControllerTest {

    @Inject
    @Client("/")
    private HttpClient httpClient;

    @Test
    void testHelloEndpoint(){

        HttpResponse<String> response = httpClient.toBlocking()
                .exchange(HttpRequest.GET("/hello"),String.class);

        assertEquals("Hello",response.body());
    }
}

下面是application-test.yml文件中我的客户端ssl配置

micronaut:
  http:
    client:
      ssl:
        enabled: true
        client-authentication: need
        key-store:
          path: <key-store file path>.p12
          password: <key-store file password>
          type: PKCS12
        trust-store:
          path: <trust-store file path>.jks
          password: <trust-store file password>
          type: JKS
        protocol: TLS

问题是,在SslAuthenticationFetcher中,当我尝试从request获取证书时,它总是返回Optional.empty。这里可能是什么问题?

  • Micronaut版本:2.0.0
  • Java版本:1.8

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...