我正在尝试使Vertx http服务器通过多个Auth提供商(如同一登录页面上的FormBased,Facebook,Google或任何其他oAuth2 Provider)中的一种来对用户进行身份验证。有可能吗?
现在,我有了仅可使用一个oAuth2提供程序的代码。当我添加第二秒时,系统崩溃并显示错误:
2018年9月21日晚上7:54:36 io.vertx.ext.web.impl.RoutingContextImplBase 严重:路由中出现意外异常 io.vertx.core.impl.NoStackTraceThrowable:传递的代码不正确 或过期。
错误代码:
import io.vertx.core.AbstractVerticle;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.json.JsonObject;
import io.vertx.core.net.JksOptions;
import io.vertx.ext.auth.oauth2.OAuth2Auth;
import io.vertx.ext.auth.oauth2.providers.FacebookAuth;
import io.vertx.ext.auth.oauth2.providers.GithubAuth;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.OAuth2AuthHandler;
import sun.security.tools.keytool.CertAndKeyGen;
import sun.security.x509.X500Name;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.*;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Date;
public class Server extends AbstractVerticle {
// you should never store these in code,
// these are your github application credentials
private static final String FB_CLIENT_ID = "xxx";
private static final String FB_CLIENT_SECRET = "xxx";
private static final String GH_CLIENT_ID = "yyy";
private static final String GH_CLIENT_SECRET = "yyy";
private static final String HOST_NAME = "dev.service.com";
@Override
public void start() throws Exception {
final Router router = Router.router(vertx);
OAuth2Auth ghAuthProvider = GithubAuth.create(vertx, GH_CLIENT_ID, GH_CLIENT_SECRET);
OAuth2Auth fbAuthProvider = FacebookAuth.create(vertx, FB_CLIENT_ID, FB_CLIENT_SECRET);
// create a oauth2 handler for GitHub
OAuth2AuthHandler ghAuthHandler = OAuth2AuthHandler.create(ghAuthProvider, "https://" + HOST_NAME + "/callback");
ghAuthHandler.addAuthority("profile");
ghAuthHandler.setupCallback(router.get("/callback"));
// create a oauth2 handler for Facebook
OAuth2AuthHandler fbAuthHandler = OAuth2AuthHandler.create(fbAuthProvider, "https://" + HOST_NAME + "/callback")
.extraParams(new JsonObject().put("scope", "email"))
.setupCallback(router.route());
// protect everything under /ghprotected
router.route("/ghprotected/*").handler(ghAuthHandler);
// mount some handler under the protected zone
router.route("/ghprotected/somepage").handler(rc -> rc.response()
.end("Welcome to the GH protected resource!"));
// protect everything under /fbprotected
router.route("/fbprotected/*").handler(fbAuthHandler);
// mount some handler under the protected zone
router.route("/fbprotected/somepage").handler(rc -> rc.response()
.end("Welcome to the FB protected resource!"));
router.get("/fbprotected").handler(ctx -> {
ctx.response()
.putHeader("Content-Type", "text/html")
// .end(userInfo.encodePrettily());
.end("fb login handler");
});
// welcome page
router.get("/").handler(ctx -> ctx.response()
.putHeader("content-type", "text/html")
.end("Hello<br><a href=\"/ghprotected/somepage\">Protected by GitHub</a>" +
"<a href=\"/fbprotected/somepage\">Protected by FB</a>"));
// Create http server with SSL and self signed certificate
HttpServerOptions httpOpts = new HttpServerOptions();
try {
// Generate a self-signed key pair and certificate.
KeyStore store = KeyStore.getInstance("JKS");
store.load(null, null);
CertAndKeyGen keypair = new CertAndKeyGen("RSA", "SHA1WithRSA", null);
X500Name x500Name = new X500Name("localhost", "IT", "unknown", "unknown", "unknown", "unknown");
keypair.generate(1024);
PrivateKey privKey = keypair.getPrivateKey();
X509Certificate[] chain = new X509Certificate[1];
chain[0] = keypair.getSelfCertificate(x500Name, new Date(), (long) 365 * 24 * 60 * 60);
store.setKeyEntry("selfsigned", privKey, "changeit".toCharArray(), chain);
store.store(new FileOutputStream(".keystore"), "changeit".toCharArray());
httpOpts.setKeyStoreOptions(new JksOptions().setPath(".keystore").setPassword("changeit"));
httpOpts.setSsl(true);
} catch (KeyStoreException | IOException | NoSuchAlgorithmException | CertificateException | NoSuchProviderException | InvalidKeyException | SignatureException ex) {
// Logger.getLogger(WebServerVerticle.class.getName()).log(Level.SEVERE, "Failed to generate a self-signed cert and other SSL configuration methods failed.", ex);
// startFuture.fail(ex);
System.out.println("error with ssl");
}
HttpServer server = vertx.createHttpServer(httpOpts);
server.requestHandler(router::accept).listen(443);
}
}
有什么建议吗?