在Websphere上安全的REST调用

时间:2016-11-07 22:02:47

标签: apache rest security websphere-8 apache-wink

我正在尝试在WebSphere 8.5.0.2上创建安全的REST服务。我想保证使用基本身份验证。我修改了我的web.xml并尝试读取自动注入的SecurityContext。我得到一个自动注入的对象但是各种操作都失败了,例如securityContext.getAuthenticationScheme(); 我还将我的角色映射到所有认证领域的用户。

我也无法在Wink的文档中找到任何内容。我做错了吗?

我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>RESTModulation</display-name>
    <!-- Wink SDK servlet configuration. 
        This servlet handles HTTP requests
        of SDK web service on application server.-->

<servlet>
    <description>
    JAX-RS Tools Generated - Do not modify</description>
    <servlet-name>EntryRestServlet</servlet-name>
    <servlet-class>com.ibm.websphere.jaxrs.server.IBMRestServlet</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.demo.DemoResourceApplication</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>EntryRestServlet</servlet-name>
    <url-pattern>
    /resources/*</url-pattern>
</servlet-mapping>
<security-constraint id="SecurityConstraint_1">
      <web-resource-collection id="WebResourceCollection_1">
        <web-resource-name>EntryRestServlet</web-resource-name>
        <description>Protection area for Rest Servlet</description>
        <url-pattern>/resources/</url-pattern>
        <http-method>GET</http-method>
        <http-method>POST</http-method>
      </web-resource-collection>
      <auth-constraint id="AuthConstraint_1">
         <description>Role1 for this rest servlet</description>
         <role-name>Role1</role-name>
      </auth-constraint> 
</security-constraint>
<security-role id="SecurityRole_1">
         <description>This is Role1</description>
         <role-name>Role1</role-name>
</security-role>    
<login-config>
      <auth-method>BASIC</auth-method>
      <realm-name>defaultWIMFileBasedRealm</realm-name>
</login-config>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

==========================================================================
Service implementation

@Path("/MyTestService")

public class MyTestService{

    @Context 
    SecurityContext securityContext;

    @GET
    @Path("/getUser1")
    @Produces(MediaType.TEXT_PLAIN)
    public Response doInquiry()throws Exception {
        String jsonData= "{'user':'I am here '}";

        String authnScheme = securityContext.getAuthenticationScheme();
           System.out.println("authnScheme : " + authnScheme);
           // retrieve the name of the Principal that invoked the resource
           String username = securityContext.getUserPrincipal().getName();
           System.out.println("username : " + username);
           // check if the current user is in Role1 
            Boolean isUserInRole = securityContext.isUserInRole("Role1");
            System.out.println("isUserInRole : " + isUserInRole);
return Response.status(Response.Status.OK).entity(jsonData).build();
    }
}

1 个答案:

答案 0 :(得分:0)

我没有从REST客户端传递正确的密码。提供正确的凭据后,它已开始工作。