From 8e7fc431a0426af1f6d4430b475b2a505f265fef Mon Sep 17 00:00:00 2001 From: chrisoberle Date: Thu, 14 Sep 2017 01:03:50 -0400 Subject: [PATCH] cleanup and refactor per comments (#2601) * import source BAEL-1084 * remove final keywords. switch getAuthorities to use java8 style. * cleanup * cleanup --- .../MyUserDetailsService.java | 17 +++++++------- .../config/SecurityConfig.java | 6 ++--- .../rolesauthorities/model/Privilege.java | 10 ++++----- .../baeldung/rolesauthorities/model/Role.java | 18 +++++++-------- .../baeldung/rolesauthorities/model/User.java | 22 +++++++++---------- .../persistence/PersistenceJPAConfig.java | 10 ++++----- .../persistence/SetupDataLoader.java | 20 ++++++++--------- .../persistence/UserService.java | 2 +- 8 files changed, 53 insertions(+), 52 deletions(-) diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/MyUserDetailsService.java b/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/MyUserDetailsService.java index 5749c6194c..f38b867a75 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/MyUserDetailsService.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/MyUserDetailsService.java @@ -3,8 +3,8 @@ package org.baeldung.rolesauthorities; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.stream.Collectors; -import org.baeldung.rolesauthorities.model.Privilege; import org.baeldung.rolesauthorities.model.Role; import org.baeldung.rolesauthorities.model.User; import org.baeldung.rolesauthorities.persistence.UserRepository; @@ -31,10 +31,10 @@ public class MyUserDetailsService implements UserDetailsService { // API @Override - public UserDetails loadUserByUsername(final String email) throws UsernameNotFoundException { + public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException { try { - final User user = userRepository.findByEmail(email); + User user = userRepository.findByEmail(email); if (user == null) { throw new UsernameNotFoundException("No user found with username: " + email); } @@ -47,13 +47,14 @@ public class MyUserDetailsService implements UserDetailsService { // UTIL - private final Collection getAuthorities(final Collection roles) { - final List authorities = new ArrayList(); + private final Collection getAuthorities(Collection roles) { + List authorities = new ArrayList(); for (Role role: roles) { authorities.add(new SimpleGrantedAuthority(role.getName())); - for (Privilege privilege: role.getPrivileges()) { - authorities.add(new SimpleGrantedAuthority(privilege.getName())); - } + authorities.addAll(role.getPrivileges() + .stream() + .map(p -> new SimpleGrantedAuthority(p.getName())) + .collect(Collectors.toList())); } return authorities; } diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/config/SecurityConfig.java b/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/config/SecurityConfig.java index cadd477e5e..7624dd7d39 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/config/SecurityConfig.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/config/SecurityConfig.java @@ -36,19 +36,19 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { } @Override - protected void configure(final AuthenticationManagerBuilder auth) throws Exception { + protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.authenticationProvider(authProvider()); } @Override - public void configure(final WebSecurity web) throws Exception { + public void configure(WebSecurity web) throws Exception { web.ignoring() .antMatchers("/resources/**"); } @Override - protected void configure(final HttpSecurity http) throws Exception { + protected void configure(HttpSecurity http) throws Exception { // @formatter:off http diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/model/Privilege.java b/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/model/Privilege.java index 5165429b5a..ab2cd08610 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/model/Privilege.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/model/Privilege.java @@ -24,7 +24,7 @@ public class Privilege { super(); } - public Privilege(final String name) { + public Privilege(String name) { super(); this.name = name; } @@ -35,7 +35,7 @@ public class Privilege { return id; } - public void setId(final Long id) { + public void setId(Long id) { this.id = id; } @@ -43,7 +43,7 @@ public class Privilege { return name; } - public void setName(final String name) { + public void setName(String name) { this.name = name; } @@ -51,13 +51,13 @@ public class Privilege { return roles; } - public void setRoles(final Collection roles) { + public void setRoles(Collection roles) { this.roles = roles; } @Override public int hashCode() { - final int prime = 31; + int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/model/Role.java b/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/model/Role.java index 9216abe470..ac33e32fcf 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/model/Role.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/model/Role.java @@ -30,7 +30,7 @@ public class Role { super(); } - public Role(final String name) { + public Role(String name) { super(); this.name = name; } @@ -41,7 +41,7 @@ public class Role { return id; } - public void setId(final Long id) { + public void setId(Long id) { this.id = id; } @@ -49,7 +49,7 @@ public class Role { return name; } - public void setName(final String name) { + public void setName(String name) { this.name = name; } @@ -57,7 +57,7 @@ public class Role { return users; } - public void setUsers(final Collection users) { + public void setUsers(Collection users) { this.users = users; } @@ -65,20 +65,20 @@ public class Role { return privileges; } - public void setPrivileges(final Collection privileges) { + public void setPrivileges(Collection privileges) { this.privileges = privileges; } @Override public int hashCode() { - final int prime = 31; + int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override - public boolean equals(final Object obj) { + public boolean equals(Object obj) { if (this == obj) { return true; } @@ -88,7 +88,7 @@ public class Role { if (getClass() != obj.getClass()) { return false; } - final Role role = (Role) obj; + Role role = (Role) obj; if (!role.equals(role.name)) { return false; } @@ -97,7 +97,7 @@ public class Role { @Override public String toString() { - final StringBuilder builder = new StringBuilder(); + StringBuilder builder = new StringBuilder(); builder.append("Role [name=").append(name).append("]").append("[id=").append(id).append("]"); return builder.toString(); } diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/model/User.java b/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/model/User.java index dd5b90e3b2..dc1096541d 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/model/User.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/model/User.java @@ -50,7 +50,7 @@ public class User { return id; } - public void setId(final Long id) { + public void setId(Long id) { this.id = id; } @@ -58,7 +58,7 @@ public class User { return firstName; } - public void setFirstName(final String firstName) { + public void setFirstName(String firstName) { this.firstName = firstName; } @@ -66,7 +66,7 @@ public class User { return lastName; } - public void setLastName(final String lastName) { + public void setLastName(String lastName) { this.lastName = lastName; } @@ -74,7 +74,7 @@ public class User { return email; } - public void setEmail(final String username) { + public void setEmail(String username) { this.email = username; } @@ -82,7 +82,7 @@ public class User { return password; } - public void setPassword(final String password) { + public void setPassword(String password) { this.password = password; } @@ -90,7 +90,7 @@ public class User { return roles; } - public void setRoles(final Collection roles) { + public void setRoles(Collection roles) { this.roles = roles; } @@ -98,7 +98,7 @@ public class User { return enabled; } - public void setEnabled(final boolean enabled) { + public void setEnabled(boolean enabled) { this.enabled = enabled; } @@ -112,14 +112,14 @@ public class User { @Override public int hashCode() { - final int prime = 31; + int prime = 31; int result = 1; result = (prime * result) + ((email == null) ? 0 : email.hashCode()); return result; } @Override - public boolean equals(final Object obj) { + public boolean equals(Object obj) { if (this == obj) { return true; } @@ -129,7 +129,7 @@ public class User { if (getClass() != obj.getClass()) { return false; } - final User user = (User) obj; + User user = (User) obj; if (!email.equals(user.email)) { return false; } @@ -138,7 +138,7 @@ public class User { @Override public String toString() { - final StringBuilder builder = new StringBuilder(); + StringBuilder builder = new StringBuilder(); builder.append("User [id=").append(id).append(", firstName=") .append(firstName).append(", lastName=").append(lastName).append(", email=").append(email).append(", password=").append(password).append(", enabled=").append(enabled).append(", roles=").append(roles).append("]"); return builder.toString(); diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/persistence/PersistenceJPAConfig.java b/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/persistence/PersistenceJPAConfig.java index 45e47ecb4a..a993e41a83 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/persistence/PersistenceJPAConfig.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/persistence/PersistenceJPAConfig.java @@ -36,10 +36,10 @@ public class PersistenceJPAConfig { @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory() { - final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); + LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(dataSource()); em.setPackagesToScan(new String[] { "org.baeldung.rolesauthorities" }); - final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); + HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); em.setJpaVendorAdapter(vendorAdapter); em.setJpaProperties(additionalProperties()); return em; @@ -47,7 +47,7 @@ public class PersistenceJPAConfig { @Bean public DataSource dataSource() { - final DriverManagerDataSource dataSource = new DriverManagerDataSource(); + DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName")); dataSource.setUrl(env.getProperty("jdbc.url")); dataSource.setUsername(env.getProperty("jdbc.user")); @@ -57,7 +57,7 @@ public class PersistenceJPAConfig { @Bean public JpaTransactionManager transactionManager() { - final JpaTransactionManager transactionManager = new JpaTransactionManager(); + JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory(entityManagerFactory().getObject()); return transactionManager; } @@ -68,7 +68,7 @@ public class PersistenceJPAConfig { } protected Properties additionalProperties() { - final Properties hibernateProperties = new Properties(); + Properties hibernateProperties = new Properties(); hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto")); hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect")); return hibernateProperties; diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/persistence/SetupDataLoader.java b/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/persistence/SetupDataLoader.java index 6c1c7d87ef..46dad4f06d 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/persistence/SetupDataLoader.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/persistence/SetupDataLoader.java @@ -35,23 +35,23 @@ public class SetupDataLoader implements ApplicationListener adminPrivileges = Arrays.asList(readPrivilege, writePrivilege); + List adminPrivileges = Arrays.asList(readPrivilege, writePrivilege); createRoleIfNotFound("ROLE_ADMIN", adminPrivileges); List rolePrivileges = new ArrayList<>(); createRoleIfNotFound("ROLE_USER", rolePrivileges); - final Role adminRole = roleRepository.findByName("ROLE_ADMIN"); - final User user = new User(); + Role adminRole = roleRepository.findByName("ROLE_ADMIN"); + User user = new User(); user.setFirstName("Admin"); user.setLastName("Admin"); user.setEmail("admin@test.com"); @@ -60,8 +60,8 @@ public class SetupDataLoader implements ApplicationListener privileges) { + private Role createRoleIfNotFound(String name, Collection privileges) { Role role = roleRepository.findByName(name); if (role == null) { role = new Role(name); diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/persistence/UserService.java b/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/persistence/UserService.java index bdea674057..3b16c78898 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/persistence/UserService.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/persistence/UserService.java @@ -13,7 +13,7 @@ public class UserService implements IUserService { @Autowired private UserRepository repository; - public User findUserByEmail(final String email) { + public User findUserByEmail(String email) { return repository.findByEmail(email); } } \ No newline at end of file