我有一个使用struts,spring,hibernate构建的应用程序。在这个应用程序中,我想建立休息服务。此休息服务将用于Android应用程序。在此应用程序中,用户和角色存储在我的数据库中。用户表中的用户名和密码以及角色表中的角色。 我想问的是,如何验证想要访问其余服务的客户端。因此,要在休息服务中访问资源,客户端必须首先登录。如果用户尚未登录,则其余的将始终返回String“not_authenticated”。通常在Web应用程序中,这使用会话。和其他人一样吗?
编辑以添加我当前的代码并更新我的问题
@POST
@Path("/login")
@Produces(MediaType.APPLICATION_JSON)
public Response login(@FormParam("username") String username,
@FormParam("password") String password){
MessageDto messageDto = customerService.autenticate(username, password);
if(messageDto.getResult().equals("success")){
return Response.ok(messageDto)
.cookie(new NewCookie("customerId", messageDto.getMessage()))
.build();
}else{
return Response.ok(messageDto)
.build();
}
}
MessageDto是一个具有两个变量String结果和String消息的对象。例如,当客户端成功登录时,json返回就像这样
{"result":"success","message":"1-admin"}
当客户端未成功登录时
{"result":"error","message":"Your username is not registered"}
答案 0 :(得分:3)
在登录成功时使用泽西响应对象设置用户cookie(如userId,token等...) HTTPONLY proprerty,
并使用控制器中的 @CookieParam 获取该Cookie,如果找不到匹配项,请首先验证您的数据库记录,然后再将其重定向到您的登录页面,否则请继续。