在Web服务中传递身份验证参数

时间:2012-05-22 14:15:20

标签: json web-services authentication header

我正在构建一个webservice服务器端。在我的webservice中,每个客户端都需要通过json格式的POST方法为它们发出的每个请求传递其身份验证参数。通过帖子传递参数是一个好习惯吗?

一个人告诉我,我应该总是使用GET方法来检索数据; POST应仅用于插入。如果是这样,我将如何通过身份验证参数?一个可以通过URL,另一个可以通过标头值。我应该使用哪种方式?

1 个答案:

答案 0 :(得分:0)

尝试实施此Web服务。此Web服务允许通过标头值传递其身份验证参数。

@WebService(serviceName="authentication")
public class WSAuthentication {
String name = null;
String password = null;

public WSAuthentication() {
   super();
}

public WSAuthentication(String name, String password) {
   this.name = name;
   this.password = password;
}

private static String getData(WSAuthentication sec) {
   System.out.println("********************* AUTHENTICATION ********************" + "\n" + 
   "**********USER: " + sec.name + "\n" + 
   "******PASSWORD: " + sec.password + "\n" + 
   "******************************** AUTHENTICATION ****************************");
   return sec.name + " -- " + sec.password;
}

@WebMethod(operationName="security", action="authenticate")
@WebResult(name="answer")
public String security(@WebParam(header=true, mode=Mode.IN, name="user") String user, @WebParam(header=true, mode=Mode.IN, name="password") String password) {
    WSAuthentication secure = new WSAuthentication(user, password);
    return getData(secure);
 }
}

我使用POST方法进行响应。我希望能帮到你。

相关问题