Dans cet article, nous vous montrons comment implémenter l’authentification de conteneur avec JAX-WS , sous Tomcat 6.0. De cette manière, l’authentification est déclarative plutôt que programmatique comme ceci: authentification d’application dans JAX-WS . Et Tomcat implémente l’authentification du conteneur via security realm .
À la fin de cet article, le service Web déployé authentifiera l’utilisateur en fonction des données d’authentification stockées dans le fichier
conf/tomcat-users.xml
de Tomcat.
1. WebService
Créez un style simple JAX-WS, RPC.
File: UserProfile.java
package com.mkyong.ws; import javax.jws.WebMethod; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; //Service Endpoint Interface @WebService @SOAPBinding(style = Style.RPC) public interface UserProfile{ @WebMethod String getUserName(); }
File: UserProfileImpl.java
package com.mkyong.ws; import javax.jws.WebService; //Service Implementation Bean @WebService(endpointInterface = "com.mkyong.ws.UserProfile") public class UserProfileImpl implements UserProfile{ @Override public String getUserName() { return "getUserName() : returned value"; } }
2. web.xml
Configurez un «opérateur» de rôle de sécurité, définissez l’url «/utilisateur» comme authentification HTTP de base requise Voir ci-dessous le fichier
web.xml
, explicite.
Fichier: web.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app__2__3.dtd"> <web-app> //... <security-role> <description>Normal operator user</description> <role-name>operator</role-name> </security-role> <security-constraint> <web-resource-collection> <web-resource-name>Operator Roles Security</web-resource-name> <url-pattern>/user</url-pattern> </web-resource-collection> <auth-constraint> <role-name>operator</role-name> </auth-constraint> <user-data-constraint> <transport-guarantee>NONE</transport-guarantee> </user-data-constraint> </security-constraint> <login-config> <auth-method>BASIC</auth-method> </login-config> <servlet-mapping> <servlet-name>user</servlet-name> <url-pattern>/user</url-pattern> </servlet-mapping> //... </web-app>
<user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint>
Voir cet article - lien://tomcat/comment-configurer-tomcat-au-support-ssl-ou-https/[Faire que Tomcat prenne en charge les connexions SSL ou https]
3. Utilisateurs Tomcat
Ajoutez un nouveau rôle, un nom d’utilisateur et un mot de passe dans le fichier
$ Tomcat/conf/tomcat-users.xml
. Dans ce cas, ajoutez le nouvel utilisateur «mkyong», «123456 ′» et associez-le à un rôle nommé «opérateur».
Fichier: $ Tomcat/conf/tomcat-users.xml
<?xml version='1.0' encoding='utf-8'?> <tomcat-users> <role rolename="tomcat"/> <role rolename="operator"/> <user username="tomcat" password="tomcat" roles="tomcat"/> <user username="mkyong" password="123456" roles="operator"/> <user name="admin" password="admin" roles="admin,manager"/> </tomcat-users>
4. Le royaume de Tomcat
Configurez le domaine de sécurité dans le fichier
$ Tomcat/conf/server.xml
. Dans ce cas, utilise par défaut
UserDatabaseRealm
pour lire les informations d’authentification dans` $ Tomcat/conf/tomcat-users.xml`.
File: $ Tomcat/conf/server.xml
<GlobalNamingResources> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml"/> </GlobalNamingResources> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
5. Déployer le service Web JAX-WS sur Tomcat
Consultez ce guide détaillé sur le lien://services Web/jax-ws/deploy-jax-ws-services-web-sur-tomcat/[comment déployer des services Web JAX-WS sur Tomcat].
6. Test
Maintenant, tout accès à votre service Web déployé est une authentification obligatoire du nom d’utilisateur et du mot de passe, voir figure: URL: http://localhost : 8080/WebServiceExample/user
7. Client WebService
Pour accéder au service Web déployé, associez un nom d’utilisateur et un mot de passe corrects, comme suit:
UserProfile port = service.getPort(UserProfile.class); BindingProvider bp = (BindingProvider) port; bp.getRequestContext().put(BindingProvider.USERNAME__PROPERTY, "mkyong"); bp.getRequestContext().put(BindingProvider.PASSWORD__PROPERTY, "123456");
Fichier: WsClient.java
package com.mkyong.client; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.BindingProvider; import javax.xml.ws.Service; import com.mkyong.ws.UserProfile; public class WsClient{ //can't parse wsdl "http://localhost:8080/WebServiceExample/user.wsdl" directly //save it as local file, and parse it private static final String WS__URL = "file:c://user.wsdl"; public static void main(String[]args) throws Exception { URL url = new URL(WS__URL); QName qname = new QName("http://ws.mkyong.com/", "UserProfileImplService"); Service service = Service.create(url, qname); UserProfile port = service.getPort(UserProfile.class); //add username and password for container authentication BindingProvider bp = (BindingProvider) port; bp.getRequestContext().put(BindingProvider.USERNAME__PROPERTY, "mkyong"); bp.getRequestContext().put(BindingProvider.PASSWORD__PROPERTY, "123456"); System.out.println(port.getUserName()); } }
sortie
getUserName() : returned value
-
Remarque ** Pour les clients ayant fourni un nom d’utilisateur ou un mot de passe non valide, Tomcat renverra l’exception suivante:
Exception in thread "main" com.sun.xml.internal.ws.client.ClientTransportException: request requires HTTP authentication: Unauthorized
Terminé.
Télécharger le code source
Téléchargez-le - lien://wp-content/uploads/2010/12/JAX-WS-Container-Authentication-Example.zip[JAX-WS-Container-Authentication-Example.zip](11 Ko)
Référence
-
http://tomcat.apache.org/tomcat-6.0-doc/realm-howto.html [Tomcat realm
COMMENT]. http://docs.sun.com/app/docs/doc/819-3669/bnccv?a=view [Example: Basic
Authentification avec JAX-WS]. http://www.ryandelaplante.com/2007/06/ssl-and-http-basic-authentication-with.html [SSL
et authentification HTTP BASIC avec Glassfish et JAX-WS]
lien://étiquette/authentification/[authentification]lien://étiquette/jax-ws/[jax-ws] tomcat services web