gpt4 book ai didi

java - Spring Boot Actuator Endpoints 安全性不适用于自定义 Spring 安全配置

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:00:22 24 4
gpt4 key购买 nike

这是我的 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")

是否有机会以正确的方式配置执行器?

已更新

我正在使用 UserDetailsS​​ervice.UserDetails.Authorities

    public Collection<? extends GrantedAuthority> getAuthorities() {
String[] authorities = permissions.stream().map(p -> {
return p.getName();
}).toArray(String[]::new);
return AuthorityUtils.createAuthorityList(authorities);
}

最佳答案

您必须为您的 management.security.roles 使用前缀 ROLE_ 例如 management.security.roles=ROLE_SOMENAME 才能解决这个问题

关于java - Spring Boot Actuator Endpoints 安全性不适用于自定义 Spring 安全配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42142556/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com