JDBC领域表单身份验证

时间:2016-05-19 19:31:14

标签: java mysql java-ee jdbc glassfish

我尝试使用JDBC Realm Form Authentication为我的应用程序设置安全性,但它不起作用。当我尝试登录页面时只是重新加载我没有收到错误,日志中没有任何内容。

这是web.xml

<error-page>
    <error-code>403</error-code>
    <location>/faces/views/errors/403.xhtml</location>
</error-page>

<login-config>
    <auth-method>FORM</auth-method>
    <realm-name>jdbcRealm</realm-name>
    <form-login-config>
        <form-login-page>/faces/views/account/login.xhtml</form-login-page>
        <form-error-page>/faces/views/account/loginerror.xhtml</form-error-page>
    </form-login-config>
</login-config>

<security-constraint>
    <web-resource-collection>
        <web-resource-name>Admin user</web-resource-name>
        <url-pattern>/faces/views/admin/*</url-pattern>
        <http-method>GET</http-method>
        <http-method>POST</http-method>
    </web-resource-collection>
    <auth-constraint>
        <role-name>admin</role-name>
    </auth-constraint>
</security-constraint>

<security-constraint>
    <web-resource-collection>
        <web-resource-name>Admin user</web-resource-name>
        <url-pattern>/faces/views/users/*</url-pattern>
        <http-method>GET</http-method>
        <http-method>POST</http-method>
    </web-resource-collection>
    <auth-constraint>
        <role-name>admin</role-name>
        <role-name>guest</role-name>
    </auth-constraint>
</security-constraint>

这是glassfish-web.xml

<security-role-mapping>
    <role-name>admin</role-name>
    <group-name>admin</group-name>
</security-role-mapping>

<security-role-mapping>
    <role-name>guest</role-name>
    <group-name>guest</group-name>
</security-role-mapping>

表格

  <form method="POST" action="j_security_check">
                    Username: <input type="text" name="j_username" />
                    Password: <input type="password" name="j_password" />


                    <input type="submit" value="Login" />
                    <input type="reset" value="Reset" />
                </form>
  1. JAAS上下文:jdbcRealm
  2. JNDI:jdbc / kyrspr
  3. 用户表:USER
  4. 用户名栏:NAME
  5. 密码栏:密码
  6. 组表:USERS_GROUP
  7. 群组名称列:GROUP_NAME
  8. 密码加密算法:MD5
  9. 和数据库表

    CREATE TABLE user (
        ADDRESS VARCHAR(255),
        EMAIL VARCHAR(255),
        IMAGE VARCHAR(255),
        NAME VARCHAR(255) PRIMARY KEY NOT NULL,
        PASSWORD VARCHAR(255),
        RATING DOUBLE,
        SPECIALLITY_ID BIGINT(20) ); CREATE UNIQUE INDEX user_NAME_uindex ON user (NAME);
    
    
    CREATE TABLE users_group
    (
        USER_ID VARCHAR(255),
        GROUP_NAME VARCHAR(15) NOT NULL,
        group_id BIGINT(20) PRIMARY KEY NOT NULL,
        CONSTRAINT users_group_user_NAME_fk FOREIGN KEY (USER_ID) REFERENCES user (NAME)
    );
    CREATE UNIQUE INDEX users_group_group_id_uindex ON users_group (group_id);
    CREATE INDEX users_group_user_NAME_fk ON users_group (USER_ID);
    

1 个答案:

答案 0 :(得分:0)

但你有自己的境界吗?尝试做类似的事情(这个例子用于GF + mysql)

$ export $ DB_USER = my_user

$ export $ DB_PASSWORD = my_password

$ $ GLASSFISH_HOME / bin / asadmin create-auth-realm --classname com.sun.enterprise.security.auth.realm.jdbc.JDBCRealm --property db-user = $ DB_USER:db-password = $ DB_PASSWORD: JAAS上下文= jdbcRealm:密码列=密码:数据源-JNDI = JDBC / myds:组表=用户:用户表=用户:组名称列=用户名:摘要算法=无:用户名称 - column = username myRealm

有关更多信息,请查看asadmin create-auth-realm文档。