Spring Session JDBCTokenRepository覆盖默认表

来源:爱站网时间:2021-09-16编辑:网友分享
我设置了Spring的会话“记住我”选项,并使用JdbcTokenRepositoryImpl作为Spring Session的PersistentTokenRepository。不幸的是,开发人员对表“ ...

问题描述


我设置了Spring的会话“记住我”选项,并使用JdbcTokenRepositoryImpl作为Spring Session的PersistentTokenRepository。不幸的是,开发人员对表“ persistent_logins”的名称进行了硬编码,这不适用于我的应用程序,并且不能在Oracle中使用(因为它需要在SQL查询中指定架构名称)。

问题是:哪个是将默认表名“ persistent_logins”覆盖到另一个的最简单方法?

在这里进行了一些搜索之后,我发现可以重写JdbcTokenRepositoryImpl。但是不幸的是,仅覆盖SQL字符串(请参见下面的代码)无法正常工作。

更新:所以我从这里获取了原始源代码:https://github.com/spring-projects/spring-security/blob/master/web/src/main/java/org/springframework/security/web/authentication/rememberme/JdbcTokenRepositoryImpl.java现在它可以工作了,但是我怀疑这是否是一个最佳解决方案,因为原始库中的任何更改都会使我的代码崩溃。


import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentRememberMeToken;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

/**
Overriding standart Spring's JdbcTokenRepositoryImpl to implement custom table instead of default
 */
public class JdbcTokenRepositoryImplCust extends JdbcTokenRepositoryImpl implements PersistentTokenRepository {
    // ~ Static fields/initializers
    // =====================================================================================

    public static final String DEF_TABLE_SQL = "sb_tst.plan_user_logins";
    public static final String CREATE_TABLE_SQL = "create table " + DEF_TABLE_SQL + " (username varchar(64) not null, series varchar(64) primary key, "
            + "token varchar(64) not null, last_used timestamp not null)";
    public static final String DEF_TOKEN_BY_SERIES_SQL = "select username,series,token,last_used from "+ DEF_TABLE_SQL +" where series = ?";
    public static final String DEF_INSERT_TOKEN_SQL = "insert into " + DEF_TABLE_SQL + "(username, series, token, last_used) values(?,?,?,?)";
    public static final String DEF_UPDATE_TOKEN_SQL = "update " + DEF_TABLE_SQL + " set token = ?, last_used = ? where series = ?";
    public static final String DEF_REMOVE_USER_TOKENS_SQL = "delete from " + DEF_TABLE_SQL + " where username = ?";

    // ~ Instance fields
    // ================================================================================================

    private String tokensBySeriesSql = DEF_TOKEN_BY_SERIES_SQL;
    private String insertTokenSql = DEF_INSERT_TOKEN_SQL;
    private String updateTokenSql = DEF_UPDATE_TOKEN_SQL;
    private String removeUserTokensSql = DEF_REMOVE_USER_TOKENS_SQL;
    private boolean createTableOnStartup;

    protected void initDao() {
        if (createTableOnStartup) {
            getJdbcTemplate().execute(CREATE_TABLE_SQL);
        }
    }

    public void createNewToken(PersistentRememberMeToken token) {
        getJdbcTemplate().update(insertTokenSql, token.getUsername(), token.getSeries(),
                token.getTokenValue(), token.getDate());
    }

    public void updateToken(String series, String tokenValue, Date lastUsed) {
        getJdbcTemplate().update(updateTokenSql, tokenValue, lastUsed, series);
    }

    public PersistentRememberMeToken getTokenForSeries(String seriesId) {
        try {
            return getJdbcTemplate().queryForObject(tokensBySeriesSql,
                    new RowMapper() {
                        public PersistentRememberMeToken mapRow(ResultSet rs, int rowNum)
                                throws SQLException {
                            return new PersistentRememberMeToken(rs.getString(1), rs
                                    .getString(2), rs.getString(3), rs.getTimestamp(4));
                        }
                    }, seriesId);
        }
        catch (EmptyResultDataAccessException zeroResults) {
            if (logger.isDebugEnabled()) {
                logger.debug("Querying token for series '" + seriesId
                        + "' returned no results.", zeroResults);
            }
        }
        catch (IncorrectResultSizeDataAccessException moreThanOne) {
            logger.error("Querying token for series '" + seriesId
                    + "' returned more than one value. Series" + " should be unique");
        }
        catch (DataAccessException e) {
            logger.error("Failed to load token for series " + seriesId, e);
        }

        return null;
    }

    public void removeUserTokens(String username) {
        getJdbcTemplate().update(removeUserTokensSql, username);
    }

    public void setCreateTableOnStartup(boolean createTableOnStartup) {
        this.createTableOnStartup = createTableOnStartup;
    }
}

上一篇:错误转换年份字段的两位数字日期>>

下一篇:映射程序在运行时是否会减慢速度?

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载