我的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
错误。我究竟做错了什么?如果您需要查看更多代码段,请告诉我们。我感谢你的帮助。
答案 0 :(得分:1)
不是大专家,但我从未使用访问(...)
方法您是否尝试过使用hasRole?
类似
.antMatchers("/api/v1/users/**").hasRole("ADMIN")