Oauth 2.0 Spring boot OAuth2资源服务器中基于角色的访问控制,角色来自Auth server
我已经用spring boot创建了一个授权服务器,我想在资源服务器中使用其中的资源所有者角色。我有一个类SecurityConfig扩展WebSecurityConfigureAdapter,其中我检查了来自mongodb的资源所有者的凭据以进行身份验证。为此,我有一个类MongouthProvider,它实现了AuthenticationProvider,我从中返回一个带有用户名、密码和角色的UsernamePasswordAuthenticationToken实例,例如“ROLE\u ADMIN”、“ROLE\u APPUSER” 我想在资源服务器中使用授权服务器数据库中的资源所有者角色。为此,我有一个类ResourceServer扩展ResourceServerConfigurerAdapter,在这个类中,我试图从auth-server检查用户角色。资源服务器工作正常。问题是它无法从身份验证服务器检查角色Oauth 2.0 Spring boot OAuth2资源服务器中基于角色的访问控制,角色来自Auth server,oauth-2.0,spring-boot,spring-security-oauth2,spring-oauth2,Oauth 2.0,Spring Boot,Spring Security Oauth2,Spring Oauth2,我已经用spring boot创建了一个授权服务器,我想在资源服务器中使用其中的资源所有者角色。我有一个类SecurityConfig扩展WebSecurityConfigureAdapter,其中我检查了来自mongodb的资源所有者的凭据以进行身份验证。为此,我有一个类MongouthProvider,它实现了AuthenticationProvider,我从中返回一个带有用户名、密码和角色的UsernamePasswordAuthenticationToken实例,例如“ROLE\u AD
@SpringBootApplication
@EnableResourceServer
@EnableOAuth2Sso
public class AuthserverClientApplication extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(AuthserverClientApplication.class, args);
}
@Configuration
protected static class ResourceServer extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("**")
//.hasAuthority("ROLE_ADMIN")
.hasRole("ADMIN")
.anyRequest().authenticated();
}
}
}
}
请帮助我如何使用资源服务器中授权服务器的角色进行基于角色的访问。您能看到我的基本oauth2项目吗?你能提供更多关于这个问题的信息吗?有例外吗?日志?我没有得到任何例外。ResourceServer的配置方法中的hasRole(“ADMIN”)不工作,它正在向http请求提供拒绝访问的消息。如果我从管理员处向用户添加了评论角色或替换角色,则一切正常。
@RequestMapping("/user")
public Principal user(OAuth2Authentication user) {
return user;
}
@SpringBootApplication
@EnableResourceServer
@EnableOAuth2Sso
public class AuthserverClientApplication extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(AuthserverClientApplication.class, args);
}
@Configuration
protected static class ResourceServer extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("**")
//.hasAuthority("ROLE_ADMIN")
.hasRole("ADMIN")
.anyRequest().authenticated();
}
}
}
}