我有一个带有以下代码的 spring mvc Web 应用程序。当用户未登录时,我将发送一个图 block View 。
当用户登录时,我将重定向到特定的网址模式。
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login() throws IOException {
if (logger.isDebugEnabled()) {
logger.debug("Requested with /login mapping");
}
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (!(authentication instanceof AnonymousAuthenticationToken)) {
List<String> userRoles = AuthenticationUtils.getUserRoles();
if (userRoles.contains("ROLE_ADMIN")) {
return "redirect:/admin.html";
} else if (userRoles.contains("ROLE_USER")) {
return "redirect:/user.html";
}
}
return "template";
}
我得到了重定向,但有一些意外的参数。如何删除它们?
http://localhost:8081/app/admin.html?total=48&loggedInUserRoles=207
我尝试了以下网址,但没有成功。
Spring MVC Controller: Redirect without parameters being added to my url
我不知道哪部分代码添加了参数。
最佳答案
您可以让您的方法返回 View
而不是 String
,然后以某种方式创建 RedirectView
:
RedirectView view = new RedirectView(url);
view.setExposeModelAttributes(false);
return view;
关于java - Spring MVC 重定向是在 url 中添加一些参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31187747/