2017-07-13 135 views
0

此网址http://localhost:8070/produits与邮差工作fine.It返回此否认403:春季安全访问后得到

enter image description here

加入春季安全性之后,这个网址返回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"); 


    } 


} 

REST式服务

@Autowired 
private ProduitRepository produitRepository; 


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

enter image description here

+0

你试过'inMemoryAuthentication'吗?只要确定你的'auth'配置有问题。 – kamwo

+0

或者更好地调试“JdbcDaoImpl.loadUserByUsername(String username)”以查找系统是否能够成功找到给定的用户 – Afridi

+0

@Afridi我存储了使用MD5编码的密码 –

回答

2

据我可以从截图中看到 - 你试图使用基本在邮递员身份验证。如果是这样,你至少应该启用它。试试这个:

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

我试过你的目的,但我有这个问题: 解析HTTP请求标头时出错 注意:在DEBUG级别会记录更多的HTTP标头解析错误。 java.lang.IllegalArgumentException:在请求目标中找到无效的字符。有效字符在RFC 7230和RFC 3986 –

+0

@AmirChoubani中定义,当你点击你的'/ product' URL时你会得到这个字符吗?你能提供完整的网址,标题等吗? – Leffchik

+0

错误不再存在。 现在,随着http.httoBasic(),并与网址: 的http://本地主机:8070/PRODUITS 它显示了未经核实的用户名和密码 –