Refactor Batch (#2439)

This commit is contained in:
Grzegorz Piwowarek 2017-08-14 21:46:04 +02:00 committed by GitHub
parent dc9ecc143d
commit bcc122b724
9 changed files with 174 additions and 177 deletions

View File

@ -8,10 +8,10 @@ import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends LdapRepository<User> {
public User findByUsername(String username);
User findByUsername(String username);
public User findByUsernameAndPassword(String username, String password);
User findByUsernameAndPassword(String username, String password);
public List<User> findByUsernameLikeIgnoreCase(String username);
List<User> findByUsernameLikeIgnoreCase(String username);
}

View File

@ -2,7 +2,11 @@ package com.baeldung.ldap.data.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.ldap.core.*;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.DirContextOperations;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.support.LdapNameBuilder;
import javax.naming.Name;
@ -43,7 +47,7 @@ public class LdapClient {
.build();
DirContextAdapter context = new DirContextAdapter(dn);
context.setAttributeValues("objectclass", new String[] { "top", "person", "organizationalPerson", "inetOrgPerson" });
context.setAttributeValues("objectclass", new String[]{"top", "person", "organizationalPerson", "inetOrgPerson"});
context.setAttributeValue("cn", username);
context.setAttributeValue("sn", username);
context.setAttributeValue("userPassword", digestSHA(password));
@ -59,7 +63,7 @@ public class LdapClient {
.build();
DirContextOperations context = ldapTemplate.lookupContext(dn);
context.setAttributeValues("objectclass", new String[] { "top", "person", "organizationalPerson", "inetOrgPerson" });
context.setAttributeValues("objectclass", new String[]{"top", "person", "organizationalPerson", "inetOrgPerson"});
context.setAttributeValue("cn", username);
context.setAttributeValue("sn", username);
context.setAttributeValue("userPassword", digestSHA(password));

View File

@ -1,17 +1,17 @@
package com.baeldung.ldap.data.service;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import com.baeldung.ldap.data.repository.User;
import com.baeldung.ldap.data.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.support.LdapUtils;
import org.springframework.stereotype.Service;
import com.baeldung.ldap.data.repository.User;
import com.baeldung.ldap.data.repository.UserRepository;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class UserService {
@ -21,20 +21,18 @@ public class UserService {
public Boolean authenticate(final String username, final String password) {
User user = userRepository.findByUsernameAndPassword(username, password);
return user != null ? true : false;
return user != null;
}
public List<String> search(final String username) {
List<User> userList = userRepository.findByUsernameLikeIgnoreCase(username);
List<String> users = null;
if (null != userList) {
users = new ArrayList<String>();
for (User user : userList) {
users.add(user.getUsername());
if (userList == null) {
return Collections.emptyList();
}
}
return users;
return userList.stream()
.map(User::getUsername)
.collect(Collectors.toList());
}
public void create(final String username, final String password) {
@ -48,7 +46,6 @@ public class UserService {
User user = userRepository.findByUsername(username);
user.setPassword(password);
userRepository.save(user);
}
private String digestSHA(final String password) {

View File

@ -1,5 +1,6 @@
package com.baeldung.ldap.javaconfig;
import com.baeldung.ldap.client.LdapClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
@ -11,13 +12,11 @@ import org.springframework.data.ldap.repository.config.EnableLdapRepositories;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.LdapContextSource;
import com.baeldung.ldap.client.LdapClient;
@Configuration
@PropertySource("classpath:application.properties")
@ComponentScan(basePackages = { "com.baeldung.ldap.*" })
@ComponentScan(basePackages = {"com.baeldung.ldap.*"})
@Profile("default")
@EnableLdapRepositories(basePackages="com.baeldung.ldap.**")
@EnableLdapRepositories(basePackages = "com.baeldung.ldap.**")
public class AppConfig {
@Autowired

View File

@ -7,13 +7,13 @@
</encoder>
</appender>
<logger name="org.springframework" level="WARN" />
<logger name="org.springframework.transaction" level="WARN" />
<logger name="org.springframework" level="WARN"/>
<logger name="org.springframework.transaction" level="WARN"/>
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
<logger name="org.springframework.web.servlet.mvc" level="WARN"/>
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="STDOUT"/>
</root>
</configuration>

View File

@ -1,7 +1,6 @@
package com.baeldung.ldap.client;
import java.util.List;
import com.baeldung.ldap.javaconfig.TestConfig;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;
@ -13,11 +12,11 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.baeldung.ldap.javaconfig.TestConfig;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("testlive")
@ContextConfiguration(classes = { TestConfig.class }, loader = AnnotationConfigContextLoader.class)
@ContextConfiguration(classes = {TestConfig.class}, loader = AnnotationConfigContextLoader.class)
public class LdapClientLiveTest {
private static final String USER2 = "TEST02";

View File

@ -1,10 +1,7 @@
package com.baeldung.ldap.client;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import java.util.List;
import com.baeldung.ldap.data.service.UserService;
import com.baeldung.ldap.javaconfig.TestConfig;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -14,12 +11,14 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import com.baeldung.ldap.data.service.UserService;
import com.baeldung.ldap.javaconfig.TestConfig;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("testlive")
@ContextConfiguration(classes = { TestConfig.class }, loader = AnnotationConfigContextLoader.class)
@ContextConfiguration(classes = {TestConfig.class}, loader = AnnotationConfigContextLoader.class)
public class LdapDataRepositoryTest {
private static final String USER2 = "TEST02";

View File

@ -1,5 +1,6 @@
package com.baeldung.ldap.javaconfig;
import com.baeldung.ldap.client.LdapClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
@ -13,12 +14,10 @@ import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.LdapContextSource;
import org.springframework.ldap.test.TestContextSourceFactoryBean;
import com.baeldung.ldap.client.LdapClient;
@Configuration
@PropertySource("classpath:test_application.properties")
@ComponentScan(basePackages = { "com.baeldung.ldap.*" })
@EnableLdapRepositories(basePackages="com.baeldung.ldap.**")
@ComponentScan(basePackages = {"com.baeldung.ldap.*"})
@EnableLdapRepositories(basePackages = "com.baeldung.ldap.**")
@Profile("testlive")
public class TestConfig {
@Autowired