SEC-1964: Handle missing series in JdbcTokenRepositoryImpl

Previously JdbcTokenRepositoryImpl would log an error with a misleading
message when the token series was missing.

Now JdbcTokenRepositoryImpl logs missing token series at info level with
a more informative message.
This commit is contained in:
Rob Winch
2012-07-18 16:17:26 -05:00
parent 7f9938c8e2
commit 340534dadb
3 changed files with 66 additions and 1 deletions

View File

@@ -1,25 +1,55 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.security.web.authentication.rememberme;
import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
import org.springframework.test.util.ReflectionTestUtils;
/**
* @author Luke Taylor
*/
@SuppressWarnings("unchecked")
@RunWith(MockitoJUnitRunner.class)
public class JdbcTokenRepositoryImplTests {
@Mock
private Log logger;
private static SingleConnectionDataSource dataSource;
private JdbcTokenRepositoryImpl repo;
private JdbcTemplate template;
@@ -39,6 +69,7 @@ public class JdbcTokenRepositoryImplTests {
@Before
public void populateDatabase() {
repo = new JdbcTokenRepositoryImpl();
ReflectionTestUtils.setField(repo, "logger", logger);
repo.setDataSource(dataSource);
repo.initDao();
template = repo.getJdbcTemplate();
@@ -90,6 +121,19 @@ public class JdbcTokenRepositoryImplTests {
assertNull(repo.getTokenForSeries("joesseries"));
}
// SEC-1964
@Test
public void retrievingTokenWithNoSeriesReturnsNull() {
when(logger.isInfoEnabled()).thenReturn(true);
assertNull(repo.getTokenForSeries("missingSeries"));
verify(logger).isInfoEnabled();
verify(logger).info(eq("Querying token for series 'missingSeries' returned no results."),
any(EmptyResultDataAccessException.class));
verifyNoMoreInteractions(logger);
}
@Test
public void removingUserTokensDeletesData() {
template.execute("insert into persistent_logins (series, username, token, last_used) values " +