2016-11-07 6 views
0

WebSphere 8.5.0.2でセキュアなRESTサービスを作成しようとしています。基本認証を使用して保護したい私は自分のweb.xmlを修正し、自動挿入されたSecurityContextを読み込もうと試みました。自動注入されたオブジェクトが得られますが、さまざまな操作が失敗しています。 securityContext.getAuthenticationScheme(); 私は、認証されたレルムのすべてのユーザーに自分の役割を割り当てました。WebsphereでセキュアなRESTコール

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

「失敗している」とはどういう意味ですか?コードからの呼び出し結果と例外があれば例外をシステムに追加してください。 – Gas

答えて

0

私は、RESTクライアントからの正しいパスワードを渡しませんでした。正しい資格情報を提供した後、それは動作し始めました。

関連する問題