spring boot post请求403,get请求成功

项目使用了spring boot  security.使用的spring boot版本是spring boot2.0.

post请求403错误,表示资源不可用。服务器理解客户的请求,但拒绝处理它,通常由于服务器上文件或目录的权限设置导致的WEB访问错误。

Spring Security CSRF 保护默认是开启的,那么在 POST 方式提交表单的时候就必须验证 Token,如果没有,那么自然也就是 403 没权限了。

需要添加spring security相关配置:

   @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/admin/health")
            .permitAll()
            .anyRequest()
            .authenticated()
            .and()
            .httpBasic()
            .and()
            .formLogin()
            .disable()
            .csrf()
            .disable();

        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
            .withUser(user)
            .password(passwordEncoder().encode(password))
            .roles();
}

 

 

Spring Boot Gateway 是基于 Spring Framework 的响应式网关,它可以帮助开发者在微服务架构中提供统一的路由和过滤器功能。在 Spring Boot Gateway 中,你可以通过编写路由规则来指定哪些请求被转发到特定的服务。如果你需要配置只转发 GETPOST 请求,你可以使用内置的谓词工厂来实现这一需求。 下面是一个配置示例,展示了如何在 Spring Boot Gateway 中只转发 GETPOST 请求: ```java import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class GatewayConfig { @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("get_post_route", r -> r .method(HttpMethod.GET, HttpMethod.POST) .path("/your/path/**") .filters(f -> f.stripPrefix(1)) .uri("http://your-service-url")) .build(); } } ``` 在上面的代码中: - `RouteLocatorBuilder` 用于构建路由规则。 - `route` 方法定义了一个路由,其中 `"get_post_route"` 是路由的唯一标识。 - `method(HttpMethod.GET, HttpMethod.POST)` 指定了只有 GETPOST 请求才会被这个路由规则匹配。 - `path("/your/path/**")` 指定了请求路径的匹配模式,这里使用 `/**` 来匹配所有以 `/your/path` 开头的路径。 - `filters(f -> f.stripPrefix(1))` 是一个过滤器配置,这里使用 `stripPrefix` 来去掉路径中的一级前缀。 - `uri("http://your-service-url")` 指定了请求被转发到的目标服务地址。 通过这样的配置,只有 GETPOST 请求会被路由到指定的后端服务,其他类型的请求则不会被转发。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值