Q: problem with authentication after successfull login it redirecting me to the login page Hello guys after days of researching about my problem none of them works 
    with me i have a problem with authentication using (Spring) when the user 
    enter the username and password it redirecting me the login page , can 
    any one help me please about this situation . thank you.
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class SecurityConfig  extends WebSecurityConfigurerAdapter{

@Autowired
private Environment env;

@Autowired
private UserSecurityService userSecurityService;


private BCryptPasswordEncoder passwordEncoder(){
    return SecurityUtility.passwordEncoder();

}

private static final String[] POBLIC_MATCHERS = {

        "/css/**",
        "/js/**",
        "/images/**",
        "/**",
        "/newUser",
        "/login",
        "/resetPassword",
        "/Register-form",
        "/forgetPassword",
        "/products",
        "/searchByCategory",
        "/searchProduct",
        "/productDetail/**"
};



@Override
protected void configure(HttpSecurity http) throws Exception{
    http.
            authorizeRequests().
            antMatchers(POBLIC_MATCHERS).
            permitAll().anyRequest().authenticated();

    http.csrf().disable().cors().disable()
.formLogin().failureUrl("/loginForm?error=true")
//      .defaultSuccessUrl("/")
    .loginPage("/loginForm")
    .permitAll()
    .and().logout().logoutRequestMatcher(new 
AntPathRequestMatcher("/logout")).logoutSuccessUrl("/? 
logout").deleteCookies("remember-me").permitAll()
    .and().rememberMe();
    }
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws 
Exception{
     auth.userDetailsService(userSecurityService)
.passwordEncoder(passwordEncoder());

}


class Controller

@RequestMapping(value="/login")
public String myProfile(
        @ModelAttribute("username") String username,
        Model model
        ){
    User user =userService.findByUsername(username);

    if(user == null){//check if the user exist in database
        model.addAttribute("registerError", "Username or 
Password incorrect !");
        return "loginForm";

    } if(user.isEnabled()==false){ //check if the user is enabled 
or not
        model.addAttribute("messageError", "you have to confirm 
your account to continue login !");
        return "loginForm";
    }

    UserDetails principal = 
userSecurityService.loadUserByUsername("username");
    UsernamePasswordAuthenticationToken authentication = new 
UsernamePasswordAuthenticationToken(principal,principal.getPassword(), 
principal.getAuthorities());

SecurityContextHolder.getContext().setAuthentication(authentication);
    model.addAttribute("loginError" , "login failed, incorrect 
password or username ! ");
    System.out.println("success login");

        return "myAccount";
    }


class UserSecurityService

 @Service
    public class UserSecurityService implements UserDetailsService {

    @Autowired
    private UserRepository userRepository;


    static final PasswordEncoder passwordEncoder = new 
    BCryptPasswordEncoder();
    @Override
    public UserDetails loadUserByUsername(String username) throws 
    UsernameNotFoundException{
        User user=userRepository.findByUsername(username);

        if(user == null){

            throw new UsernameNotFoundException("Username not 
    found");
        }

        return user;
    }

    }

