ApplyOncePerRequestFiltertoSpringBootActuators”改为中文。
创始人
2024-09-11 06:30:52
0

在Spring Boot应用程序中,有些Actuators需要额外的安全保护,例如,我们可能需要只允许授权的用户访问应用程序的某些端点。为此,可以使用Spring Security来保护Actuators,但默认情况下,Actuators的许多端点可以通过未经身份验证的请求进行访问。因此,需要在Spring Security中应用一个OncePerRequestFilter,以确保每个请求只执行一次。

下面是一个示例:

首先,在pom.xml文件中添加以下依赖项:


    org.springframework.boot
    spring-boot-starter-security

然后,创建一个SecurityConfig类来配置Spring Security:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/actuator/**").hasRole("ADMIN")
            .and()
            .httpBasic()
            .and()
            .csrf().disable();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
            .withUser("admin")
            .password("{noop}password")
            .roles("ADMIN");
    }
}

上面的代码将只允许具有'ADMIN”角色的用户访问Actuators端点,并使用HTTP基本身份验证对他们进行身份验证。

最后,在应用程序中注册一个OncePerRequestFilter:

@Component
public class ActuatorFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        if (request.getRequestURI().startsWith("/actuator")) {
            filterChain.doFilter(request, response);
        } else {
            super.doFilterInternal(request, response, filterChain);
        }
    }
}

上面的代码将应用程序的OncePerRequestFilter限制在Actuators端点上。

现在,Actuators将只允许经过身份验证的请求访问,并使用OncePerRequestFilter确保每个请求只执行一次。

相关内容

热门资讯

线上(wepoke真的)原来是... 线上(wepoke真的)原来是真的有挂!其实真的有挂(2022已更新)(哔哩哔哩);亲,其实确实真的...
两教程(Wepoke程序)软件... 两教程(Wepoke程序)软件透明挂辅助工具(软件透明挂)透视辅助(2024已更新)(哔哩哔哩);致...
软件(wepoke透明)原来是... 软件(wepoke透明)原来是真的有挂!其实真的有挂(2020已更新)(哔哩哔哩)是一款可以让一直输...
一模拟器(德扑工具)外挂辅助工... 一模拟器(德扑工具)外挂辅助工具(透视)透视辅助(2025已更新)(哔哩哔哩);亲真的是有正版授权,...
系统(aapoker讲解)竟然... 系统(aapoker讲解)竟然真的有挂!其实真的有挂(2021已更新)(哔哩哔哩);aapoker讲...
6系统(aapoker下载)外... 6系统(aapoker下载)外挂辅助工具(辅助挂)透视辅助(2023已更新)(哔哩哔哩)aapoke...
智能(德扑之星刷数据)果真真的... 智能(德扑之星刷数据)果真真的有挂!原来真的有挂(2025已更新)(哔哩哔哩);《WPK辅助透视》‌...
1机器人(德州nzt软件)软件... 1机器人(德州nzt软件)软件透明挂辅助软件(透视)透视辅助(2022已更新)(哔哩哔哩);人气非常...
ai代打(德扑之星决策)确实是... ai代打(德扑之星决策)确实是真的有挂!原来真的有挂(2020已更新)(哔哩哔哩);科技详细教程小薇...
第8透明(wepoke数据)外... 第8透明(wepoke数据)外挂透明挂辅助神器(辅助挂)透视辅助(2023已更新)(哔哩哔哩);原来...