Here is my Security config class, I am using BCryptPasswordEncoder
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Autowired
private UserDetailsService userDetailsService;
@Autowired
private PasswordEncoder encoder;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic()
.and()
.authorizeRequests()
.antMatchers(HttpMethod.POST,"/api/auth/register")
.permitAll()
.antMatchers(HttpMethod.GET,"/api/auth/resources")
.hasAuthority("USER")
.and()
.csrf().disable();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(encoder);
}
}
This is my implementation of the UserDetailsService
public class UserDetailsServiceImpl implements UserDetailsService{
@Autowired
private AccountRepository repo;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Account account = repo.findByUsername(username).orElseThrow(() -> new UsernameNotFoundException("No user found for username " + username));
User user =
new User(account.getUsername(), account.getPassword(), true, true, true, true, AuthorityUtils.createAuthorityList(account.getAuthorities()));
return user;
}
}
The POST method above is where I can provide the username and password which is stored in a MySQL table.
Now when I can call the GET method using Postman using the username and password I just added, I get a 401 Unauthorized error like below
{
"timestamp": "2020-05-23T20:12:10.165+0000",
"status": 401,
"error": "Unauthorized",
"message": "Unauthorized",
"path": "/api/auth/resources"
}