serverhttpsecurity 缺少_不允许使用HTTP 405-Spring Boot + Spring Security

我有一个简单的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 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方法?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值