繁体   English   中英

获取后Spring Security Access拒绝403

[英]Spring Security Access denied 403 after get

带有Postman的URL http://localhost:8070/produits可以正常工作,它返回以下内容:

在此处输入图片说明

添加Spring安全性后,即使使用正确的用户名和密码,该URL返回403访问也会被拒绝。

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");


    }


}

宁静的服务

@Autowired
private ProduitRepository produitRepository;


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

在此处输入图片说明

据我从您的屏幕截图中看到的-您正在尝试在邮递员中使用基本身份验证。 如果是这样,则至少应启用它。 尝试这个:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.httpBasic();
    http.authorizeRequests().antMatchers("/produits").hasRole("USER");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM