【问题标题】:Java forms auth with j_security_check and GlassfishJava 使用 j_security_check 和 Glassfish 形成身份验证
【发布时间】:2012-08-09 17:48:09
【问题描述】:

我正在涉足 Java 的浑水,我正在通过 PackPub JavaEE 6 With Netbeans7 book 工作。早期的示例之一是表单身份验证,它涉及使用安全角色“管理员”和约束设置部署描述符 (web.xml)。然后,它会引导您通过将这些角色分配给新组,然后使用 Glassfish 控制台在这些组中创建新用户来获取 Glassfish 描述符。

当我尝试访问此受保护页面内的页面时,我会按预期看到登录页面,但我的登录不起作用。即使我知道我正在输入在 glassfish 控制台中创建的有效凭据,我仍然会看到我的登录错误页面(在 j_security_check URL 上呈现)。

登录页面非常基本:

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Log in to view secure content</title>
    </head>
    <body>
        <h1>Log in</h1>
        <form action="j_security_check" method="POST">
            <table border="0">
                <tbody>
                    <tr>
                        <td slign="right">Username: &nbsp;</td>
                        <td><input type="text" name="j_username" value="" /></td>
                    </tr>
                    <tr>
                        <td slign="right">Password: &nbsp;</td>
                        <td><input type="password" name="j_password" value="" /></td>
                    </tr>
                    <tr>
                        <td></td>
                        <td><input type="submit" value="Login" /></td>
                    </tr>
                </tbody>
            </table>

        </form>
    </body>
</html>

我没有配置一些东西,它可能是一些非常基本的东西,但是这本书没有帮助解决诸如此类的问题,所以想知道我是否可以得到一些关于从哪里开始调试或诊断这个问题的指针。

我的 web.xml 是:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <security-constraint>
        <display-name>Admin Pages</display-name>
        <web-resource-collection>
            <web-resource-name>Administrative pages</web-resource-name>
            <description/>
            <url-pattern>/admin/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <description/>
            <role-name>Admin</role-name>
        </auth-constraint>
    </security-constraint>
    <login-config>
        <auth-method>FORM</auth-method>
        <realm-name>file</realm-name>
        <form-login-config>
            <form-login-page>/login.jsp</form-login-page>
            <form-error-page>/loginerror.jsp</form-error-page>
        </form-login-config>
    </login-config>
    <security-role>
        <description>Administrators</description>
        <role-name>Admin</role-name>
    </security-role>
    <security-role>
        <description>public user</description>
        <role-name>User</role-name>
    </security-role>
</web-app>

glassfish-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app error-url="">
  <security-role-mapping>
    <role-name>Admin</role-name>
    <group-name>Admin</group-name>
  </security-role-mapping>
  <class-loader delegate="true"/>
  <jsp-config>
    <property name="keepgenerated" value="true">
      <description>Keep a copy of the generated servlet class' java code.</description>
    </property>
  </jsp-config>
</glassfish-web-app>

我已经在 Glassfish 控制台中仔细检查过,我正在编辑

Configuartions|Security|Realms|file

并且我的新用户有一个“管理员”组列表

谢谢

【问题讨论】:

  • 你检查过密钥文件吗?
  • @alf:不,那在哪里?我才刚刚开始,所以不知道在哪里/如何
  • 如果您没有在管理控制台中更改它,它的路径是 domain-dir/config/keyfile。打开它并验证它是否包含您的用户。
  • 也许thisquestin 的答案可能对你和我一样有帮助?

标签: java glassfish-3 netbeans-7 j-security-check


【解决方案1】:

您是否使用自定义登录页面?

如果你配置了所有的东西,也看看你的登录页面,你应该有类似的东西

<form method="POST" action="j_security_check">
    <input type="submit">
    <!-- more code -->
</form>

试试

<login-config>    
  <auth-method>BASIC</auth-method>    
  <realm-name>file</realm-name>  
</login-config>

【讨论】:

  • 嗨 Luis,是的,它是一个自定义登录页面。我已将代码添加到原始帖子中。
  • 看起来更像 glassfish 配置而不是你的网络应用程序,因为我从未尝试过那个我可能没有太大帮助。但是尝试将登录配置更改为基本 BASICfile
【解决方案2】:

是的。大约 2 年前,但我遇到了同样的问题并找到了解决方案。尽管我已经安装了正确版本的 Netbeans (7.0) 和正确版本的 jdk (1.6.0_45) 以遵循本书,但 Netbeans 附带 glassfish 3.1。所以我所做的是从 Netbeans 中删除 glassfish 服务器,将其卸载,然后安装 glashfish 3.0.1,所以现在我的示例运行良好。不要忘记以系统管理员身份执行 Netbeans。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-07
    • 1970-01-01
    • 2011-01-13
    • 2017-02-16
    • 2013-08-29
    • 1970-01-01
    • 2014-08-18
    相关资源
    最近更新 更多