How can I pass custom information from an App Engine Authenticator to the Endpoint?

后端 未结 2 1614

I am referencing @MinWan \'s awesome answer in this post Google Cloud Endpoints and user's authentication, where he describes a way to add custom headers to a request ag

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-28 10:02

    Just tested, and you can definitely subclass User to contain whichever private fields you want. Just use class inheritance polymorphism to return an object of that type from the Authenticator method, without changing the type from default User in the method signature.

    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");
            }
    }
    

提交回复
热议问题