【问题标题】:HTTP 403 when attempt to perform Action尝试执行操作时的 HTTP 403
【发布时间】:2013-07-24 21:16:37
【问题描述】:

我在表单上有一个动作

<form action="LoginAction.do" method="POST">
            <div id="table"
                class="ui-widget-header ui-corner-all ui-widget-content">
                <table align="center" style="width: 100%">
                    <tr>
                        <td align="center"><span
                            style="color: white; font-size: 20px;"><fmt:message
                                    key="lg" /></span></td>
                        <td align="right"><input type="text" id="login" name="login" />
                        <td>
                    </tr>
                    <tr>
                        <td align="center"><span
                            style="color: white; font-size: 20px;"><fmt:message
                                    key="paswd" /></span></td>
                        <td align="right"><input type="text" id="password"
                            name="password" /></td>
                    </tr>
                    <tr>
                        <td align="right" colspan="2"><input type="submit"
                            id="approve" style="font-size: 10px;"
                            value="<fmt:message key='enter'/>" /></td>
                    </tr>
                </table>
            </div>
        </form>

当我按下按钮时它应该执行 LoginAction

HttpSession session = req.getSession();
log.debug("attempt to checkuser");
String page = req.getRequestURI();
String login = req.getParameter("login");
String password = req.getParameter("password");
loginService = new LoginServiceImpl();
if (loginService.checkExists(login, password)) {
     session.setAttribute("enterAttr", true);
     session.setAttribute("loginame", login);
     return page;
}
session.setAttribute("enterAttr", false);
return "redirect:" + page;

在 ActionServet 中它应该检查 Action 的结果,然后重定向到某个页面。

String name = getActionName(req);
Action action = (Action) factory.getAction(name, getClass()
            .getClassLoader());         
String view = action.exec(req, resp);
//router
if (view.startsWith("redirect:")) {
     resp.sendRedirect(view.substring("redirect:".length(),
                view.length()));
} else {
     getServletContext().getRequestDispatcher(
                "/WEB-INF/jsp/" + view + ".jsp").forward(req, resp);

}

但是当我尝试按下按钮时,我得到的是HTTP Status 403 - Access to the requested resource has been denied

问题出在哪里?与 tomcat 用户有关吗?

这里是web.xml

<?xml version="1.0" encoding="Cp1251"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

<context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>messages</param-value>
</context-param>

<servlet>
    <servlet-name>ActionServlet</servlet-name>
    <servlet-class>com.project.reservation.web.ActionServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>ActionServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

<security-role>
    <description>Role for administrator's actions</description>
    <role-name>admin</role-name>
</security-role>

<welcome-file-list>
    <welcome-file>/main.jsp</welcome-file>
</welcome-file-list>


<security-constraint>
    <web-resource-collection>
        <web-resource-name>reservation</web-resource-name>
        <url-pattern>*.do</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>admin</role-name>
    </auth-constraint>
    <user-data-constraint>
        <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>
</security-constraint>

【问题讨论】:

  • 所以当你只满足你的 else 条件时你得到403?如果您尝试将请求重定向(而不是转发)到/WEB-INF/* 中的页面,您将获得403
  • 当我在浏览器中按下 .jsp 页面上的按钮时得到它。问题是我无法调试它 - 调试器没有遇到任何断点(我在 Eclipse 中使用远程调试)
  • 您的操作是否正在执行?例如,是否满足 if/else 条件?
  • 我无法检查 - 调试器没有遇到任何断点。
  • 听起来你的映射有问题。你能显示你的 LoginAction 的映射吗?

标签: jsp tomcat servlets security-constraint


【解决方案1】:

您正在实现自己的身份验证机制,但使用容器 web.xml 文件来指定安全约束。如果我是你,我可以尝试将我的身份验证代码移动到过滤器并从 web.xml 文件中删除安全约束。

【讨论】:

  • 您已将web.xml 设置为保护具有admin 角色的用户访问*.do。如果用户还没有登录,他们肯定没有admin 角色。
  • 我这样解决了 - 我根本不检查用户和登录 - 我只是在他在网站上执行某些操作时才检查 - 所以所有主要操作对所有人都可用,但一些- 我需要检查数据库中的用户。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-15
  • 2012-03-04
  • 1970-01-01
  • 2011-05-15
  • 1970-01-01
  • 1970-01-01
  • 2021-04-11
相关资源
最近更新 更多