2016-04-27 109 views
1

我嘗試使用Java Rest Services進行第一步。它應該與BASIC身份驗證。我使用Glassfish 4.1.1。這是我的ToDo課程。它不起作用。我不知道爲什麼,我希望有人能幫助我。Java Rest基本身份驗證

我使用一個類作爲根類。一個服務類和一個收集服務的類。

<?xml version="1.0" encoding="UTF-8"?> 

<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd"> 
<glassfish-web-app> 
    <context-root>/RestServiceBasicAuth</context-root> 
    <security-role-mapping> 
     <role-name>asadmin</role-name> 
     <group-name>asadmin</group-name> 

    </security-role-mapping> 
</glassfish-web-app> 

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
    id="WebApp_ID" version="3.1"> 

    <servlet> 
     <servlet-name>javax.ws.rs.core.Application</servlet-name> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>javax.ws.rs.core.Application</servlet-name> 
     <url-pattern>/rest-api</url-pattern> 
    </servlet-mapping> 

    <security-constraint> 

     <web-resource-collection> 
      <web-resource-name>RestServiceBasicAuth</web-resource-name> 
      <url-pattern>/RestServiceBasicAuth/rest-api/todos</url-pattern> 
      <http-method>GET</http-method> 
      <http-method>POST</http-method> 
      <http-method>PUT</http-method> 
      <http-method>HEAD</http-method> 
     </web-resource-collection> 

     <auth-constraint> 
      <role-name>asadmin</role-name> 
     </auth-constraint> 

     <user-data-constraint> 
      <transport-guarantee>CONFIDENTIAL</transport-guarantee> 
     </user-data-constraint> 

    </security-constraint> 

    <security-role> 
     <role-name>asadmin</role-name> 
    </security-role> 

    <login-config> 
     <auth-method>BASIC</auth-method> 
     <realm-name>admin-realm</realm-name> 
    </login-config> 

</web-app> 
+0

更換

<url-pattern>/RestServiceBasicAuth/rest-api/todos</url-pattern> 

如果我沒有驗證請求響應它。 – michaeldbjava

+0

非常感謝... – michaeldbjava

回答

1

我想你應該

<url-pattern>/rest-api</url-pattern>