After implementing the spring security, application is running but I am not getting the access token and those services where I have implemented security are not working only getting unauthorized error.
Within the controller class I would be getting the token but I think I am missing something for that I am not getting it now
this is the API for login
Login Controller
@PostMapping("/auth/login")
public ResponseEntity<?> login(@RequestBody @Valid UsersDto request) {
SecurityContext context = (SecurityContext) SecurityContextHolder.getContext();
Authentication authentication = authManager
.authenticate(new UsernamePasswordAuthenticationToken(context.getAuthentication().getPrincipal(),context.getAuthentication().getCredentials()));
UserEntity user = (UserEntity) authentication.getPrincipal();
String accessToken = jwtUtil.generateAccessToken(user);
AuthResponse response = new AuthResponse(user.getName(), accessToken);
System.out.println("Getting");
return ResponseEntity.ok().body(response);
}
This is the config file
@EnableWebSecurity
@SuppressWarnings(value = { "warningOption", "deprecation" })
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class ApplicationSecurity extends WebSecurityConfigurerAdapter {
@Autowired
private UserRepository userRepo;
@Autowired
private UserDetailsService userDetailsService;
@Bean
public AuthenticationManager getAuthenticationManager() throws Exception {
return super.authenticationManagerBean();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(encodePWD());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.authorizeRequests().antMatchers("/users/add").permitAll().antMatchers("/users/auth/login").permitAll()
.anyRequest().authenticated().and().httpBasic();
}
@Bean
public BCryptPasswordEncoder encodePWD() {
return new BCryptPasswordEncoder();
}
}
UserDetailsclass
public class CustomizedUserPrincipal implements UserDetails {
private static final long serialVersionUID = 8632209412694363798L;
private UserEntity userEntity;
public UserEntity getUserEntity() {
return userEntity;
}
public void setUserEntity(UserEntity user) {
this.userEntity = user;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return null;
}
@Override
public String getPassword() {
// TODO Auto-generated method stub
return userEntity.getPassword();
}
@Override
public String getUsername() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isAccountNonExpired() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isAccountNonLocked() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isCredentialsNonExpired() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isEnabled() {
// TODO Auto-generated method stub
return true;
}