首页 > 解决方案 > Spring security 401 Unauthorized 尽管有 permitAll

问题描述

我正在尝试使用 Spring Boot 做我的第二个应用程序。即使我使用permittAll方法,我的安全配置也有问题。这是我的代码的一部分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 Unauthorized,尝试了很多方法,但不知道该怎么做。当然,Rest 控制器处理 /register 和 /users。

预先感谢您的任何帮助。

标签: javaspringspring-bootspring-security

解决方案


推荐阅读