如何保护转发网址

时间:2014-04-29 08:53:06

标签: java spring spring-mvc spring-security

有没有办法保护转发网址?

要清楚,我是一个错误处理程序:

@Component
public class MyAuthenticationFailureHandler implements
        AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest request,
            HttpServletResponse response, AuthenticationException exception)
            throws IOException, ServletException {

        if (exception.getClass().isAssignableFrom(
                MyException.class)) {
            MyException myException = (MyException) exception;
            RequestDispatcher dispatcher = request.getRequestDispatcher("/signup/exception");
            request.setAttribute("userID", myException.getUserID());
            dispatcher.forward(request, response);
        }
    }

}

和网络控制器:

@Controller
@RequestMapping("/signup")
public class SignupController {

    @RequestMapping("/exception")
    public ModelAndView signup() {
        ModelAndView model = new ModelAndView(new InternalResourceView("/WEB-INF/jsp/signup.jsp", true));
        return model;
    }

}

我希望路由http://{hostname:port}/signup/exception只能从我自己的处理程序转发,而不能直接访问(通过在浏览器栏上编写URL和params)。

2 个答案:

答案 0 :(得分:0)

添加类似

的属性
request.setAttribute("isForwarded","True")
在处理程序中

并检查控制器中的该属性。

如果是,请继续重定向到适当的页面。

答案 1 :(得分:0)

我没有测试它,但我知道servlet容器拒绝回答相对于WEB-INF的请求,但是你可以转发到WEB-INF下的jsp servlet。可能你可以尝试使用像/ WEB-INF / signup / exception这样的网址吗?

或者,我认为您正在使用Spring Security。我认为安全过滤器不适用于转发的请求。什么给出了以下拦截网址?

<intercept-url pattern="/signup/exception" access="denyAll" />