Abilitazione dell'autenticazione di base per l'accesso ai servizi Web

È possibile configurare l'autenticazione di base per le applicazioni client per accedere ai servizi Web.

Informazioni su questa attività

Se è necessario utilizzare l'applicazione client del servizio Web con l'autenticazione di base per accedere alle risorse del servizio Web protetto, il client deve fornire il nome utente e la password nella richiesta durante la comunicazione con il fornitore del servizio.

Procedura

  1. Abilitare le funzioni jaxws-2.2, servlet-3.0(o servlet-3.1) e appSecurity-2.0 nel file server.xml .
    <featureManager>
      <feature>jaxws-2.2</feature>
      <feature>servlet-3.0</feature>
      <feature>appSecurity-2.0</feature>
    </featureManager>
  2. Configurare il realm di login nel file server.xml e collegare il realm al provider del servizio.
    <application id="TransportSecurityProvider" name="TransportSecurityProvider"
                 location="TransportSecurityProvider.war" type="ear">
            <application-bnd>
                <security-role name="Employee">
                    <user name="employee0" />
                    <group name="employeeGroup" />
                </security-role>        
                <security-role name="Manager">
                    <user name="manager0" />
                </security-role>
                <security-role name="AllAuthenticated">
                    <special-subject type="ALL_AUTHENTICATED_USERS" />
                </security-role>
            </application-bnd>
        </application>
        
        <basicRegistry id="basic" realm="BasicRealm">
            <user name="employee0" password="emp0pwd" />
            <user name="employee1" password="emp1pwd" />
            <user name="manager0" password="mgr0pwd" />
            <group name="employeeGroup">
                <member name="employee0" />
                <member name="employee1" />
            </group>
        </basicRegistry>   
  3. Configurare il provider del servizio specificando gli endpoint del servizio web.
    1. Crea servizi Web.
      @WebService(serviceName = "SayHelloPojoService",
                  portName = "SayHelloPojoPort")
      public class SayHelloPojoService implements SayHelloService {
      	...
      }
      
      @WebService(serviceName = "SayHelloStatelessService",
                  portName = "SayHelloStatelessPort",
                  endpointInterface = "com.ibm.ws.jaxws.transport.server.security.SayHelloService")
      @Stateless(name = "SayHelloSessionBean")
      public class SayHelloStatelessService implements SayHelloLocal {
      	...
      }
    2. Configurare il file ibm-ws-bnd.xml per il fornitore del servizio.
      <?xml version="1.0" encoding="UTF-8"?>
      <webservices-bnd xmlns="http://websphere.ibm.com/xml/ns/javaee" 
      		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      		xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-ws-bnd_1_0.xsd" 
      		version="1.0">
      	<http-publishing>
      		<webservice-security> 
      			<security-constraint>
      				<web-resource-collection>
      					<web-resource-name>Only Managers</web-resource-name>
      					<url-pattern>/manager/*</url-pattern>
      					<http-method>GET</http-method>
      					<http-method>POST</http-method>
      				</web-resource-collection>
      				<auth-constraint id="AuthConstraint_manager">
      					<role-name>Manager</role-name>
      				</auth-constraint>
      			</security-constraint>
      			<security-constraint>
      				<web-resource-collection>
      					<web-resource-name>Employees</web-resource-name>
      					<url-pattern>/employee/*</url-pattern>
      					<http-method>GET</http-method>
      					<http-method>POST</http-method>
      				</web-resource-collection>
      				<auth-constraint id="AuthConstraint_employee">
      					<role-name>Employee</role-name>
      				</auth-constraint>
      			</security-constraint>
      			<!-- SECURITY ROLES -->
      			<security-role id="Staff">
      				<role-name>Employee</role-name>
      				<role-name>Manager</role-name>
      			</security-role>
      			<!-- AUTHENTICATION METHOD: Basic authentication -->
      			<login-config id="LoginConfig">
      				<auth-method>BASIC</auth-method>
      				<realm-name>Authentication</realm-name>
      			</login-config>                    
      		</webservice-security>
      	</http-publishing>
      </webservices-bnd>
      Nota:
      • Il file ibm-ws-bnd.xml deve trovarsi nella directory /WEB-INF di un'applicazione Web o nella directory /META-INF di un'applicazione del servizio Web basata su EJB (archivio JAR).
      • L'elemento login-config nel file ibm-ws-bnd.xml diventa effettivo solo in un'applicazione del servizio Web basata su EJB (archivio JAR). Per un'applicazione web, l'elemento login-config viene ignorato e viene utilizzato il valore dello stesso elemento nel file web.xml .
  4. Configurare il servizio client specificando gli endpoint del servizio Web. Ad esempio, l'applicazione client è un'applicazione Web denominata TransportSecurityClient.war.
    1. Configurare l''applicazione client nel file server.xml .
      <application id="TransportSecurityClient" name="TransportSecurityClient" 
      		location="TransportSecurityClient.war"
      		context-root="TransportSecurityClient" type="war" />
    2. Configurare i file ibm-ws-bnd.xml per l'applicazione client.
      <?xml version="1.0" encoding="UTF-8"?>
      <webservices-bnd xmlns="http://websphere.ibm.com/xml/ns/javaee" 
      		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      		xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-ws-bnd_1_0.xsd"
      		version="1.0">
      	<!-- POJO service reference binding-->
      	<service-ref name="service/SayHelloPojoService">
      		<port name="SayHelloPojoPort" 
      				namespace="http://ibm.com/ws/jaxws/transport/security/"
      				username="employee1"
      				password="{xor}OjIvbi8oOw=="
      				/>
      	</service-ref>
      	<!-- Stateless service reference binding-->
      	<service-ref name="service/SayHelloStatelessService">
      		<port name="SayHelloStatelessPort" 
      				namespace="http://ibm.com/ws/jaxws/transport/security/"
      				username="employee1"
      				password="{xor}OjIvbi8oOw=="
      				/>
      	</service-ref>
      </webservices-bnd>
      Nota:
      • Il file ibm-ws-bnd.xml deve essere nella directory /WEB-INF dell'applicazione web del client.
      • I valori degli attributi username e password devono corrispondere al nome utente e alla password dell'elemento basicRegistry nel file server.xml . La password può essere codificata utilizzando il comando securityUtility .
    3. Generare gli stub del client utilizzando l'ubicazione wsdl
      @WebServiceClient(name = "SayHelloPojoService",
                        targetNamespace = "http://ibm.com/ws/jaxws/transport/security/",
                        wsdlLocation = "https://localhost:8020/TransportSecurityProvider/unauthorized/employPojoService?wsdl")
      public class SayHelloPojoService
                      extends Service
      {...}
      
      @WebServiceClient(name = "SayHelloStatelessService",
          targetNamespace = "http://ibm.com/ws/jaxws/transport/security/",
          wsdlLocation = "https://localhost:8020/TransportSecurityProvider/unauthorized/EmployStatelessService?wsdl")
      public class SayHelloStatelessService
                      extends Service
      {...}
    4. Utilizzare l'annotazione @WebServiceRef per inserire il servizio Web nel servlet. Ad esempio, TestJaxWsTransportSecurityServlet.
      @WebServiceRef(name = "service/SayHelloPojoService")
      SayHelloPojoService pojoService;
      
      @WebServiceRef(name = "service/SayHelloStatelessService")
      SayHelloStatelessService statelessService;