破碎的toString,可选,无值 [英] Broken toString of Optional without value

查看:55
本文介绍了破碎的toString,可选,无值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有基于证书的身份验证:

I have an certificate based authentication:

import java.security.cert.X509Certificate;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.inject.Inject;
import javax.inject.Named;
import javax.naming.InvalidNameException;
import javax.naming.ldap.LdapName;
import javax.naming.ldap.Rdn;

@Named
public class UsernameService {

    private static final String MISSING_NAME = null;

    private static final Logger LOG = Logger.getLogger(UsernameService.class.getCanonicalName());

    @Inject
    private final Optional<X509Certificate> cert = null;

    public String currentRequestUsername() {
        if (cert.isPresent()) {
            try {
                LOG.finest("Check Certificate: " + cert); // <- exception!
                for (Rdn rdn : new LdapName(cert.get().getSubjectDN().getName()).getRdns()) {
                    LOG.finest("Check RDN entry for Common name(cn): " + rdn);
                    if (rdn.getType().equals("CN")) {
                        LOG.finer("Found Username: " + rdn.getValue().toString());
                        return rdn.getValue().toString();
                    }
                }
            } catch (InvalidNameException e) {
                LOG.log(Level.CONFIG, "Parse DName impossible: " + cert.get().getSubjectDN().getName() + ".", e);
            } catch (NullPointerException e) {
                LOG.log(Level.FINE, e.getMessage(), e);
            }
        }
        return MISSING_NAME;
    }
}

如您在上面的代码中看到的,如果值存在",则将可选内容打印到记录器中.现在是什么意思?空值是有效值吗?答案在 javadoc :

As you can see in the code above, if a value is "present" print the optional to the logger. What does it mean present? Are null values valid values? The answer is in the javadoc:

如果此Optional中存在值,则返回该值,否则抛出NoSuchElementException.

If a value is present in this Optional, returns the value,otherwise throws NoSuchElementException.

好,因此重现值是存在的". null是否可能返回值?我们了解更多:

Ok, so returing values are "present". Is null a possible returning value? We read more:

返回:此Optional保留的非null值

Returns: the non-null value held by this Optional

因此,不,不会将空值标识为存在.

So no, null values are not identified as present.

App-Config执行请求范围的证书Bean:

The App-Config does the request-scoped certificate bean:

@Bean
@Autowired
@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public X509Certificate getCertificate(HttpServletRequest r) {
    X509Certificate[] certs = (X509Certificate[]) r.getAttribute("javax.servlet.request.X509Certificate");
    if (certs == null || certs.length == 0) {
        return null;
    }
    return certs[0];
}

但是我在控制台中遇到了异常:

But I get an exception in the console:

org.springframework.aop.AopInvocationException: AOP configuration seems to be invalid: tried calling method [public abstract java.lang.String java.security.cert.Certificate.toString()] on target [null]; nested exception is java.lang.IllegalArgumentException: object is not an instance of declaring class
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:351)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
        at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:752)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
        at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:136)
        at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:124)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
        at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
        at $java.security.cert.X509Certificate$$EnhancerBySpringCGLIB$$96797f0e.toString(<generated>)
        at java.util.Formatter$FormatSpecifier.printString(Formatter.java:2886)
        at java.util.Formatter$FormatSpecifier.print(Formatter.java:2763)
        at java.util.Formatter.format(Formatter.java:2520)
        at java.util.Formatter.format(Formatter.java:2455)
        at java.lang.String.format(String.java:2940)
        at java.util.Optional.toString(Optional.java:346)
        at java.lang.String.valueOf(String.java:2994)
        at java.lang.StringBuilder.append(StringBuilder.java:131)
        at XXX.UsernameService.currentRequestUsername(UsernameService.java:27)
...
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

有什么主意吗?

推荐答案

现在,我在IllegalArgumentException的构造函数中创建了一个断点. 然后,我退回了2条痕迹并检查了CDI调用.

Now I created a breakpoint in the constructor of the IllegalArgumentException. Then I steped back 2 traces and inspect the CDI invocation.

我看到一个NullBean具有一个org.springframework.beans.factory.support.NullBean实例.

I see an NullBean having an instance of org.springframework.beans.factory.support.NullBean.

所以可选值似乎不是null,但是bean是空bean.

So the optional seems not to be null but the bean is a null bean.

我的解决方案是简单地捕获NullBean不是证书的异常.

My solution is to simply catch the Exception that a NullBean is not a Certificate.

import java.security.cert.X509Certificate;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.inject.Inject;
import javax.inject.Named;
import javax.naming.InvalidNameException;
import javax.naming.ldap.LdapName;
import javax.naming.ldap.Rdn;

@Named
public class UsernameService {

    private static final String MISSING_NAME = null;

    private static final Logger LOG = Logger.getLogger(UsernameService.class.getCanonicalName());

    @Inject
    private final Optional<X509Certificate> cert = null;

    public String currentRequestUsername() {
        if (cert.isPresent()) {
            try {
                LOG.finest("Check Certificate: " + cert);
                for (Rdn rdn : new LdapName(cert.get().getSubjectDN().getName()).getRdns()) {
                    LOG.finest("Check RDN entry for Common name(cn): " + rdn);
                    if (rdn.getType().equals("CN")) {
                        LOG.finer("Found Username: " + rdn.getValue().toString());
                        return rdn.getValue().toString();
                    }
                }
            } catch (InvalidNameException e) {
                LOG.log(Level.CONFIG, "Parse DName impossible: " + cert.get().getSubjectDN().getName() + ".", e);
            } catch (NullPointerException e) {
                LOG.log(Level.FINE, e.getMessage(), e);
            } catch (RuntimeException e) {
                LOG.log(Level.FINE, e.getMessage(), e);
            }
        }
        return MISSING_NAME;
    }
}

这篇关于破碎的toString,可选,无值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆