Spring 4 mvc我总是得到Request方法' POST'不支持

时间:2016-03-15 21:46:26

标签: spring-mvc spring-security spring-4

我的问题是我总是Request method 'POST' not supported! 我试图测试我的其余网络服务。这是WS:

@RequestMapping(value = "/addGouvernorate", method = RequestMethod.POST, produces = { "application/json" })
    public ResponseEntity<Gouvernorate> addGouvernorateee(@RequestBody Gouvernorate gouvernorate) throws IOException {
        gouvernorateService.addGouvernorat(gouvernorate);
        return new ResponseEntity<Gouvernorate>(gouvernorate, HttpStatus.OK);
    }

我试图用jsoup调用它,如下所示:

package test.sofiane.rqs;

import java.io.IOException;

import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.springframework.security.crypto.codec.Base64;

public class Test {

    public static void main(String[] args) throws IOException {
        String base64login = new String(Base64.encode("sof:1".getBytes()));
        Response docs = Jsoup.connect("http://localhost:8080/site/data/addGouvernorate")
                .data("name", "name")
                .data("delegation", "delegation")
                .data("district", "district")
                .data("postalCode", "postalCode")
                .method(Method.POST)
                .execute();
    }
}

我得到例外:

Exception in thread "main" org.jsoup.HttpStatusException: HTTP error fetching URL. Status=405, URL=http://localhost:8080/mintad/data/addGouvernorate
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:459)
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:434)
    at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:181)
    at test.sofiane.rqs.Test.main(Test.java:21)

使用Postman应用程序,我得到了

Etat HTTP 405 - Request method 'POST' not supported

我也使用弹簧安全。

如下

package com.site.spring.security;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;

/**
 * @author Sofiane
 */

@Configuration
@EnableWebSecurity
@ComponentScan
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    DataSource dataSource;

    @Autowired
    @Qualifier("customUserDetailsService")
    UserDetailsService userDetailsService;

    @Autowired
    public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        http.authorizeRequests()
            .antMatchers("/", "/home").permitAll()
            //.antMatchers("/admin/**").denyAll()//.access("hasRole('ADMIN')")
            .and().formLogin().loginPage("/login")
            .defaultSuccessUrl("/welcome").usernameParameter("username").passwordParameter("password")
            .and().exceptionHandling().accessDeniedPage("/404");
        // @formatter:off
    }
}

一个猜测:它可能是我必须发送的csrftoken,因为我在Spring安全性背后但是如何生成它? 请帮助,因为它驱使我疯狂的家伙

0 个答案:

没有答案