Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
88 lines (77 sloc) 3.14 KB
package ca.ljz.winter.security.realms;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.function.Function;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class JdbcRealm extends RealmAdapter {
private final static Logger LOGGER = Logger.getLogger(JdbcRealm.class.getName());
protected static final String PASSWORD_QUERY = "select password from user where username = ?";
protected static final String ROLE_QUERY = "select role from user_role where username = ?";
protected static final String PERMISSION_QUERY = "select permission from user_role ur, role_permission rp where ur.role = pr.role and ur.username = ?";
@Override
public String getPassword(String username) {
LOGGER.info("Enter ca.ljz.winter.security.realms.JdbcRealm.getPassword(String)");
try {
String[] password = connect(
conn -> executeQuery(conn, properties.get("Query.Password", PASSWORD_QUERY), username));
if (password.length < 1)
return null;
return password[0];
} finally {
LOGGER.info("Exit ca.ljz.winter.security.realms.JdbcRealm.getPassword(String)");
}
}
@Override
public String[] getRoles(String username) {
LOGGER.info("Enter ca.ljz.winter.security.realms.JdbcRealm.getRoles(String)");
try {
return connect(conn -> executeQuery(conn, properties.get("Query.Role", ROLE_QUERY), username));
} finally {
LOGGER.info("Exit ca.ljz.winter.security.realms.JdbcRealm.getRoles(String)");
}
}
@Override
public String[] getPermissions(String username) {
LOGGER.info("Enter ca.ljz.winter.security.realms.JdbcRealm.getPermissions(String)");
try {
return connect(conn -> executeQuery(conn, properties.get("Query.Permission", PERMISSION_QUERY), username));
} finally {
LOGGER.info("Exit ca.ljz.winter.security.realms.JdbcRealm.getPermissions(String)");
}
}
private String[] connect(Function<Connection, String[]> function) {
LOGGER.info("Enter ca.ljz.winter.security.realms.JdbcRealm.connect(Function<Connection, String[]>)");
try (Connection conn = ((DataSource) new InitialContext().lookup(properties.get("DataSource")))
.getConnection()) {
return function.apply(conn);
} catch (NamingException | SQLException e) {
throw new RuntimeException(e);
} finally {
LOGGER.info("Exit ca.ljz.winter.security.realms.JdbcRealm.connect(Function<Connection, String[]>)");
}
}
private String[] executeQuery(Connection conn, String query, String username) {
LOGGER.info("Enter ca.ljz.winter.security.realms.JdbcRealm.executeQuery(Connection, String, String)");
try (PreparedStatement ps = conn.prepareStatement(query)) {
ps.setString(1, username);
try (ResultSet rs = ps.executeQuery()) {
rs.last();
String[] result = new String[rs.getRow()];
rs.beforeFirst();
for (int i = 0; rs.next(); i++) {
result[i] = rs.getString(1);
}
return result;
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
LOGGER.info("Exit ca.ljz.winter.security.realms.JdbcRealm.executeQuery(Connection, String, String)");
}
}
}