cleanup and refactor per comments (#2601)

* import source BAEL-1084

* remove final keywords.  switch getAuthorities to use java8 style.

* cleanup

* cleanup
This commit is contained in:
chrisoberle 2017-09-14 01:03:50 -04:00 committed by Grzegorz Piwowarek
parent c0350ccc99
commit 8e7fc431a0
8 changed files with 53 additions and 52 deletions

View File

@ -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<? extends GrantedAuthority> getAuthorities(final Collection<Role> roles) {
final List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
private final Collection<? extends GrantedAuthority> getAuthorities(Collection<Role> roles) {
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
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;
}

View File

@ -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

View File

@ -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<Role> roles) {
public void setRoles(Collection<Role> 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;

View File

@ -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<User> users) {
public void setUsers(Collection<User> users) {
this.users = users;
}
@ -65,20 +65,20 @@ public class Role {
return privileges;
}
public void setPrivileges(final Collection<Privilege> privileges) {
public void setPrivileges(Collection<Privilege> 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();
}

View File

@ -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<Role> roles) {
public void setRoles(Collection<Role> 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();

View File

@ -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;

View File

@ -35,23 +35,23 @@ public class SetupDataLoader implements ApplicationListener<ContextRefreshedEven
@Override
@Transactional
public void onApplicationEvent(final ContextRefreshedEvent event) {
public void onApplicationEvent(ContextRefreshedEvent event) {
if (alreadySetup) {
return;
}
// == create initial privileges
final Privilege readPrivilege = createPrivilegeIfNotFound("READ_PRIVILEGE");
final Privilege writePrivilege = createPrivilegeIfNotFound("WRITE_PRIVILEGE");
Privilege readPrivilege = createPrivilegeIfNotFound("READ_PRIVILEGE");
Privilege writePrivilege = createPrivilegeIfNotFound("WRITE_PRIVILEGE");
// == create initial roles
final List<Privilege> adminPrivileges = Arrays.asList(readPrivilege, writePrivilege);
List<Privilege> adminPrivileges = Arrays.asList(readPrivilege, writePrivilege);
createRoleIfNotFound("ROLE_ADMIN", adminPrivileges);
List<Privilege> 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<ContextRefreshedEven
user.setEnabled(true);
userRepository.save(user);
final Role basicRole = roleRepository.findByName("ROLE_USER");
final User basicUser = new User();
Role basicRole = roleRepository.findByName("ROLE_USER");
User basicUser = new User();
basicUser.setFirstName("User");
basicUser.setLastName("User");
basicUser.setEmail("user@test.com");
@ -74,7 +74,7 @@ public class SetupDataLoader implements ApplicationListener<ContextRefreshedEven
}
@Transactional
private final Privilege createPrivilegeIfNotFound(final String name) {
private Privilege createPrivilegeIfNotFound(String name) {
Privilege privilege = privilegeRepository.findByName(name);
if (privilege == null) {
privilege = new Privilege(name);
@ -84,7 +84,7 @@ public class SetupDataLoader implements ApplicationListener<ContextRefreshedEven
}
@Transactional
private final Role createRoleIfNotFound(final String name, final Collection<Privilege> privileges) {
private Role createRoleIfNotFound(String name, Collection<Privilege> privileges) {
Role role = roleRepository.findByName(name);
if (role == null) {
role = new Role(name);

View File

@ -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);
}
}