我有Spring引导rest应用程序。最近,我使用JsonWebToken实现了用户身份验证。
当我使用postman测试它时,一切似乎都很好,但是当其他部分从角应用程序调用时,就会出现引用cors的错误:
对飞行前请求的响应不会通过访问控制检查:请求的资源上没有“访问-控制-允许-原产地”标题。因此,“http://localhost:4200”源是不允许访问的。响应具有HTTP状态代码403。
这是我的代码:
@EnableWebSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private JwtAuthFilter authFilter;
@Autowired
private JwtAuthenticationProvider authenticationProvider;
@Autowired
private JwtAuthenticationEntryPoint authenticationEntryPoint;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
//http.csrf().ignoringAntMatchers("/token");
http.csrf().disable();
http.cors().and().authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.antMatchers("/token").permitAll()
.antMatchers("/rest/**").authenticated()
.and()
.addFilterBefore(authFilter, UsernamePasswordAuthenticationFilter.class)
.exceptionHandling()
.authenticationEntryPoint(authenticationEntryPoint);
}
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("*"));
configuration.setAllowedMethods(Arrays.asList("GET","POST","OPTIONS","PUT","DELETE"));
configuration.setAllowCredentials(true);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/rest/**", configuration);
return source;
}
}JwtAuthFilter:
@Component
public class JwtAuthFilter implements Filter
{
@Override
public void doFilter(ServletRequest request, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException
{
HttpServletRequest servletRequest = (HttpServletRequest) request;
HttpServletResponse response = (HttpServletResponse) servletResponse;
String authorization = servletRequest.getHeader("Authorization");
String origin = servletRequest.getHeader("Origin");
if (authorization != null)
{
JwtAuthToken token = new JwtAuthToken(authorization.replaceAll("Bearer ", ""));
SecurityContextHolder.getContext().setAuthentication(token);
}
filterChain.doFilter(servletRequest, response);
}
@Override
public void destroy()
{
}
@Override
public void init(FilterConfig filterConfig) throws ServletException
{
}
}我的登录控制器有一个映射"/token",所有其他控制器都映射为"/rest/**“有趣的是,调用"/token”控制器很好.它返回令牌和用户详细信息。服务器响应包含:
但当我给其他控制器打电话..。例如,"/rest/task/all“出现了错误,并且没有访问控制允许凭据和访问控制允许源标头。
当我改变
source.registerCorsConfiguration("/rest/**", configuration);转到source.registerCorsConfiguration("/**", configuration);
我在访问控制允许源头http://localhost:4200和http://localhost:4200中有两个值,这是错误的。
有人能提供帮助吗:D?
发布于 2019-03-26 12:20:10
这是个老问题&我不确定你是否找到了答案。我只能建议调试这些问题的方法,因为这些问题通常是在本地设置的。
1.首先,仅使用Java代码不足以调试CORS问题。您需要查看浏览器网络选项卡,并查看哪些调用失败-选项调用或实际数据调用。其次,在浏览器中,请求和响应标头需要仔细检查,以确定失败的请求&这些请求需要列出,特别是在下面。下面这些。
应请求方- Origin
在响应端- Access-Control-Allow-Credentials,Access-Control-Allow-Headers,Access-Control-Allow-Methods和Access-Control-Allow-Origin
2.
当我使用postman测试它时,一切似乎都很好,但是当其他部分从角应用程序调用时,就会出现引用cors的错误:
这是正常行为,因为CORS是浏览器安全特性,您可能在任何其他客户端中都看不到这个问题。CORS错误并不意味着您的API调用没有成功,它只意味着浏览器正在抑制响应&而不是向前推进。
因此,如果您的API使用者不是web浏览器,那么您很可能在API方面对CORS没有什么可做的。其他客户端不通过包含OPTIONS请求而使其成为一个两步过程。
3.配置行- http.cors()将向过滤器链中添加filter - org.springframework.web.filter.CorsFilter,因此在JwtAuthFilter中放置断点,以查看代码流的流向。
4.对于url - /token & /rest/**,系统行为预计会有所不同,因为令牌url是不安全的,而rest/**只允许通过身份验证的用户使用,因此会出现额外的过滤器。
Spring Security exclude url patterns in security annotation configurartion
How to disable spring security for particular url
因此,查找CORS问题的理想方法是通过接收有问题的请求来调试代码&验证响应标头- Access-Control-Allow-Origin是否正确填充。
https://stackoverflow.com/questions/51635327
复制相似问题