[英]Java : method reference of an arbitrary object
Given the following code (copied out of context to keep things simple) It works fine, and enables OAuth2 JWT token validation.鉴于以下代码(复制上下文以保持简单)它工作正常,并启用 OAuth2 JWT 令牌验证。
...
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.oauth2.server.resource.OAuth2ResourceServerConfigurer;
...
protected void configure(HttpSecurity http) throws Exception {
http.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt);
}
...
I have a hard time understanding the OAuth2ResourceServerConfigurer::jwt
method reference我很难理解
OAuth2ResourceServerConfigurer::jwt
方法参考
httpConfig.oauth2ResourceServer
expects an Customizer
as input. httpConfig.oauth2ResourceServer
需要一个Customizer
作为输入。 The Customizer
is a functionnal interface that defines one instance method which is void customize(T t)
. Customizer
是一个功能接口,它定义了一个实例方法,即void customize(T t)
。
The jwt
instance method of OAuth2ResourceServerConfigurer
returns an object of type OAuth2ResourceServerConfigurer.JwtConfigurer
, which does not define the void customize(T t)
method. OAuth2ResourceServerConfigurer
的jwt
实例方法返回一个OAuth2ResourceServerConfigurer
类型的对象,该对象没有定义void customize(T t)
OAuth2ResourceServerConfigurer.JwtConfigurer
void customize(T t)
方法。
How can it comply with the Customizer
interface?它如何符合
Customizer
界面?
Also, I don't understand which instance of OAuth2ResourceServerConfigurer
will be used to call the 'jwt()' method.另外,我不明白
OAuth2ResourceServerConfigurer
哪个实例将用于调用 'jwt()' 方法。
The basic Java article does not help me.基本的 Java 文章对我没有帮助。 ( https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html )
( https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html )
I am familiar with Lambdas and method references, but this one I don't get.我熟悉 Lambda 和方法引用,但我不明白这一点。
How can it comply with the
Customizer
interface?它如何符合
Customizer
界面?
When a method reference implements a functional interface, the parameters and the return type have to comply (be compatible), not the method name.当方法引用实现功能接口时,参数和返回类型必须符合(兼容),而不是方法名称。
Consistent with how a lambda expression has to have parameters and a return type that is compatible with the functional method, but the method name isn't given anywhere.与 lambda 表达式必须具有与函数方法兼容的参数和返回类型的方式一致,但未在任何地方给出方法名称。
Also, I don't understand which instance of
OAuth2ResourceServerConfigurer
will be used to call thejwt()
method.另外,我不明白
OAuth2ResourceServerConfigurer
哪个实例将用于调用jwt()
方法。
In the link you provided, see the section named " Reference to an Instance Method of an Arbitrary Object of a Particular Type ".在您提供的链接中,请参阅名为“ 对特定类型的任意对象的实例方法的引用”的部分。
Since your method reference is OAuth2ResourceServerConfigurer::jwt
, which is a no-arg instance method, the t
parameter of the customize(T t)
method becomes the this
value of the jwt()
call.由于您的方法引用是
OAuth2ResourceServerConfigurer::jwt
,这是一个无参数实例方法, customize(T t)
方法的t
参数成为jwt()
调用的this
值。
It is basically the same as this lambda expression:它与这个 lambda 表达式基本相同:
http.oauth2ResourceServer(t -> t.jwt());
Which is equivalent to this anonymous class:这相当于这个匿名类:
http.oauth2ResourceServer(new Customizer<OAuth2ResourceServerConfigurer<HttpSecurity>>() {
@Override
public void customize(OAuth2ResourceServerConfigurer<HttpSecurity> t) {
t.jwt();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.