the*_*y05 1 java rest web-services jersey web
我的网站转到一个登录页面,我想在用户登录时重定向到另一个页面。我有一个“POST”方法将“用户名”和“密码”发送到服务器,服务器检查用户名和密码存在。
这是我的方法
@POST
@Path("logIn")
public void signIn(@PathParam("profileName") String profileName, @PathParam("password") String password) {
if (profileService.getProfile(profileName) != null && (profileService.getPassword(profileName)).equals(password)){
//Render a new page ex "feed.jsp"
}
else {
//send unsucessful message back to client??
}
Run Code Online (Sandbox Code Playgroud)
客户端能够正确发布用户名和密码并检查它是否存在......我只是不知道如何让它呈现(重定向到???)一个新页面
重定向,(使用Response.seeOther(URI),将任何需要的值作为查询参数传递。例如
@POST
@Path("logIn")
public Response login(@Context ServletContext context) {
UriBuilder uriBuilder = UriBuilder.fromUri(URI.create(context.getContextPath()));
uriBuilder.path(.. <path-to-your-jsp> ..);
uriBuilder.queryParam("key1", "value1");
uriBuilder.queryParam("key1", "value2");
URI uri = uriBuilder.build();
return Response.seeOther(uri).build();
}
Run Code Online (Sandbox Code Playgroud)
使用 Jersey 的JSP MVC 特性,这里也有明确的演示。例如
@POST
@Path("logIn")
public Viewable login() {
Map<String, String> model = new HashMap<>();
model.put("key1", "value1");
model.put("key2", "value2");
return new Viewable("/feed", model);
}
Run Code Online (Sandbox Code Playgroud)
feed.jsp
<html>
<body>
<p>${it.key1}</p>
<p>${it.key2}</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
旁白:您真的要在 URI 路径中传递密码吗?这是一个巨大的安全风险。最好在请求正文中实际传递它。
现在我考虑了一下,您应该始终按照POST/REDIRECT/GET模式从登录 POST重定向。如果您想在整个方法中使用 JSP MVC,您可以让控制器返回Viewable登录页面(在 GET 上),并在成功(POST)时重定向到提要控制器,否则重定向回相同的登录页面(得到)。有几种不同的解决方案。
例如
@Path("/login")
public class LoginController {
@GET
public Viewable loginPage() {
...
return new Viewable("/login", model); // to login.jsp
}
@POST
public Response loginPost(Form form, @Context UriInfo uriInfo) {
...
UriBuilder builder = uriInfo.getBaseUriBuilder();
if (success) {
builder.path("/feed"); // to FeedController GET
} else {
builder.path("/login"); // to LoginController GET
}
return Response.seeOther(builder.build()).build();
}
}
@Path("/feed")
public class FeedController {
@GET
public Viewable getFeed() {
...
return new Viewable("/feed", model); // to feed.jsp
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5905 次 |
| 最近记录: |