spring security导致登录后从https跳转至http解决方案

1. 项目为spring boot项目,由原来的http连接更换为https连接,因项目中配置的了spring security,登录被spring security拦截重定向后会跳转到http

解决办法:

nginx中增加

proxy_set_header   X-Forwarded-Proto 'https';

yml文件中增加

server:
  use-forward-headers: true
  tomcat:
    remote-ip-header: x-forwarded-for
    protocol-header: x-forwarded-proto

security代码中增加以下

protected void configure(HttpSecurity http) throws Exception {
     http.requiresChannel().anyRequest().requiresSecure();

2.因项目内部使用fegin调用仍采用了http方式,即项目中https针对前端,http针对后端fegin需要做https通道排除

RequestMatcher requestMatcher = new NegatedRequestMatcher(new AntPathRequestMatcher("fegin接口调用"));
http.requiresChannel().requestMatchers(requestMatcher).requiresSecure();

  遇到的坑;requestMatchers不能传入多个new NegatedRequestMatcher,需要使用 AndRequestMatcher进行合并才可以,如

RequestMatcher a = new NegatedRequestMatcher(new AntPathRequestMatcher("/a"));
RequestMatcher b= new NegatedRequestMatcher(new AntPathRequestMatcher("b"));
RequestMatcher c = new NegatedRequestMatcher(new AntPathRequestMatcher("/c"));
AndRequestMatcher andRequestMatcher = new AndRequestMatcher(a,b,c);
http.requiresChannel().requestMatchers(andRequestMatcher).requiresSecure();

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值