如何禁用 oData API 的 HTTP POST 请求



我正在使用oData和ServletRegistrationBean设置一个新的Java应用程序。我想禁用接收 POST 请求的选项并仅允许 GET 请求。

我应该在哪里设置它?我可以创建白名单/黑名单吗?

ServletRegistrationBean odataServRegstration = new ServletRegistrationBean(new CXFNonSpringJaxrsServlet(), "/odata/*");
Map<String, String> initParameters = new HashMap<>();
initParameters.put("javax.ws.rs.Application", "org.apache.olingo.odata2.core.rest.app.ODataApplication");
initParameters.put("org.apache.olingo.odata2.service.factory", "com.sap.context.JPAServiceFactory");
odataServRegstration.setInitParameters(initParameters);
return odataServRegstration;

在春季安全性中,您可以轻松配置,例如,只有角色管理员的用户才能发出非 GetRequests。我很快就会提供一个例子,除非你在此之前在网上找到它。其他未塞勒将收到 403。

一个简约的例子是:

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

注意.antMatchers(HttpMethod.POST, "/**").hasRole("ADMIN")

这是我禁用 POST 选项的方法。

@Override
@RequestMapping(method = {GET,  PATCH, DELETE})
public void service(final HttpServletRequest servletRequest, final HttpServletResponse servletResponse) throws ServletException {
    try {........................

最新更新