SpringBoot不断请求登录凭据

2cmtqfgy  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(244)

嗯,我目前正在一个springsecurity项目上编程,我想在数据库中注册一个用户。
问题是,如果我访问应该注册的端点,我会被重定向到spring boot的默认登录页,而这不应该发生。
这是我的 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 {
}

}

我的控制器打电话来了 `UserController.java` 看起来像这样:

@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";
}

}

mv1qrgav

mv1qrgav1#

试试这个。

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
       http.formLogin().disable();
    }

}

相关问题