我在 Spring 做一个休息应用程序,我有一个如下所示的注销方法。我对 spring 没有了解,所以我只是到处搜索并做了这个。
@RestController
public class LogoutController {
@Autowired
private DatabaseService databaseService;
@RequestMapping(value = "/myApp/user/logout", method = GET, produces = {"application/xml"})
public Users performLogout(@RequestHeader("AuthenticationID") String authID, HttpServletRequest request) throws DatatypeConfigurationException {
return handleLogout(request, authID);
}
private Users handleLogout(HttpServletRequest request, String authID) throws DatatypeConfigurationException {
LogService.info(this.getClass().getName(), "Received Logout Request");
final UsersXMLBuilder usersXMLBuilder = new UsersXMLBuilder();
Users usersXML = usersXMLBuilder.buildDefaultUsersTemplate();
HttpSession session = request.getSession();
AppUtilities utils = new AppUtilities();
try {
//Checking with RegEX
if (utils.isValidUUID(authToken)) {
//Get User Login Record from DB By the AuthID and Delete It
//Invalidate The Session
session.invalidate();
LogService.info(this.getClass().getName(), "Session Invaliated");
} else {
LogService.info(this.getClass().getName(), "Invalid AuthID Found. Not a Valid UUID");
usersXML.setResponseCode(-5);
usersXML.setResponseText("User Session is Not Valid");
}
} catch (Exception ex) {
LogService.error(this.getClass().getName(), ex);
usersXML.setResponseCode(-4);
usersXML.setResponseText("Error Occured!");
return usersXML;
} finally {
LogService.info(this.getClass().getName(), "LogOut Process Finished");
}
return usersXML;
}
}
问题
<小时/>1-当我在请求中没有传递身份验证ID时,当Spring给出白色标签错误页面时,我是否可以返回XML消息。
2-我如何获取身份验证 header 并检查其是否为空并给出缺少 AuthID 的消息。
3-如何显式设置属性并在每个 Controller 中检查它是否存在。
4-我计划有一个表,我可以在其中存储用户登录时间,并为 session 提供 10 分钟的时间,如果我收到带有 AuthID 的用户的任何请求,则更新它更多 10 分钟。那么我可以有一个类或方法来检查任何传入的请求吗?这样我就可以检测 AuthID 并更新我的表。
感谢您的时间和帮助。
最佳答案
拦截器将为每个请求运行。它可以停止请求并自行响应。
关于java - 为每个请求设置属性 (Spring REST),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31050337/