spring boot AuthenticationManager每次返回403

spring boot AuthenticationManager每次返回403,spring,rest,spring-boot,spring-security,basic-authentication,Spring,Rest,Spring Boot,Spring Security,Basic Authentication,我是spring security新手,尝试在spring boot rest服务上实现基本身份验证。我使用基于数据库的身份验证,并拥有用户和角色表。当我请求应用程序中任何具有正确凭据的控制器时,它总是给我403禁止。我不明白为什么。我多次检查角色是否正确。在数据库角色名为“USER”和“RESTAURANT”以及“ADMIN”。我尝试了角色前缀和独立大写语法,但都不起作用。不知道我做错了什么。下面是我的配置类: @Configuration @EnableWebSecurity @Enable

我是spring security新手,尝试在spring boot rest服务上实现基本身份验证。我使用基于数据库的身份验证,并拥有用户和角色表。当我请求应用程序中任何具有正确凭据的控制器时,它总是给我403禁止。我不明白为什么。我多次检查角色是否正确。在数据库角色名为“USER”和“RESTAURANT”以及“ADMIN”。我尝试了角色前缀和独立大写语法,但都不起作用。不知道我做错了什么。下面是我的配置类:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true,prePostEnabled=true,jsr250Enabled=true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{

    @Autowired
    private UserDetailsService customUserDetailsService;

    @Autowired
    private AuthenticationEntryPoint authEntryPoint;

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth)
        throws Exception
    {
        auth.userDetailsService(customUserDetailsService)
        .passwordEncoder(passwordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf().disable()
            .httpBasic()
            .authenticationEntryPoint(authEntryPoint)
            .and()
            .authorizeRequests()
            .antMatchers("/user/register","/forgotPassword").permitAll()
            .anyRequest().authenticated()
            .and()
            .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
             ;
    }
    }
以下是我的UserDetailService实现:

@Service
public class CustomUserDetailsService implements UserDetailsService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException{
        User user = userRepository.findByUsername(username);
        System.out.println(user.toString()); //here i check if it's finding right user 
        if (user == null) {
            throw  new UsernameNotFoundException(username +" not found");
        }
        return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), getAuthorities(user));
    }

    private static Collection<? extends GrantedAuthority> getAuthorities(User user)
    {
        String[] userRoles = user.getRoles()
                                    .stream()
                                    .map((role) -> role.getName())
                                    .toArray(String[]::new);
        Collection<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList(userRoles);
        return authorities;
    }
}

每一个帮助和建议都将被精确化。

将角色保存为数据库中的ROLE\u USER、ROLE\u ADMIN并添加特定方法可能会对您有所帮助

.antMatchers(HttpMethod.POST,"/user/register","/forgotPassword").permitAll()
.antMatchers(HttpMethod.GET,"/restaurantURL").permitAll()
编辑:

在数据库中将角色保存为ROLE\u USER、ROLE\u ADMIN并添加特定方法可能会对您有所帮助

.antMatchers(HttpMethod.POST,"/user/register","/forgotPassword").permitAll()
.antMatchers(HttpMethod.GET,"/restaurantURL").permitAll()
编辑:

是否引发了任何异常(可能正在记录)?@PreAuthorize(“hasRole('ROLE\u USER'))是否引发了任何异常(可能正在记录)?@PreAuthorize(“hasRole('ROLE\u USER'))可能的重复项实际工作了!谢谢,伙计,我只保存了带有角色前缀的角色,并且工作了,但我想知道为什么必须这样?为什么spring security无法解析唯一的角色名?它实际上起了作用!谢谢,伙计,我只保存了带有角色前缀的角色,并且工作了,但我想知道为什么必须这样?为什么spring security不能只解析角色名?
.antMatchers(HttpMethod.POST,"/user/register","/forgotPassword").permitAll()
.antMatchers(HttpMethod.GET,"/restaurantURL").permitAll()