Search code examples
javaspringspring-bootspring-security

Spring Boot keeps asking for login credentials


Well, I am currently programming on a project with Spring Security where I'd like to register a user in the database.

The proble is, if I vistit the endpoint where the registration should happen I get redirected to the default Login page of Spring Boot which should not happen.

This is my SecurityConfiguration.java

    public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Bean
    public BCryptPasswordEncoder encodePasswd(){
        return new BCryptPasswordEncoder();
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/").permitAll();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/**");
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    }
}

My RestController called UserController.java looks like this:

@RestController
@RequestMapping("/secure/rest")
public class UserController {
    @Autowired
    private UserRepository userRepository;

    private BCryptPasswordEncoder passwordEncoder =  new BCryptPasswordEncoder();

    @PostMapping("user/register")
    public String addUser(@RequestBody User user){
        String passwd = user.getPasswordUser();
        String encryptPasswd = passwordEncoder.encode(passwd);
        user.setPasswordUser(encryptPasswd);
        userRepository.save(user);
        return "addedUser";
    }

}

Solution

  • Try this.

    @Configuration
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
        
        @Override
        protected void configure(HttpSecurity http) throws Exception {
           http.formLogin().disable();
        }
        
    }