Polish gh-4048

This commit is contained in:
Rob Winch 2016-08-30 09:42:28 -05:00
parent 422bc37115
commit 53352e336d
2 changed files with 45 additions and 5 deletions

View File

@ -110,7 +110,8 @@ import org.springframework.util.Assert;
* @author colin sampaleanu
* @author Luke Taylor
*/
public class JdbcDaoImpl extends JdbcDaoSupport implements UserDetailsService, MessageSourceAware {
public class JdbcDaoImpl extends JdbcDaoSupport
implements UserDetailsService, MessageSourceAware {
// ~ Static fields/initializers
// =====================================================================================
@ -126,8 +127,7 @@ public class JdbcDaoImpl extends JdbcDaoSupport implements UserDetailsService, M
// ~ Instance fields
// ================================================================================================
protected final MessageSourceAccessor messages = SpringSecurityMessageSource
.getAccessor();
protected MessageSourceAccessor messages = SpringSecurityMessageSource.getAccessor();
private String authoritiesByUsernameQuery;
private String groupAuthoritiesByUsernameQuery;
@ -149,6 +149,13 @@ public class JdbcDaoImpl extends JdbcDaoSupport implements UserDetailsService, M
// ~ Methods
// ========================================================================================================
/**
* @return the messages
*/
protected MessageSourceAccessor getMessages() {
return this.messages;
}
/**
* Allows subclasses to add their own granted authorities to the list to be returned
* in the <tt>UserDetails</tt>.
@ -171,6 +178,7 @@ public class JdbcDaoImpl extends JdbcDaoSupport implements UserDetailsService, M
"Use of either authorities or groups must be enabled");
}
@Override
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException {
List<UserDetails> users = loadUsersByUsername(username);
@ -218,6 +226,7 @@ public class JdbcDaoImpl extends JdbcDaoSupport implements UserDetailsService, M
protected List<UserDetails> loadUsersByUsername(String username) {
return getJdbcTemplate().query(this.usersByUsernameQuery,
new String[] { username }, new RowMapper<UserDetails>() {
@Override
public UserDetails mapRow(ResultSet rs, int rowNum)
throws SQLException {
String username = rs.getString(1);
@ -238,6 +247,7 @@ public class JdbcDaoImpl extends JdbcDaoSupport implements UserDetailsService, M
protected List<GrantedAuthority> loadUserAuthorities(String username) {
return getJdbcTemplate().query(this.authoritiesByUsernameQuery,
new String[] { username }, new RowMapper<GrantedAuthority>() {
@Override
public GrantedAuthority mapRow(ResultSet rs, int rowNum)
throws SQLException {
String roleName = JdbcDaoImpl.this.rolePrefix + rs.getString(2);
@ -256,6 +266,7 @@ public class JdbcDaoImpl extends JdbcDaoSupport implements UserDetailsService, M
protected List<GrantedAuthority> loadGroupAuthorities(String username) {
return getJdbcTemplate().query(this.groupAuthoritiesByUsernameQuery,
new String[] { username }, new RowMapper<GrantedAuthority>() {
@Override
public GrantedAuthority mapRow(ResultSet rs, int rowNum)
throws SQLException {
String roleName = getRolePrefix() + rs.getString(3);
@ -395,8 +406,10 @@ public class JdbcDaoImpl extends JdbcDaoSupport implements UserDetailsService, M
public void setEnableGroups(boolean enableGroups) {
this.enableGroups = enableGroups;
}
@Override
public void setMessageSource(MessageSource messageSource) {
Assert.notNull(messageSource, "messageSource cannot be null");
this.messages = new MessageSourceAccessor(messageSource);
}
}

View File

@ -16,8 +16,11 @@
package org.springframework.security.core.userdetails.jdbc;
import java.util.Locale;
import org.junit.Test;
import org.springframework.context.MessageSource;
import org.springframework.security.PopulatedDatabase;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
@ -25,6 +28,10 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* Tests {@link JdbcDaoImpl}.
@ -69,7 +76,8 @@ public class JdbcDaoImplTests {
}
@Test
public void testCheckDaoOnlyReturnsGrantedAuthoritiesGrantedToUser() throws Exception {
public void testCheckDaoOnlyReturnsGrantedAuthoritiesGrantedToUser()
throws Exception {
JdbcDaoImpl dao = makePopulatedJdbcDao();
UserDetails user = dao.loadUserByUsername("scott");
assertThat(user.getAuthorities()).hasSize(1);
@ -187,4 +195,23 @@ public class JdbcDaoImplTests {
}
}
@Test(expected = IllegalArgumentException.class)
public void setMessageSourceWhenNullThenThrowsException() throws Exception {
JdbcDaoImpl dao = new JdbcDaoImpl();
dao.setMessageSource(null);
}
@Test
public void setMessageSourceWhenNotNullThenCanGet() throws Exception {
MessageSource source = mock(MessageSource.class);
JdbcDaoImpl dao = new JdbcDaoImpl();
dao.setMessageSource(source);
String code = "code";
dao.getMessages().getMessage(code);
verify(source).getMessage(eq(code), any(Object[].class), any(Locale.class));
}
}