L'Authentification de base avec Jetty embarqué 7 serveur et n'web.xml fichier
J'ai intégré la mise en œuvre de la Jetée de 7 en cours d'exécution en tant que service et souhaitez ajouter l'authentification de base avec pas de web.xml fichier pour un servlet.
J'ai créé mes informations d'identification à l'aide de la procédure décrite ici
J'ai pensé que je pouvais créer le serveur, créer un gestionnaire de sécurité avec l'authentification de base et de joindre une HashLoginService au gestionnaire de sécurité. Mais je suis clairement en manque de plusieurs choses, parce que je ne suis jamais arriver demander des informations d'identification.
Ci-dessous est le code. Toute aide serait grandement appréciée.
server = new Server(port);
server.addConnector(getSslChannelConnector(securePort));
server.setGracefulShutdown(1000);
server.setStopAtShutdown(true);
//create the context handler for the server
ServletContextHandler sch = new ServletContextHandler(server, WEBAPP_CONTEXT);
//attach the security handler to it that has basic authentication
sch.setSecurityHandler(getSecurityHandler());
//define the processing servlet.
sch.addServlet(new ServletHolder(new ProcessingServlet()), "/process");
.
.
private SecurityHandler getSecurityHandler() {
//add authentication
Constraint constraint = new Constraint(Constraint.__BASIC_AUTH,"user");
constraint.setAuthenticate(true);
constraint.setRoles(new String[]{"user","admin"});
//map the security constraint to the root path.
ConstraintMapping cm = new ConstraintMapping();
cm.setConstraint(constraint);
cm.setPathSpec("/*");
//create the security handler, set the authentication to Basic
//and assign the realm.
ConstraintSecurityHandler csh = new ConstraintSecurityHandler();
csh.setAuthenticator(new BasicAuthenticator());
csh.setRealmName(REALM);
csh.addConstraintMapping(cm);
//set the login service
csh.setLoginService(getHashLoginService());
return csh;
}
private HashLoginService getHashLoginService() {
//create the login service, assign the realm and read the user credentials
//from the file /tmp/realm.properties.
HashLoginService hls = new HashLoginService();
hls.setName(REALM);
hls.setConfig("/tmp/realm.properties");
hls.setRefreshInterval(0);
return hls;
}
OriginalL'auteur jspyeatt | 2011-11-08
Vous devez vous connecter pour publier un commentaire.
J'ai eu ce travail et posté un échantillon webapp ici
OriginalL'auteur Jesper J.
Le code est largement ok.
Mon interface est légèrement différente pour l'ajout de la ConstraintMapping comme le seul CM ajouter semble avoir disparu de ma version de la jetée 7.
Bar que mon code est fondamentalement identique et ne travailler pour moi.
Noter qu'une fois authentifié votre navigateur ne vous demandera pas de nouveau à moins que vous redémarrez votre navigateur ou de suivre les instructions ici
OriginalL'auteur user1140607