chenfromsz / correct

15 stars 6 forks source link

Spring Security Access denied 403 after get #9

Open amirensit opened 7 years ago

amirensit commented 7 years ago

This url http://localhost:8070/produits with Postman works fine

After adding spring security, this url return 403 access denied even with correct username and password.

SecurityConfig.java


    import javax.sql.DataSource;

@Configuration

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter    {

    @Autowired
    private DataSource dataSource;

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

        /*auth.inMemoryAuthentication().withUser("admin").password("1234").roles("ADMIN","USER");
        auth.inMemoryAuthentication().withUser("user").password("1234").roles("USER");*/

        auth.jdbcAuthentication().dataSource(dataSource)
        .usersByUsernameQuery("select username as principal,password as credentials,active from users where username =?").
        authoritiesByUsernameQuery("select username as principal,roles as role from users_roles where username =?")
        .rolePrefix("ROLE_").passwordEncoder(new Md5PasswordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests().antMatchers("/produits").hasRole("USER");

    }

}
The restful service

@Autowired
private ProduitRepository produitRepository;

@RequestMapping(value="/produits",method=RequestMethod.GET)
public List<Produit> listProduits()
{
    return produitRepository.findAll();
}
chenfromsz commented 7 years ago

Increase in configuration:

@Bean Public, JdbcTokenRepositoryImpl, tokenRepository () { JdbcTokenRepositoryImpl jtr = new, JdbcTokenRepositoryImpl (); Jtr.setDataSource (dataSource); Return jtr; }

Reference resources: Https://github.com/chenfromsz/spring-boot-security.git