【问题标题】:Access denied on jdbc-authentication with any user [OAUTH2]任何用户的 jdbc-authentication 访问被拒绝 [OAUTH2]
【发布时间】:2017-08-20 03:06:10
【问题描述】:

我的 Spring-Boot 应用程序中有一个带有 oauth2-authorization 的 jdbc-authentication。 这是我的 jdbc-authentication 代码:

auth.jdbcAuthentication().dataSource(dataSource)
      .usersByUsernameQuery(
       "select username, password, 1 from users where username = ?") 
      .authoritiesByUsernameQuery(
       "select u.username, r.name from users u, roles r, role_users ru "
       + "where u.username = ? and u.id =  ru.users_id  and ru.roles_id = r.id "); 

我的授权配置如下:

http.
    anonymous().disable()
    .requestMatchers().antMatchers("/api/v1/users/**")
    .and().authorizeRequests()
    .antMatchers("/api/v1/users/**").access("hasRole('ADMIN')")
    .and().exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler());

现在,当我对数据库中的用户进行身份验证时,一切正常。我使用具有角色 "ADMIN" 的用户进行身份验证,因此我认为它应该允许我访问 /api/v1/users/** 资源。

但我得到的只是access_denied 错误。我究竟做错了什么?如果您需要查看更多代码 sn-ps,请告诉我。感谢您的所有帮助。

【问题讨论】:

    标签: java authentication spring-boot oauth-2.0 authorization


    【解决方案1】:

    不是大专家,但我从未使用过 access(...)

    方法

    您是否尝试过使用 hasRole ?

    类似

    .antMatchers("/api/v1/users/**").hasRole("ADMIN")
    

    【讨论】:

    • 您的情况似乎与此类似:dzone.com/articles/… 看看 httpsecurity 配置,甚至规则的顺序也很重要(如此处所述stackoverflow.com/questions/30819337/…
    • 不知何故 /api/v1/users/** 端点不起作用。我将其更改为/api/v1/users,现在已授予访问权限。但不幸的是,所有用户(也包括那些没有“ADMIN”角色的用户)。
    猜你喜欢
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 2020-04-26
    • 2018-04-23
    • 2020-09-12
    • 1970-01-01
    相关资源
    最近更新 更多