【问题标题】:JDBC Realm authentication doesn't detect password changes immediatelyJDBC Realm 身份验证不会立即检测到密码更改
【发布时间】:2016-06-25 20:25:35
【问题描述】:

我正在为我的 Web 应用程序使用 JdbcRealm 和基于 FORM 的身份验证,它是使用 JSF 创建的。我创建了一个用于用户管理的网页。问题是当用户更改密码时,jdbcRealm 不会立即反映更改并重定向到错误页面。

我正在使用具有以下配置的 Jetty 插件进行开发:

web.xml

  <login-config>
        <auth-method>FORM</auth-method>
        <realm-name>RealmName</realm-name>
          <form-login-config> 
             <form-login-page>/login.xhtml</form-login-page> 
             <form-error-page>/access-denied.xhtml</form-error-page> 
         </form-login-config> 
    </login-config>
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>secured</web-resource-name>
            <description/>
            <url-pattern>/views/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>USER</role-name>
            <role-name>ADMINISTRATOR</role-name>
        </auth-constraint>
    </security-constraint> 

pom.xml

<plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.3.7.RC1</version>
                <configuration>
                    <scanIntervalSeconds>10</scanIntervalSeconds>
                    <webApp>
                        <contextPath>/ROOT</contextPath>
                    </webApp>
                    <dumpOnStart>true</dumpOnStart>
                    <loginServices>
                        <loginService implementation="org.eclipse.jetty.security.JDBCLoginService">
                            <name>RealmName</name>
                            <config>${project.basedir}/src/main/resources/realm.properties</config>
                        </loginService>
                    </loginServices>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.38</version>
                    </dependency>
                    <dependency>
                        <groupId>c3p0</groupId>
                        <artifactId>c3p0</artifactId>
                        <version>0.9.1.2</version>
                    </dependency>
                </dependencies>
            </plugin>

realm.properties

jdbcdriver = com.mysql.jdbc.Driver
url = urlOfMyDatabase
username = myUser
password = myPassword

usertable = table
usertablekey = id_user
usertableuserfield = username
usertablepasswordfield = password

roletable = role
roletablekey = id_role
roletablerolefield = name

userroletable = user
userroletableuserkey = id_user
userroletablerolekey = role_id
cachetime = 300

我猜是因为缓存时间的原因,但是当我执行用户修改时,如何维护缓存并刷新呢?

以下配置是针对我的开发环境的,对于生产环境,我使用的是具有以下配置的 Tomcat

<Realm className="org.apache.catalina.realm.JDBCRealm" driverName="com.mysql.jdbc.Driver" localDataSource="true" connectionURL="urlOfMyDatabase" connectionName="myUser" connectionPassword="myPassword" userTable="user_view" userNameCol="username" userCredCol="password" userRoleTable="user_view" roleNameCol="role"/>

我希望你能帮助我:)

【问题讨论】:

  • 如何检测到这种情况?如果用户更改了他的密码,他必须已经登录。
  • 如果用户更改密码,注销并尝试验证它会被重定向到错误页面

标签: tomcat jetty jdbcrealm


【解决方案1】:

我猜密码更改在 5 分钟内没有被拾起?检查你的 realm.properties 我是怎么猜到的。提示:查看该文件的末尾,或在此页面上 ctrl+f 300

您可能根本不需要任何缓存:许多用户在 5 分钟后再次登录(从而使用缓存)的概率可能接近于零。因此,此缓存只会消耗您的内存,而不会从中获得任何收益。如果对单个记录的简单查找会减慢您的应用程序或数据库的速度,那么您就会遇到另一个问题。摆脱它。

在没有任何帮助的情况下将缓存放在那里被称为过早优化。而且您会在其他地方发现过早优化是万恶之源。

【讨论】:

    猜你喜欢
    • 2020-03-26
    • 2020-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    相关资源
    最近更新 更多