I have problem with validating user credentials. When I give correct credentials first time everything goes OK but giving invalid credentials first and then give correct ones I get invalid credentials error. I use Postman Basic Auth.
My config class:
@Configuration @EnableWebSecurity public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserService userService; @Autowired private CustomAuthenticationEntryPoint authenticationEntryPoint; @Override protected void configure(HttpSecurity http) throws Exception { http.cors().and().csrf().disable().authorizeRequests() .antMatchers(HttpMethod.POST ,"/login").permitAll() .antMatchers("/admin").hasAuthority("ADMIN") .anyRequest().authenticated().and().exceptionHandling().authenticationEntryPoint(authenticationEntryPoint).and() .sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.ALWAYS).and() .logout() .deleteCookies("remove") .invalidateHttpSession(true); http.rememberMe().disable(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(this.userService) .and().eraseCredentials(true); } @Bean public BCryptPasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); }
And my controller class
@PostMapping public ResponseEntity loginButtonClicked(HttpServletRequest request) { HttpSession session = request.getSession(); final String authorization = request.getHeader("Authorization"); String[] authorizationData=null; if (authorization != null && authorization.startsWith("Basic")) { // Authorization: Basic base64credentials String base64Credentials = authorization.substring("Basic" .length()).trim(); String credentials = new String(Base64.getDecoder().decode(base64Credentials), Charset.forName("UTF-8")); // credentials = username:password authorizationData = credentials.split(":", 2); UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(authorizationData[0], authorizationData[1],Arrays.asList(new SimpleGrantedAuthority("USER"))); User user = userService.findUserEntityByLogin(authorizationData[0]); if(user != null && user.getFromWhenAcceptLoginAttempts() != null && (user.getFromWhenAcceptLoginAttempts()).isBefore(LocalDateTime.now())){ // Authenticate the user Authentication authentication = authenticationManager.authenticate(authRequest); SecurityContext securityContext = SecurityContextHolder.getContext(); securityContext.setAuthentication(authentication); // Create a new session and add the security context. session = request.getSession(); session.setAttribute("SPRING_SECURITY_CONTEXT", securityContext); return new ResponseEntity(new LoginResponseObject(200,"ACCESS GRANTED. YOU HAVE BEEN AUTHENTICATED"), HttpStatus.OK); }else{ session.getId(); SecurityContextHolder.clearContext(); if(session != null) { session.invalidate(); } return new ResponseEntity(new ErrorObject(403,"TOO MANY LOGIN REQUESTS","YOU HAVE ENTERED TOO MANY WRONG CREDENTIALS. YOUR ACCOUNT HAS BEEN BLOCKED FOR 15 MINUTES.", "/login"), HttpStatus.FORBIDDEN); } }else{ session.getId(); SecurityContextHolder.clearContext(); if(session != null) { session.invalidate(); } return new ResponseEntity(new ErrorObject(401,"INVALID DATA","YOU HAVE ENTERED WRONG USERNAME/PASSWORD CREDENTIALS", "/login"), HttpStatus.UNAUTHORIZED); } } @Bean public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } @Bean public ObjectMapper objectMapper(){ return new ObjectMapper(); } @Bean public HttpSessionEventPublisher httpSessionEventPublisher() { return new HttpSessionEventPublisher(); }
authorizationData = credentials.split(":", 2);
=> Are you sure? Ifcredentials
containsuser:password
, I don't know what iscredentials.split(":", 2);
– Arnaud Denoyelle