请求方法“get”

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

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

  1. @CrossOrigin(origins = "*", maxAge = 3600)
  2. @RestController
  3. @RequestMapping("/api/test")
  4. public class TestController {
  5. @GetMapping("/all")
  6. public String allAccess() {
  7. return "Public Content.";
  8. }
  9. @PostMapping("/testpostA")
  10. public ResponseEntity<?> testPostA() {
  11. return ResponseEntity.ok(new MessageResponse("Test POST successfully!"));
  12. }
  13. @PostMapping("/testpostB")
  14. public String testPostB() {
  15. return "String return POST successfully.";
  16. }
  17. }

pom.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <parent>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-parent</artifactId>
  9. <version>2.4.2</version>
  10. <relativePath /> <!-- lookup parent from repository -->
  11. </parent>
  12. <groupId>com.eurogain</groupId>
  13. <artifactId>WebPortal</artifactId>
  14. <version>0.0.1-SNAPSHOT</version>
  15. <name>WebPortal</name>
  16. <description>Java Spring Boot project for the Eurogain web portal for both internal and external client.</description>
  17. <properties>
  18. <java.version>15</java.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.hibernate</groupId>
  23. <artifactId>hibernate-envers</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-data-jpa</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-mail</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-quartz</artifactId>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework.boot</groupId>
  39. <artifactId>spring-boot-starter-security</artifactId>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-starter-web</artifactId>
  44. </dependency>
  45. <dependency>
  46. <groupId>io.jsonwebtoken</groupId>
  47. <artifactId>jjwt</artifactId>
  48. <version>0.9.1</version>
  49. </dependency>
  50. <dependency>
  51. <groupId>org.springframework.boot</groupId>
  52. <artifactId>spring-boot-starter-validation</artifactId>
  53. </dependency>
  54. <dependency>
  55. <groupId>mysql</groupId>
  56. <artifactId>mysql-connector-java</artifactId>
  57. <scope>runtime</scope>
  58. </dependency>
  59. <dependency>
  60. <groupId>org.springframework.boot</groupId>
  61. <artifactId>spring-boot-starter-test</artifactId>
  62. <scope>test</scope>
  63. </dependency>
  64. <dependency>
  65. <groupId>org.springframework.security</groupId>
  66. <artifactId>spring-security-test</artifactId>
  67. <scope>test</scope>
  68. </dependency>
  69. </dependencies>
  70. <build>
  71. <plugins>
  72. <plugin>
  73. <groupId>org.springframework.boot</groupId>
  74. <artifactId>spring-boot-maven-plugin</artifactId>
  75. </plugin>
  76. </plugins>
  77. </build></project>

Web安全配置适配器

  1. @Configuration
  2. @EnableWebSecurity
  3. @EnableGlobalMethodSecurity(
  4. // securedEnabled = true,
  5. // jsr250Enabled = true,
  6. prePostEnabled = true)
  7. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  8. @Autowired
  9. UserDetailsServiceImpl userDetailsService;
  10. @Autowired
  11. private AuthEntryPointJwt unauthorizedHandler;
  12. @Bean
  13. public AuthTokenFilter authenticationJwtTokenFilter() {
  14. return new AuthTokenFilter();
  15. }
  16. @Override
  17. public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
  18. authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
  19. }
  20. @Bean
  21. @Override
  22. public AuthenticationManager authenticationManagerBean() throws Exception {
  23. return super.authenticationManagerBean();
  24. }
  25. @Bean
  26. public PasswordEncoder passwordEncoder() {
  27. return new BCryptPasswordEncoder();
  28. }
  29. @Override
  30. protected void configure(HttpSecurity http) throws Exception {
  31. System.out.println("============================ Inside WebSecurityConfig ===========================Ken");
  32. http.csrf().disable().authorizeRequests().anyRequest().permitAll();
  33. }}

最后,web应用程序:

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

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

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

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

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

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

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

WebSecurity配置的更新代码:

  1. @Configuration
  2. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  3. @Override
  4. public void configure(HttpSecurity http) throws Exception {
  5. http.cors().and().csrf().disable();
  6. }
  7. }
vx6bjr1n

vx6bjr1n1#

试试这样的配置:

  1. import org.springframework.context.annotation.Bean;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.web.servlet.config.annotation.CorsRegistry;
  4. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  5. @Configuration
  6. public class CorsConfig {
  7. @Bean
  8. public WebMvcConfigurer corsConfigurer() {
  9. return new WebMvcConfigurer() {
  10. @Override
  11. public void addCorsMappings(CorsRegistry registry) {
  12. registry.addMapping("/**").allowedMethods("HEAD", "GET", "PUT", "POST", "DELETE", "PATCH");
  13. }
  14. };
  15. }
  16. }
展开查看全部
kyvafyod

kyvafyod2#

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

相关问题