大佬教程收集整理的这篇文章主要介绍了不允许使用HTTP 405-Spring Boot + Spring Security,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
您忘记了csrf
-Token。
建议您在Meta-tag中添加csrf-Token。您可以在Spring Security文档中阅读它
这样,您可以执行以下操作:
$(function () {
var token = $("Meta[name='_csrf']").attr("content");
var header = $("Meta[name='_csrf_header']").attr("content");
$(document).AJAXSend(function(e, xhr, options) {
xhr.setrequestheader(header, token);
});
});
我有一个简单的rest API,可与数据库一起使用。在添加安全部分之前,它一直工作正常。现在,它在POST和deletE请求上提供http
405不允许。我不知道为什么。GET请求正常工作。
所以这是控制器类:
@Controller
public class MarkerController {
private Logger logger = Logger.getLogger(MarkerController.class.getName());
@Autowired
private MarkerserviceInterface markerservice;
@requestMapping(value="/markers",method=requestMethod.GET)
public @ResponseBody List<Marker> getMarkers(@requestParam(value="city",DefaultValue="") String city) {
logger.info("HANDLE GET requEST");
return this.markerservice.getAllMarkers();
}
@requestMapping(value="/markers/new",method=requestMethod.POST)
public @ResponseBody Marker addMarker(@requestBody Marker marker) {
logger.info("HANDLE POST requEST");
this.markerservice.addMarker(marker);
return marker;
}
@requestMapping(value="/markers/delete",method=requestMethod.Delete)
public @ResponseBody String deleteMarker(@requestParam(value="id",DefaultValue="") String id) {
logger.info("HANDLE deletE requEST");
if (!id.equals("")) {
logger.info(id);
this.markerservice.deleteMarker(Long.parseLong(id));
}
return "";
}
@requestMapping(value="/admin/map")
public String trafficSpy() {
logger.info("HANDLE MAP");
return "index";
}
@requestMapping(value="/admin")
public String admin() {
return "admin";
}
@requestMapping(value="/login")
public String login() {
return "login";
}
}
这是SecurityConfig:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
@Qualifier("userDetailsservice")
UserDetailsservice userDetailsservice;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth)
throws Exception {
auth.userDetailsservice(userDetailsservicE).passwordEncoder(
passwordEncoder());
}
@Override
protected void configure(httpSecurity http) throws Exception {
http
.authorizerequests()
.antMatchers("/admin/**")
.access("hasRole('ROLE_ADMIN')")
.antMatchers("/markers/**")
.access("hasRole('ROLE_USER')")
.and()
.formLogin()
.loginPage("/login")
.failureUrl("/login?error")
.usernameParameter("username")
.passwordParameter("password")
.and()
.logout()
.logoutsuccessUrl("/login?logout")
.and()
.csrf()
.and()
.exceptionHandling()
.accessDeniedPage("/403");
}
@Bean
public passwordEncoder passwordEncoder() {
passwordEncoder encoder = new BCryptpasswordEncoder();
return encoder;
}
@Bean
public DaoAuthenticationProvider authprovider() {
DaoAuthenticationProvider authprovider = new DaoAuthenticationProvider();
authprovider.setUserDetailsservice(userDetailsservicE);
authprovider.setpasswordEncoder(passwordEncoder());
return authprovider;
}
}
使用以下ajax代码调用deletE请求:
$.ajax({
url: "localhost:8080/markers/delete?id=" + currentMarker.get("id"),type: 'deletE',success: function(result) {
console.log(result);
}
});
这是控制台中给出的消息:
2015-05-11 15:48:13.671 WARN 8279 --- [nio-8181-exec-6] o.s.web.servlet.PageNotFound : request method 'deletE' not supported
这些是响应的标头。我可以看到,在AlLLOW中,我只有GET和HEAD。因此,如果我是对的,则意味着控制器中的方法仅接受GET和HEAD请求。
(Status-LinE) http/1.1 405 Method Not Allowed
Server Apache-Coyote/1.1
x-content-type-options nosniff
x-xss-protection 1; mode=block
Cache-Control no-cache,no-store,max-age=0,must-revalidate
Pragma no-cache
Expires 0
X-Frame-Options DENY
Allow GET,HEAD
Content-Type application/json;charset=UTF-8
Transfer-Encoding chunked
Date Mon,11 May 2015 17:35:31 GMT
在回应中,我有这个例子:
org.springframework.web.httprequestMethodNotSupportedException
知道是什么引起了这个问题吗?如何允许POST和deletE方法?
以上是大佬教程为你收集整理的不允许使用HTTP 405-Spring Boot + Spring Security全部内容,希望文章能够帮你解决不允许使用HTTP 405-Spring Boot + Spring Security所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。