gpt4 book ai didi

java - Spring Boot - 不允许使用 Post 方法,但 GET 有效

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:08:15 30 4
gpt4 key购买 nike

我的 spring boot mysql 项目有问题, Controller 类只为 METHOD GET(get all) 查找,但我似乎无法发布并得到错误 405:方法“POST”不允许

这是我的 Controller 类:

 package com.example.demo.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.example.demo.Blog;
import com.example.demo.repository.BlogRespository;

import java.util.List;
import java.util.Map;

@RestController
public class BlogController {

@Autowired
BlogRespository blogRespository;

@GetMapping("/blog")
public List<Blog> index(){
return blogRespository.findAll();
}

@GetMapping("/blog/{id}")
public Blog show(@PathVariable String id){
int blogId = Integer.parseInt(id);
return blogRespository.findById(blogId)
.orElseThrow(() -> new IllegalArgumentException(
"The requested resultId [" + id +
"] does not exist."));
}

@PostMapping("/blog/search")
public List<Blog> search(@RequestBody Map<String, String> body){
String searchTerm = body.get("text");
return blogRespository.findByTitleContainingOrContentContaining(searchTerm, searchTerm);
}

@PostMapping("/blog")
public Blog create(@RequestBody Map<String, String> body){
String title = body.get("title");
String content = body.get("content");
return blogRespository.save(new Blog(title, content));
}

@PutMapping("/blog/{id}")
public Blog update(@PathVariable String id, @RequestBody Map<String, String> body){
int blogId = Integer.parseInt(id);
// getting blog
Blog blog = blogRespository.findById(blogId)
.orElseThrow(() -> new IllegalArgumentException(
"The requested resultId [" + id +
"] does not exist."));
blog.setTitle(body.get("title"));
blog.setContent(body.get("content"));
return blogRespository.save(blog);
}


@DeleteMapping("blog/{id}")
public boolean delete(@PathVariable String id){
int blogId = Integer.parseInt(id);
blogRespository.delete(blogId);
return true;
}


}

如果你需要的话,这是我的存储库类

package com.example.demo.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.example.demo.Blog;

import java.util.List;

@Repository
public interface BlogRespository extends JpaRepository<Blog, Integer> {

// custom query to search to blog post by title or content
List<Blog> findByTitleContainingOrContentContaining(String text, String textAgain);

}

我正在尝试使用 SoapUI 发送 POST 请求,但似乎找不到解决方案,非常感谢

最佳答案

如果您配置或启用了 csrf,则不允许发布方法那么您需要在发布表单或数据时提供有效的 csrf

为此检查你的 spring 安全配置例如

    @Configuration
@EnableWebSecurity
@ComponentScan(basePackageClasses = CustomUserDetailsService.class)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
.....

RequestMatcher csrfRequestMatcher = new RequestMatcher() {
// Enabled CSFR protection on the following urls:
//@formatter:off
private AntPathRequestMatcher[] requestMatchers =
{
new AntPathRequestMatcher("/**/verify"),
new AntPathRequestMatcher("/**/login*")
};
//@formatter:off

@Override
public boolean matches(final HttpServletRequest request) {
// If the request match one url the CSFR protection will be enabled
for (final AntPathRequestMatcher rm : requestMatchers) {
if (rm.matches(request)) {
System.out.println();
/* return true; */
}
}
return false;
} // method matches
};
@Override
protected void configure(final HttpSecurity http) throws Exception {
//@formatter:off

http.headers().frameOptions().sameOrigin()
.and()
.authorizeRequests()
.antMatchers("/","/css/**", "/static/**", "/view/**", "**/error/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/mvc/login").permitAll()
.authenticationDetailsSource(authenticationDetailsSource())
.successHandler(authenticationSuccessHandler)
.usernameParameter("username").passwordParameter("password")
.and()
.logout().permitAll()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.addLogoutHandler(customLogoutHandler)
.logoutSuccessHandler(customLogoutSuccessHandler)
.logoutSuccessUrl("/login?logout")
.and()
.exceptionHandling()
.accessDeniedPage("/403")
.and()
.csrf()/* .requireCsrfProtectionMatcher(csrfRequestMatcher) */
.ignoringAntMatchers("/crud/**","/view/**")
;
// @formatter:off


}

谢谢

关于java - Spring Boot - 不允许使用 Post 方法,但 GET 有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50756842/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com