Java使用j_security_check和Glassfish形成auth

问题描述 投票:0回答:2

我正在把我的脚趾浸入Java的阴暗水域,我正在使用PackPub JavaEE 6和Netbeans7 book。早期示例之一是表单身份验证,其中涉及使用安全角色“admin”和约束来设置部署描述符(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

我的新用户有一个'Admin'组列表

谢谢

java glassfish-3 netbeans-7 j-security-check
2个回答
0
投票

你在使用自定义登录页面吗?

如果您配置了所有内容,请查看您的登录页面,您应该有类似的东西

<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>

0
投票

是啊。大约2年前,但我遇到了同样的问题并建立了解决方案。虽然我已经安装了正确版本的Netbeans(7.0)和正确版本的jdk(1.6.0_45)来跟随这本书,但Netbeans附带了glassfish 3.1。所以我所做的是从Netbeans中删除glassfish服务器,卸载它,并安装glashfish 3.0.1,所以现在我的示例工作得很好。不要忘记以系统管理员身份执行Netbeans。

© www.soinside.com 2019 - 2024. All rights reserved.