User Details Service integration to the gateway.
hrandika opened this issue · comments
In 'double' project we use
auth.inMemoryAuthentication()
.withUser("user").password("password").roles("USER")
.and()
.withUser("admin").password("admin").roles("USER", "ADMIN", "READER", "WRITER")
.and()
.withUser("audit").password("audit").roles("USER", "ADMIN", "READER");
and we can login users with these details. But when we add
auth.userDetailsService(customUserDetailsService).passwordEncoder(passwordEncoder());
it gives an error with
java.lang.ClassNotFoundException: <pakage>.CustomUserDetailsService$UserRepositoryUserDetails
How to solve this issue. Or even get user details from database?
this is my way:
@Service
public class CustomUserDetailsService implements UserDetailsService
{
@Autowired
private UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String userName)
throws UsernameNotFoundException {
UsersEntity usersEntity = userRepository.findOne(userName);
if(usersEntity == null){
throw new UsernameNotFoundException("UserName "+userName+" not found");
}
SecurityUser user = new SecurityUser();
user.setUsername(usersEntity.getUsername());
user.setPassword(usersEntity.getPassword());
user.setEnabled(usersEntity.isEnabled());
return user;
}
@Entity
@Table(name = "users", schema = "", catalog = "spring_security")
public class UsersEntity {
private String username;
private String password;
private boolean enabled;
@Id
@Column(name = "username", nullable = false, insertable = true, updatable = true, length = 50)
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Basic
@Column(name = "password", nullable = false, insertable = true, updatable = true, length = 50)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Basic
@Column(name = "enabled", nullable = false, insertable = true, updatable = true)
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UsersEntity that = (UsersEntity) o;
if (enabled != that.enabled) return false;
if (username != null ? !username.equals(that.username) : that.username != null) return false;
if (password != null ? !password.equals(that.password) : that.password != null) return false;
return true;
}
@Override
public int hashCode() {
int result = username != null ? username.hashCode() : 0;
result = 31 * result + (password != null ? password.hashCode() : 0);
result = 31 * result + (enabled ? 1 : 0);
return result;
}
public interface UserRepository extends CrudRepository<UsersEntity, String> {
}
I use spring data jpa ,you must design the db for user table that have
mysql:
CREATE TABLE `users` (
`username` varchar(50) NOT NULL,
`password` varchar(60) NOT NULL,
`enabled` bit(1) NOT NULL,
PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2.this is get password way
String password = "123456";
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
System.out.println(passwordEncoder.encode(password));
user password(123456)
admin $2a$10$qeW7Nv.L53Et7.UOTJPipeIUQIvaOH8yw76FWbe2xy.qhzeRlZ0O6 1
Seems like a general question on Spring Security. Stack Overflow probably best source for answer.
I'll think about adding a new blog on custom user details services.