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确保每个请求只执行一次。

相关内容

热门资讯

透视玄学!aapoker辅助工... 《aapoker辅助工具免费下载软件透明挂》是一款多人竞技的aapoker辅助工具免费下载辅助透视游...
透视能赢!aapoker有脚本... 一、aapoker有脚本吗简介了解软件请加微:136704302aapoker有脚本吗是一款在线扑克...
透视科技!wepoker辅助透... 透视科技!wepoker辅助透视软件,wepoker私人局辅助挂,必备教程(最初真的有挂),亲,有的...
透视安卓版!wpk透视辅助,w... 您好,wpk透视辅助这款游戏可以开挂的,确实是有挂的,需要了解加Q群【1067239143】很多玩家...
透视数据!wepoker透视脚... 透视数据!wepoker透视脚本安卓,wepoker私人局外卦,wepoke教程(先前真的有挂);w...
透视智能ai!红龙poker透... 您好,aapoker辅助是真的吗这款游戏可以开挂的,确实是有挂的,需要了解加Q群【106723914...
透视app!德普之星辅助功能如... 这是一款非常优秀的德普之星辅助功能如何设置 ia辅助检测软件,能够让你了解到德普之星辅助功能如何设置...
透视了解!hhpoker免费透... 透视了解!hhpoker免费透视脚本,hhpoker开辅助软件,玩家必看科普(有挂详情)1、超多福利...
透视安装!智星德州菠萝透视插件... 透视安装!智星德州菠萝透视插件工具,wepoker有透视吗,透明挂教程(都是真的是有挂)智星德州菠萝...
辅助透视!德普之星透视辅助软件... 1、辅助透视!德普之星透视辅助软件是真的吗,wepoker作弊辅助,记者发布(有挂总结);详细教程。...