Java Rest基本身份验证

时间:2016-04-27 08:06:18

标签: java rest authentication basic-authentication

我尝试使用Java Rest Services的第一步。应该使用BASIC身份验证。我使用Glassfish 4.1.1。这是我的ToDo课程。它不起作用。我不知道为什么,我希望有人可以帮助我。

我使用一个类作为根类。一个服务类和一个收集服务的类。

<?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>

1 个答案:

答案 0 :(得分:1)

我认为你应该替换

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

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