java - Spring Security 不适用于其他帖子网址
问题描述
对于这个问题,我没有找到令人满意的答案。
我有一个安全配置,到目前为止一直运行良好。
我想再添加一个POST
网址,所有人都可以访问。
虽然其他排除的网址运行良好,但添加的额外添加的网址不起作用。
我的代码:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors()
.and()
.csrf().disable()
.authorizeRequests()
.antMatchers(HttpMethod.POST, "/ws/**").authenticated()
.antMatchers(HttpMethod.DELETE, "/**").authenticated()
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.antMatchers(HttpMethod.GET, "/ws/getEvents").permitAll()// ---> While this is working
.antMatchers(HttpMethod.POST, "/ws/persons/createNotificationsSubscriber*").permitAll()// -->this not working
.anyRequest().authenticated()
.and()
.logout()
.logoutSuccessUrl("http://localhost:3006/eventsMainView")
.and()
.csrf().disable()
.httpBasic();
}
解决方案
这里的问题是
.antMatchers(HttpMethod.POST, "/ws/**").authenticated()
表示使用 POST 请求验证从/ws开始的所有 URL,但
.antMatchers(HttpMethod.POST,"/ws/persons/createNotificationsSubscriber*").permitAll() // --> this not working
这从相同的/ws开始,它是一个 POST 请求,所以 Spring 不允许这样做
做你的工作,请使用这个-
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors()
.and()
.csrf().disable()
.authorizeRequests()
.antMatchers(HttpMethod.POST, "/ws/persons/createNotificationsSubscriber*").permitAll()// --> This will work
.antMatchers(HttpMethod.POST, "/ws/**").authenticated()
.antMatchers(HttpMethod.DELETE, "/**").authenticated()
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.antMatchers(HttpMethod.GET, "/ws/getEvents").permitAll()// ---> While this is working
.anyRequest().authenticated()
.and()
.logout()
.logoutSuccessUrl("http://localhost:3006/eventsMainView")
.and()
.csrf().disable()
.httpBasic();
}
推荐阅读
- android - 广播接收器未使用警报管理器触发
- python - 使用 Selenium循环浏览网页的标签
- python - NodeJS没有来自python子的输出
- discord - 1.5.x版本如何获取discord服务器中所有用户的列表
- c++ - 为什么将变量调用为 remove() 的参数会出错?
- javascript - 如何在 React js 中使用 axios 将一个或多个文件发送到 API?
- qt - Qt3D 渲染的基本问题
- reactjs - changing the dark mode color in chakra ui
- android - ADB - how to check if file contains a string
- c# - ModelBinding throws error No parameterless constructor defined for this object