我正在尝试使用Spring Boot做我的第二个应用程序。即使我使用allowtAll方法,我在安全配置方面也会遇到问题。这是我使用WebSecurityConfig的代码的一部分:
@Configuration
@EnableWebSecurity(debug=true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
MyUserDetailsService userDetailsService;
@Autowired
private PasswordEncoder passwordEncoder;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.httpBasic()
.and()
.authorizeRequests()
.antMatchers("/register").permitAll()
.antMatchers("/users").authenticated()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider());
}
@Bean
public DaoAuthenticationProvider authenticationProvider(){
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setPasswordEncoder(passwordEncoder);
provider.setUserDetailsService(userDetailsService);
return provider;
}
}
当我在/ register上使用Postman时,我有401未经授权,试图通过多种方式进行操作,但不知道该怎么办。当然Rest控制器处理/ register和/ users。
在此先感谢您的帮助。