【发布时间】:2017-06-27 19:24:03
【问题描述】:
这是我的 Spring Boot 1.5.1 Actuator application.properties:
#Spring Boot Actuator
management.contextPath: /actuator
management.security.roles=R_0
这是我的WebSecurityConfig:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Value("${logout.success.url}")
private String logoutSuccessUrl;
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http.addFilterBefore(new CorsFilter(), ChannelProcessingFilter.class);
http
.csrf().ignoringAntMatchers("/v1.0/**", "/logout")
.and()
.authorizeRequests()
.antMatchers("/oauth/authorize").authenticated()
//Anyone can access the urls
.antMatchers("/signin/**").permitAll()
.antMatchers("/v1.0/**").permitAll()
.antMatchers("/auth/**").permitAll()
.antMatchers("/actuator/health").permitAll()
.antMatchers("/actuator/**").hasAuthority("R_0")
.antMatchers("/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/login")
.failureUrl("/login?error=true")
.usernameParameter("username")
.passwordParameter("password")
.permitAll()
.and()
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl(logoutSuccessUrl)
.permitAll();
// @formatter:on
}
/**
* Configures the authentication manager bean which processes authentication requests.
*/
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
}
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}
现在我可以使用具有R_0 权限的正确用户成功登录我的应用程序,但是当我尝试访问时
http://localhost:8080/api/actuator/beans
我收到以下错误:
There was an unexpected error (type=Forbidden, status=403).
Access is denied. User must have one of the these roles: R_0
如何正确配置 Spring Boot Actuator 以了解正确的 Authentication ?
现在为了让它工作,我必须做以下技巧:
management.security.enabled=false
.antMatchers("/actuator/health").permitAll()
.antMatchers("/actuator/**").hasAuthority("R_0")
是否有机会以正确的方式配置执行器?
更新
我正在使用UserDetailsService.UserDetails.Authorities
public Collection<? extends GrantedAuthority> getAuthorities() {
String[] authorities = permissions.stream().map(p -> {
return p.getName();
}).toArray(String[]::new);
return AuthorityUtils.createAuthorityList(authorities);
}
【问题讨论】:
-
谢谢,但我可以通过
.antMatchers("/actuator/**").hasAuthority("R_0")来管理这个问题。问题是我无法使用默认的 Spring Boot Actuator 安全集成,而无需提供像这样的自定义配置。 -
我遇到了同样的问题。请注意,通过在
AuthenticationManagerBuilder上设置UserDetailsService会以某种方式覆盖默认安全配置,这意味着需要像通过HttpSecurity类所做的那样显式 URI 访问配置。您是否设法使其按您最初的预期工作? -
是的,您必须为您的
management.security.roles使用前缀ROLE_,例如management.security.roles=ROLE_SOMENAME -
由于我使用的是 Spring Boot 1.3.5,因此我的问题与此属性的复数与单数格式有关。对于我的版本,您只能将一个角色定义为,例如
management.security.role=ACTUATOR,在 1.4.x 版本中已更改为复数等效项。此外,我为AuthenticationManagerBuilder类的UserDetails检索组合了两个来源,一个是我的服务用户的UserDetailsService的实现,第二个是执行器和管理员用户的InMemoryUserDetailsManagerConfigurer的实例(你可以逃脱也只有一个)。
标签: java spring spring-boot spring-security spring-boot-actuator