请求方法“get”

ngynwnxp  于 2021-07-23  发布在  Java
关注(0)|答案(2)|浏览(286)

我的restapi在本地运行得很好,但是当我放置相同的jar并从云服务器运行它时,我只能调用get api,而不能从postmen调用post api。
下面是我的代码。
REST控制器

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/api/test")
public class TestController {

@GetMapping("/all")
public String allAccess() {
    return "Public Content.";
}

@PostMapping("/testpostA")
public ResponseEntity<?> testPostA() {
    return ResponseEntity.ok(new MessageResponse("Test POST successfully!"));
}

@PostMapping("/testpostB")
public String testPostB() {
    return "String return POST successfully.";
}
}

pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.2</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.eurogain</groupId>
<artifactId>WebPortal</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>WebPortal</name>
<description>Java Spring Boot project for the Eurogain web portal for both internal and external client.</description>
<properties>
    <java.version>15</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-envers</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-quartz</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt</artifactId>
        <version>0.9.1</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build></project>

Web安全配置适配器

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(
    // securedEnabled = true,
    // jsr250Enabled = true,
    prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
UserDetailsServiceImpl userDetailsService;

@Autowired
private AuthEntryPointJwt unauthorizedHandler;

@Bean
public AuthTokenFilter authenticationJwtTokenFilter() {
    return new AuthTokenFilter();
}

@Override
public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
    authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}

@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean();
}

@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    System.out.println("============================ Inside WebSecurityConfig ===========================Ken");
    http.csrf().disable().authorizeRequests().anyRequest().permitAll();
}}

最后,web应用程序:

@SpringBootApplication
public class WebPortalApplication {
public static void main(String[] args) {
    SpringApplication.run(WebPortalApplication.class, args);
}}

在本地运行相同的jar文件,调用所有post和get api都没有问题。但是,当我们将同一个jar移到aws ec2云示例并运行它时,我们只能调用get api。调用任何post api时, Postman 控制台引发以下错误:

但是,调用get api时没有问题:

服务器控制台输出-无错误:

更新-第1轮
使用wget测试,错误也为500。没有更多信息。

更新-第2轮测试使用wget post,并在修改websecurityconfig之后进行。这是照片:

服务器控制台在wget post上包含警告:

WebSecurity配置的更新代码:

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
public void configure(HttpSecurity http) throws Exception {

    http.cors().and().csrf().disable();
}
}
vx6bjr1n

vx6bjr1n1#

试试这样的配置:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CorsConfig {

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**").allowedMethods("HEAD", "GET", "PUT", "POST", "DELETE", "PATCH");
            }
        };
    }
}
kyvafyod

kyvafyod2#

问题是由于nginx反向代理处理引起的。在localhost中,我使用的是http,但在dev服务器中,我们只使用https。因此,我对所有restfulapi url都使用了https。

相关问题