在Spring引导上使用OAuth2保护特定资源id
我在Spring Boot上有一个工作的OAUTH2实现,AuthorizationServer和ResourceServer在同一个实现上,使用密码授权。 关于服务器:在Spring引导上使用OAuth2保护特定资源id,spring,spring-boot,oauth,Spring,Spring Boot,Oauth,我在Spring Boot上有一个工作的OAUTH2实现,AuthorizationServer和ResourceServer在同一个实现上,使用密码授权。 关于服务器: TokenStore是自定义的,并使用Web服务远程存储令牌 我有一个自定义的AuthenticationProvider 这用于根据给定的权限控制对资源的访问,例如,在ResourceServer配置中: public void configure(HttpSecurity http) throws Except
- TokenStore是自定义的,并使用Web服务远程存储令牌
- 我有一个自定义的AuthenticationProvider
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/api/resource/**")
.hasAnyAuthority("USER", "ADMIN")
.antMatchers("/api/admin/**")
.hasAnyAuthority("ADMIN");
}
现在,我需要控制用户可以访问“/api/resource/1”,但不能访问“/api/resource/2”,此ID可以更改,我可以在身份验证期间获取列表
我尝试将ID列表添加到OAuth2AccessToken附加信息中,并在ResourceServer配置中添加自定义筛选器,但它总是为空
那么,在不使用JWT的情况下如何正确地实现这一点呢?经过一些思考和研究,如果有人试图实现类似的目标,我将这样做:
- 将允许的ID映射到权限,例如ID_201和作为角色的模块,因此我将有一个角色_ADMIN
- 可以在Web安全表达式中引用路径变量,如中所述。因此,我们的想法是传递变量(资源id)并检查它是否被允许
在Web安全配置中:public class WebSecurity { public boolean checkResourceId(Authentication authentication, int id) { //check if the list of authorities contains ID_{id}. } }
http .authorizeRequests() .antMatchers("/resource/{resourceId}/**").access("@webSecurity.checkResourceId(authentication,#resourceId)") ...