啟用 Web 服務存取的基本鑑別
您可以配置基本鑑別,供用戶端應用程式存取 Web 服務。
關於這項作業
如果您需要使用 Web 服務用戶端應用程式並透過基本鑑別來存取受保護的 Web 服務資源,用戶端與服務提供者通訊時,必須在要求中提供使用者名稱和密碼。
程序
- 在 server.xml 檔中啟用
jaxws-2.2、servlet-3.0(或servlet-3.1) 和appSecurity-2.0特性。<featureManager> <feature>jaxws-2.2</feature> <feature>servlet-3.0</feature> <feature>appSecurity-2.0</feature> </featureManager> - 在 server.xml 檔中配置登入網域範圍,並將網域範圍連結至服務提供者。
<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> - 指定 Web 服務端點來配置服務提供者。
- 建立 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 { ... } - 配置服務提供者的 ibm-ws-bnd.xml 檔案 。
<?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>附註:- ibm-ws-bnd.xml 檔必須在 Web 應用程式的 /WEB-INF 目錄中,或在 EJB 型 Web 服務應用程式(JAR 保存檔)的 /META-INF 目錄中。
- ibm-ws-bnd.xml 檔中的
login-config元素只會在 EJB 型 Web 服務應用程式 (JAR 保存檔) 中生效。 對於 Web 應用程式,系統不會處理login-config元素,會使用 web.xml 檔中相同元素的值。
- 建立 Web 服務。
- 指定 Web 服務端點來配置服務用戶端。 例如,用戶端應用程式是名為 TransportSecurityClient.war的 Web 應用程式。
- 在 server.xml 檔中配置用戶端應用程式。
<application id="TransportSecurityClient" name="TransportSecurityClient" location="TransportSecurityClient.war" context-root="TransportSecurityClient" type="war" /> - 配置用戶端應用程式的 ibm-ws-bnd.xml 檔。
<?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>附註:- ibm-ws-bnd.xml 檔必須在用戶端 Web 應用程式的 /WEB-INF 目錄中。
- username 和 password 屬性的值必須符合 server.xml 檔中
basicRegistry元素的使用者名稱和密碼。 可以使用securityUtility指令來編碼密碼。
- 利用 WSDL 位置來產生用戶端 Stub。
@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 {...} - 使用
@WebServiceRef註釋,將 Web 服務注入 Servlet。 例如,TestJaxWsTransportSecurityServlet。@WebServiceRef(name = "service/SayHelloPojoService") SayHelloPojoService pojoService; @WebServiceRef(name = "service/SayHelloStatelessService") SayHelloStatelessService statelessService;
- 在 server.xml 檔中配置用戶端應用程式。