Spring Boot安全性的CORS问题 [英] CORS problems with spring boot security
问题描述
我使用弹簧靴,弹簧安全装置,并希望避免发生任何损坏。我正在尝试从此处获取第二个答案(可以在Spring中完全禁用CORS支持吗? ?)我需要在cors上添加自定义过滤器,这里是过滤器:
I use spring boot, spring security and want to avoid any cors actions. Im trying the second answer from here (Can you completely disable CORS support in Spring?) where I need to add custom filter to cors, here is the filter:
@Component
public class CorsFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response,
final FilterChain filterChain) throws ServletException, IOException {
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, PATCH, HEAD");
response.addHeader("Access-Control-Allow-Headers", "Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers");
response.addHeader("Access-Control-Expose-Headers", "Access-Control-Allow-Origin, Access-Control-Allow-Credentials");
response.addHeader("Access-Control-Allow-Credentials", "true");
response.addIntHeader("Access-Control-Max-Age", 10);
filterChain.doFilter(request, response);
}
}
因此,我在此处创建了CorsFilter类插入,我得到了错误:
So Ive created the class insert there CorsFilter and I got the mistake:
'springSecurityFilterChain' threw exception; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'corsFilter' is expected to be of type 'org.springframework.web.filter.CorsFilter' but was actually of type 'fa.backend.core.security.CorsFilter'
我的安全配置如下:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and().antMatcher("/users").authorizeRequests()
.antMatchers(HttpMethod.POST, "/login").permitAll()
.and()
.addFilterBefore(
new JWTLoginFilter(tokenAuthentication, authenticationManagerBean()),
UsernamePasswordAuthenticationFilter.class)
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
}
您能告诉我如何解决吗?我的目标是禁用任何cors操作,因为我从前端收到:
Could you tell me how to fix it? My aim is to disable any cors actions, because from frontend I recieve:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8081' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
推荐答案
在之后删除 cors()。and部分配置覆盖中的http。以下是代码段。
remove the 'cors().and' part after http in the configure override. Following is the snippet.
@Override
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/users").authorizeRequests()
.antMatchers(HttpMethod.POST, "/login").permitAll()
.and()
.addFilterBefore(
new JWTLoginFilter(tokenAuthentication, authenticationManagerBean()),
UsernamePasswordAuthenticationFilter.class)
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
}
这篇关于Spring Boot安全性的CORS问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!