The request was rejected because the header value “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWe

抛出的具体异常

org.springframework.security.web.firewall.RequestRejectedException: 
The request was rejected because the header value 
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) 
å讯é/2.0.0 Chrome/87.0.4280.141 Electron/11.5.0 
Safari/537.36" is not allowed. 

提示用户请求头有非法数据(å讯é)被SpringSecurity拦截了

解决方法

在nginx服务代理上固定该请求头值,把非法字符去掉即可

proxy_set_header User-Agent "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.289 Safari/537.36"; 
Spring Security中,请求因头部值不被允许而被拒绝,通常是由于Spring Security的安全策略对某些特殊字符或格式的头部值进行了限制。可以参考以下几种可能的解决方案: ### 自定义防火墙策略 可以自定义Spring Security的防火墙策略,允许特定的头部值。以下是一个示例代码: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.web.firewall.DefaultHttpFirewall; import org.springframework.security.web.firewall.HttpFirewall; @Configuration public class SecurityConfig { @Bean public HttpFirewall allowUrlEncodedSlashHttpFirewall() { DefaultHttpFirewall firewall = new DefaultHttpFirewall(); firewall.setAllowUrlEncodedSlash(true); // 可以添加其他允许的配置 return firewall; } } ``` 通过上述代码,自定义了一个防火墙策略,允许特定的头部值。 ### 对头部值进行编码 可以在前端对头部值进行编码,例如使用JavaScript的`encodeURIComponent()`方法,然后在后端进行解码。参考引用[3],可以在前端代码中对参数使用`encodeURIComponent()`进行编码。 ### 配置CORS 如果问题是由于跨域请求引起的,可以配置CORS(跨域资源共享)来允许特定的头部值。以下是一个Spring Boot的示例配置: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; import java.util.Arrays; @Configuration public class CorsConfig { @Bean public CorsFilter corsFilter() { CorsConfiguration config = new CorsConfiguration(); config.setAllowCredentials(true); config.addAllowedOriginPattern("*"); config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS")); config.setAllowedHeaders(Arrays.asList("*")); config.setExposedHeaders(Arrays.asList("*")); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", config); return new CorsFilter(source); } } ``` 通过上述配置,允许所有的头部值。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值