在RESTEasy和JAX-RS

时间:2018-07-10 15:49:14

标签: java jax-rs resteasy

我想使用RESTEasy和JAX-RS实现简单的身份验证,以便只有具有正确用户名和密码的用户才能访问该服务。我的网络应用程序代码如下:

@GET
@Path("/{param}")
public Response printMessage(@PathParam("param") String msg, @Context HttpHeaders headers) {
    String result = null;
    String username = headers.getRequestHeader("username").get(0);
    String password = headers.getRequestHeader("password").get(0);
    if(username.equals("hello2017") && password.equals("bye")) { 
        result =  "Restful example : " + msg + " from " + headers.getRequestHeader("user-agent").get(0) + " " + headers.getRequestHeader("username").get(0)+ " " + headers.getRequestHeader("password").get(0) + " " + hashString2( headers.getRequestHeader("password").get(0));
        return Response.status(200).entity(result).build();
    }else {
        result="Not Authorized";
        return Response.status(404).entity(result).build();
        //
    }
}

我的web.xml看起来像这样:

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

    <context-param>
        <param-name>resteasy.resources</param-name>
        <param-value>de.locationservice.LocationService</param-value>
    </context-param>

    <listener>
        <listener-class>
            org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
        </listener-class>
    </listener>
    <servlet>
        <servlet-name>resteasy-servlet</servlet-name>
        <servlet-class>
            org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
        </servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>resteasy-servlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

但是我不认为这是正确的方法,因为我仅使用它实现了简单的身份验证。我正在尝试关注this,但是由于我是这个主题的新手,所以我仍然不了解它。

0 个答案:

没有答案