Oli*_*ler 12 authentication google-app-engine facebook-authentication google-cloud-endpoints google-oauth
我在这篇Google Cloud Endpoints帖子和用户身份验证中引用了@MinWan的精彩答案,他描述了一种向针对App Engine的Cloud Endpoints的请求添加自定义标头的方法.
很明显,我们可以添加一个自定义标头,并根据我们想要验证的每个服务(例如Google,Twitter,Facebook)编写验证器,其中每个验证器读取特定的标头并对服务进行身份验证.如果令牌有效,则服务通常会返回包含电子邮件地址或用户ID的响应,以及一些额外信息[A],我们会从中生成com.google.api.server.spi.auth.common.User,稍后将其作为com.google.appengine.api.users.User传递到端点方法.
第一个问题:为什么我们有两个不同的用户实体,例如具有不同命名空间的用户?看起来,这些都不是子/超类,所以它们可能在幕后明确地投射.
第二个问题:显式转换的User实体带来的问题,并且没有自定义字段,我可以放置服务返回的额外信息[A],这是额外的信息丢失.这样的额外信息可能有助于将外部服务的oauth2用户与本地用户或由其他服务返回的oauth2用户进行匹配.
有什么输入?处理多种身份验证服务的建议方法是什么?
刚刚测试过,你肯定可以将User子类化为包含你想要的任何私有字段.只需使用类继承多态就可以从Authenticator方法返回该类型的对象,而无需在方法签名中更改默认用户的类型.
import javax.servlet.http.HttpServletRequest;
import com.google.api.server.spi.auth.common.User;
import com.google.api.server.spi.config.Authenticator;
public class BazUser extends User {
private String secret; // extra piece of data held by this User
public BazUser(String email) {
super(email);
this.secret = "notasecret";
}
public BazUser (String email, String secret) {
super (email);
this.secret = secret;
}
}
public class BazAuthenticator implements Authenticator {
public User authenticate(HttpServletRequest req) {
return new BazUser ("userid@baz.com", "secret");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1306 次 |
| 最近记录: |