org.wf.jwtp.provider.JdbcTokenStore Maven / Gradle / Ivy
The newest version!
package org.wf.jwtp.provider;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.util.Assert;
import org.wf.jwtp.util.TokenUtil;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* jdbc存储token的实现
* Created by wangfan on 2018-12-28 下午 1:00.
*/
public class JdbcTokenStore implements TokenStore {
protected final Log logger = LogFactory.getLog(this.getClass());
private final JdbcTemplate jdbcTemplate;
private RowMapper rowMapper = new TokenRowMapper();
private static final String UPDATE_FIELDS = "access_token, user_id, permissions, roles, refresh_token, expire_time";
private static final String BASE_SELECT = "select token_id, " + UPDATE_FIELDS + ", create_time, update_time from oauth_token";
private static final String SQL_SELECT_BY_TOKEN = BASE_SELECT + " where user_id = ? and access_token = ?";
private static final String SQL_SELECT_BY_USER_ID = BASE_SELECT + " where user_id = ? order by create_time";
private static final String SQL_INSERT = "insert into oauth_token (" + UPDATE_FIELDS + ") values (?,?,?,?,?,?)";
private static final String SQL_UPDATE = "update oauth_token set " + UPDATE_FIELDS.replaceAll(", ", "=?, ") + "=? where token_id = ?";
private static final String SQL_UPDATE_PERMS = "update oauth_token set permissions = ? where user_id = ?";
private static final String SQL_UPDATE_ROLES = "update oauth_token set roles = ? where user_id = ?";
private static final String SQL_DELETE = "delete from oauth_token where user_id = ? and access_token = ?";
private static final String SQL_DELETE_BY_USER_ID = "delete from oauth_token where user_id = ?";
private static final String SQL_SELECT_KEY = "select token_key from oauth_token_key";
private static final String SQL_INSERT_KEY = "insert into oauth_token_key (token_key) values (?)";
public JdbcTokenStore(DataSource dataSource) {
Assert.notNull(dataSource, "DataSource required");
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
@Override
public String getTokenKey() {
String tokenKey = null;
try {
tokenKey = jdbcTemplate.queryForObject(SQL_SELECT_KEY, String.class);
} catch (EmptyResultDataAccessException e) {
}
if (tokenKey == null || tokenKey.trim().isEmpty()) {
tokenKey = TokenUtil.getHexKey();
jdbcTemplate.update(SQL_INSERT_KEY, tokenKey);
}
return tokenKey;
}
@Override
public Token createNewToken(String userId, String[] permissions, String[] roles) {
return createNewToken(userId, permissions, roles, TokenUtil.DEFAULT_EXPIRE);
}
@Override
public Token createNewToken(String userId, String[] permissions, String[] roles, long expire) {
String tokenKey = getTokenKey();
logger.debug("-------------------------------------------");
logger.debug("构建token使用tokenKey:" + tokenKey);
logger.debug("-------------------------------------------");
Token token = TokenUtil.buildToken(userId, expire, TokenUtil.parseHexKey(tokenKey));
token.setPermissions(permissions);
token.setRoles(roles);
if (storeToken(token) > 0) {
if (Config.getInstance().getMaxToken() != null && Config.getInstance().getMaxToken() != -1) {
List userTokens = findTokensByUserId(userId);
if (userTokens.size() > Config.getInstance().getMaxToken()) {
for (int i = 0; i < userTokens.size() - Config.getInstance().getMaxToken(); i++) {
removeToken(userId, userTokens.get(i).getAccessToken());
}
}
}
return token;
}
return null;
}
@Override
public int storeToken(Token token) {
List