我正在使用" SPRING MVC"当我们收到错误时,我想将用户重定向到上一页"访问被拒绝"所以我创建了一个来自" AccessDeniedHandle"
的工具我的问题是如何才能获得上一页的链接?或者我应该使用控制器? 有什么建议吗?
答案 0 :(得分:-1)
我找到了使用控制器的解决方案,但如果你认为你有更好的建议请分享
<强>弹簧security.xml文件
<security:http...>
...
<security:access-denied-handler error-page="/403" />
...
</security:http...>
<强> myController.java
@RequestMapping(value="/403", method=RequestMethod.GET)
public void accessDenied(HttpSession session,HttpServletRequest request, HttpServletResponse response, Model model, Authentication auth) {
String previousPage = (String) session.getAttribute("previousPage");
if(previousPage.isEmpty())
{
if(request.isUserInRole("ROLE_ADMIN"))
{
try {
response.sendRedirect("olx/index");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if(request.isUserInRole("ROLE_USER"))
{
try {
response.sendRedirect("me/index");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
else
{
try {
response.sendRedirect(previousPage);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
每个jsp中的
<% Object thisPage = request.getAttribute("javax.servlet.forward.request_uri");
session.setAttribute("previousPage",thisPage); %>