我最近升级到,这意味着SpringBoot1.5.1,而且我再也不能通过检查oauth2范围来保护管理端点。它以前在中工作过。
这是以前使用过的配置:
@Configuration
public class SecurityConfiguration extends ResourceServerConfigurerAdapter {
@Value("${management.context-path}")
private String managementContextPath;
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
// some paths I don't want to secure at all
.antMatchers("/path1/**", "/path2/**").permitAll()
// access to health endpoint is open to anyone
.antMatchers(HttpMethod.GET, managementContextPath + "/health").permitAll()
// but app.admin scope is necessary for other management endpoints
.antMatchers(managementContextPath + "/**").access("#oauth2.hasScope('my-super-scope')") //
// And we make sure the user is authenticated for all the other cases
.anyRequest().authenticated();
}
}
这是配置的重要部分:
security:
oauth2:
client:
clientId: a-client
clientSecret: the-client-password
resource:
tokenInfoUri: http://my-spring-oauth2-provider/oauth/check_token
management:
context-path: /my-context
security:
enabled: true
endpoints:
health:
sensitive: false
当我试图在/my-context/refresh
上发布时,我得到了HTTP401“需要完全身份验证”,尽管我提供了一个有效的OAuth2令牌
通过查看日志,我发现我的请求被认为是匿名的,检查FilterChainProxy日志时发现OAuth2AuthenticationProcessingFilter
没有被调用。在深入研究我发现可以更改oauth2资源筛选器的顺序。之后,我尝试了一下,现在我有了一个OAuth2身份验证,是的,完成了对吧?
哼,不,现在我有一个Access is denied. User must have one of the these roles: ACTUATOR
错误。
我尝试了其他一些事情,包括禁用管理安全性(但我的规则不适用,访问对每个人都是开放的),使用(ugh) @Order
(不变),甚至,瞧,阅读和应用文件说:
若要重写应用程序访问规则,请添加类型为@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)的@Bean,如果不想重写执行器访问规则,则使用@Order(ManagementServerProperties.ACCESS_OVERRIDE_ORDER);如果要重写执行器访问规则,则使用@Order(ManagementServerProperties.ACCESS_OVERRIDE_ORDER)。
但是这并没有改变错误:User must have one of these roles: ACTUATOR
有人有解决办法/想法吗?
更新:我也在使用Zuul,所以我最终创建了一个特定的zuul路由到我需要的端点(在我的例子中是云总线刷新),在另一个没有公开的后端服务上不受保护,并使用oauth2保护该zuul路由。
不过,我还是把这个问题留在这里,如果有人能找到解决办法的话,可能会很有用。
发布于 2017-06-19 12:46:04
可能是明显的船长,但请看http://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-monitoring.html。您可以使用management.security.roles覆盖该角色,只需添加任何您的Oauth2凭据所具有的角色。
发布于 2018-10-10 02:13:37
我也面临着这个问题。我使用的解决方法是在我定义的新端点上公开执行器操作,只需调用执行器bean来处理请求。
例如,为了用Oauth2保护/my/refresh,我只是在{任何-api-前缀}/refreshConfig上公开了一个新的资源,并为这个URL在rest控制器上公开了一个请求处理程序;在rest控制器中,我连接了RefreshEndpoint bean,在请求处理程序中我只调用了refreshEndpoint.refresh()。
https://stackoverflow.com/questions/42280956
复制相似问题