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:
parent
c0350ccc99
commit
8e7fc431a0
|
@ -3,8 +3,8 @@ package org.baeldung.rolesauthorities;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
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.Role;
|
||||||
import org.baeldung.rolesauthorities.model.User;
|
import org.baeldung.rolesauthorities.model.User;
|
||||||
import org.baeldung.rolesauthorities.persistence.UserRepository;
|
import org.baeldung.rolesauthorities.persistence.UserRepository;
|
||||||
|
@ -31,10 +31,10 @@ public class MyUserDetailsService implements UserDetailsService {
|
||||||
// API
|
// API
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UserDetails loadUserByUsername(final String email) throws UsernameNotFoundException {
|
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final User user = userRepository.findByEmail(email);
|
User user = userRepository.findByEmail(email);
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
throw new UsernameNotFoundException("No user found with username: " + email);
|
throw new UsernameNotFoundException("No user found with username: " + email);
|
||||||
}
|
}
|
||||||
|
@ -47,13 +47,14 @@ public class MyUserDetailsService implements UserDetailsService {
|
||||||
|
|
||||||
// UTIL
|
// UTIL
|
||||||
|
|
||||||
private final Collection<? extends GrantedAuthority> getAuthorities(final Collection<Role> roles) {
|
private final Collection<? extends GrantedAuthority> getAuthorities(Collection<Role> roles) {
|
||||||
final List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
|
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
|
||||||
for (Role role: roles) {
|
for (Role role: roles) {
|
||||||
authorities.add(new SimpleGrantedAuthority(role.getName()));
|
authorities.add(new SimpleGrantedAuthority(role.getName()));
|
||||||
for (Privilege privilege: role.getPrivileges()) {
|
authorities.addAll(role.getPrivileges()
|
||||||
authorities.add(new SimpleGrantedAuthority(privilege.getName()));
|
.stream()
|
||||||
}
|
.map(p -> new SimpleGrantedAuthority(p.getName()))
|
||||||
|
.collect(Collectors.toList()));
|
||||||
}
|
}
|
||||||
return authorities;
|
return authorities;
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,19 +36,19 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
|
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
||||||
auth.authenticationProvider(authProvider());
|
auth.authenticationProvider(authProvider());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void configure(final WebSecurity web) throws Exception {
|
public void configure(WebSecurity web) throws Exception {
|
||||||
web.ignoring()
|
web.ignoring()
|
||||||
.antMatchers("/resources/**");
|
.antMatchers("/resources/**");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure(final HttpSecurity http) throws Exception {
|
protected void configure(HttpSecurity http) throws Exception {
|
||||||
|
|
||||||
// @formatter:off
|
// @formatter:off
|
||||||
http
|
http
|
||||||
|
|
|
@ -24,7 +24,7 @@ public class Privilege {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Privilege(final String name) {
|
public Privilege(String name) {
|
||||||
super();
|
super();
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ public class Privilege {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setId(final Long id) {
|
public void setId(Long id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@ public class Privilege {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setName(final String name) {
|
public void setName(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,13 +51,13 @@ public class Privilege {
|
||||||
return roles;
|
return roles;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRoles(final Collection<Role> roles) {
|
public void setRoles(Collection<Role> roles) {
|
||||||
this.roles = roles;
|
this.roles = roles;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
final int prime = 31;
|
int prime = 31;
|
||||||
int result = 1;
|
int result = 1;
|
||||||
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||||
return result;
|
return result;
|
||||||
|
|
|
@ -30,7 +30,7 @@ public class Role {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Role(final String name) {
|
public Role(String name) {
|
||||||
super();
|
super();
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
@ -41,7 +41,7 @@ public class Role {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setId(final Long id) {
|
public void setId(Long id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,7 +49,7 @@ public class Role {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setName(final String name) {
|
public void setName(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,7 +57,7 @@ public class Role {
|
||||||
return users;
|
return users;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUsers(final Collection<User> users) {
|
public void setUsers(Collection<User> users) {
|
||||||
this.users = users;
|
this.users = users;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,20 +65,20 @@ public class Role {
|
||||||
return privileges;
|
return privileges;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPrivileges(final Collection<Privilege> privileges) {
|
public void setPrivileges(Collection<Privilege> privileges) {
|
||||||
this.privileges = privileges;
|
this.privileges = privileges;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
final int prime = 31;
|
int prime = 31;
|
||||||
int result = 1;
|
int result = 1;
|
||||||
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(final Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (this == obj) {
|
if (this == obj) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -88,7 +88,7 @@ public class Role {
|
||||||
if (getClass() != obj.getClass()) {
|
if (getClass() != obj.getClass()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
final Role role = (Role) obj;
|
Role role = (Role) obj;
|
||||||
if (!role.equals(role.name)) {
|
if (!role.equals(role.name)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -97,7 +97,7 @@ public class Role {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
final StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
builder.append("Role [name=").append(name).append("]").append("[id=").append(id).append("]");
|
builder.append("Role [name=").append(name).append("]").append("[id=").append(id).append("]");
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,7 +50,7 @@ public class User {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setId(final Long id) {
|
public void setId(Long id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,7 +58,7 @@ public class User {
|
||||||
return firstName;
|
return firstName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFirstName(final String firstName) {
|
public void setFirstName(String firstName) {
|
||||||
this.firstName = firstName;
|
this.firstName = firstName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,7 +66,7 @@ public class User {
|
||||||
return lastName;
|
return lastName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLastName(final String lastName) {
|
public void setLastName(String lastName) {
|
||||||
this.lastName = lastName;
|
this.lastName = lastName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,7 +74,7 @@ public class User {
|
||||||
return email;
|
return email;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setEmail(final String username) {
|
public void setEmail(String username) {
|
||||||
this.email = username;
|
this.email = username;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,7 +82,7 @@ public class User {
|
||||||
return password;
|
return password;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPassword(final String password) {
|
public void setPassword(String password) {
|
||||||
this.password = password;
|
this.password = password;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,7 +90,7 @@ public class User {
|
||||||
return roles;
|
return roles;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRoles(final Collection<Role> roles) {
|
public void setRoles(Collection<Role> roles) {
|
||||||
this.roles = roles;
|
this.roles = roles;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,7 +98,7 @@ public class User {
|
||||||
return enabled;
|
return enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setEnabled(final boolean enabled) {
|
public void setEnabled(boolean enabled) {
|
||||||
this.enabled = enabled;
|
this.enabled = enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,14 +112,14 @@ public class User {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
final int prime = 31;
|
int prime = 31;
|
||||||
int result = 1;
|
int result = 1;
|
||||||
result = (prime * result) + ((email == null) ? 0 : email.hashCode());
|
result = (prime * result) + ((email == null) ? 0 : email.hashCode());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(final Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (this == obj) {
|
if (this == obj) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -129,7 +129,7 @@ public class User {
|
||||||
if (getClass() != obj.getClass()) {
|
if (getClass() != obj.getClass()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
final User user = (User) obj;
|
User user = (User) obj;
|
||||||
if (!email.equals(user.email)) {
|
if (!email.equals(user.email)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -138,7 +138,7 @@ public class User {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
final StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
builder.append("User [id=").append(id).append(", firstName=")
|
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("]");
|
.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();
|
return builder.toString();
|
||||||
|
|
|
@ -36,10 +36,10 @@ public class PersistenceJPAConfig {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
||||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||||
em.setDataSource(dataSource());
|
em.setDataSource(dataSource());
|
||||||
em.setPackagesToScan(new String[] { "org.baeldung.rolesauthorities" });
|
em.setPackagesToScan(new String[] { "org.baeldung.rolesauthorities" });
|
||||||
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||||
em.setJpaVendorAdapter(vendorAdapter);
|
em.setJpaVendorAdapter(vendorAdapter);
|
||||||
em.setJpaProperties(additionalProperties());
|
em.setJpaProperties(additionalProperties());
|
||||||
return em;
|
return em;
|
||||||
|
@ -47,7 +47,7 @@ public class PersistenceJPAConfig {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public DataSource dataSource() {
|
public DataSource dataSource() {
|
||||||
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
||||||
dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
|
dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
|
||||||
dataSource.setUrl(env.getProperty("jdbc.url"));
|
dataSource.setUrl(env.getProperty("jdbc.url"));
|
||||||
dataSource.setUsername(env.getProperty("jdbc.user"));
|
dataSource.setUsername(env.getProperty("jdbc.user"));
|
||||||
|
@ -57,7 +57,7 @@ public class PersistenceJPAConfig {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public JpaTransactionManager transactionManager() {
|
public JpaTransactionManager transactionManager() {
|
||||||
final JpaTransactionManager transactionManager = new JpaTransactionManager();
|
JpaTransactionManager transactionManager = new JpaTransactionManager();
|
||||||
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
|
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
|
||||||
return transactionManager;
|
return transactionManager;
|
||||||
}
|
}
|
||||||
|
@ -68,7 +68,7 @@ public class PersistenceJPAConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Properties additionalProperties() {
|
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.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
||||||
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
|
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
|
||||||
return hibernateProperties;
|
return hibernateProperties;
|
||||||
|
|
|
@ -35,23 +35,23 @@ public class SetupDataLoader implements ApplicationListener<ContextRefreshedEven
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
public void onApplicationEvent(final ContextRefreshedEvent event) {
|
public void onApplicationEvent(ContextRefreshedEvent event) {
|
||||||
if (alreadySetup) {
|
if (alreadySetup) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// == create initial privileges
|
// == create initial privileges
|
||||||
final Privilege readPrivilege = createPrivilegeIfNotFound("READ_PRIVILEGE");
|
Privilege readPrivilege = createPrivilegeIfNotFound("READ_PRIVILEGE");
|
||||||
final Privilege writePrivilege = createPrivilegeIfNotFound("WRITE_PRIVILEGE");
|
Privilege writePrivilege = createPrivilegeIfNotFound("WRITE_PRIVILEGE");
|
||||||
|
|
||||||
// == create initial roles
|
// == create initial roles
|
||||||
final List<Privilege> adminPrivileges = Arrays.asList(readPrivilege, writePrivilege);
|
List<Privilege> adminPrivileges = Arrays.asList(readPrivilege, writePrivilege);
|
||||||
createRoleIfNotFound("ROLE_ADMIN", adminPrivileges);
|
createRoleIfNotFound("ROLE_ADMIN", adminPrivileges);
|
||||||
List<Privilege> rolePrivileges = new ArrayList<>();
|
List<Privilege> rolePrivileges = new ArrayList<>();
|
||||||
createRoleIfNotFound("ROLE_USER", rolePrivileges);
|
createRoleIfNotFound("ROLE_USER", rolePrivileges);
|
||||||
|
|
||||||
final Role adminRole = roleRepository.findByName("ROLE_ADMIN");
|
Role adminRole = roleRepository.findByName("ROLE_ADMIN");
|
||||||
final User user = new User();
|
User user = new User();
|
||||||
user.setFirstName("Admin");
|
user.setFirstName("Admin");
|
||||||
user.setLastName("Admin");
|
user.setLastName("Admin");
|
||||||
user.setEmail("admin@test.com");
|
user.setEmail("admin@test.com");
|
||||||
|
@ -60,8 +60,8 @@ public class SetupDataLoader implements ApplicationListener<ContextRefreshedEven
|
||||||
user.setEnabled(true);
|
user.setEnabled(true);
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
|
|
||||||
final Role basicRole = roleRepository.findByName("ROLE_USER");
|
Role basicRole = roleRepository.findByName("ROLE_USER");
|
||||||
final User basicUser = new User();
|
User basicUser = new User();
|
||||||
basicUser.setFirstName("User");
|
basicUser.setFirstName("User");
|
||||||
basicUser.setLastName("User");
|
basicUser.setLastName("User");
|
||||||
basicUser.setEmail("user@test.com");
|
basicUser.setEmail("user@test.com");
|
||||||
|
@ -74,7 +74,7 @@ public class SetupDataLoader implements ApplicationListener<ContextRefreshedEven
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
private final Privilege createPrivilegeIfNotFound(final String name) {
|
private Privilege createPrivilegeIfNotFound(String name) {
|
||||||
Privilege privilege = privilegeRepository.findByName(name);
|
Privilege privilege = privilegeRepository.findByName(name);
|
||||||
if (privilege == null) {
|
if (privilege == null) {
|
||||||
privilege = new Privilege(name);
|
privilege = new Privilege(name);
|
||||||
|
@ -84,7 +84,7 @@ public class SetupDataLoader implements ApplicationListener<ContextRefreshedEven
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@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);
|
Role role = roleRepository.findByName(name);
|
||||||
if (role == null) {
|
if (role == null) {
|
||||||
role = new Role(name);
|
role = new Role(name);
|
||||||
|
|
|
@ -13,7 +13,7 @@ public class UserService implements IUserService {
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserRepository repository;
|
private UserRepository repository;
|
||||||
|
|
||||||
public User findUserByEmail(final String email) {
|
public User findUserByEmail(String email) {
|
||||||
return repository.findByEmail(email);
|
return repository.findByEmail(email);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue