spring security session过期 iframe嵌套解决方案

我开始的做法是,复写了org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler这个类,此类事spring security登入成功之后进行重定向的处理类,

public class MyAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
 
    
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
        RequestCache requestCache = new HttpSessionRequestCache();
        
        String url = null;
        SavedRequest savedRequest = requestCache.getRequest(request,response);
        if(savedRequest != null){
            url = savedRequest.getRedirectUrl();
        }
       if(url == null){//sesion过期url就会变成null,
            getRedirectStrategy().sendRedirect(request,response,"/user/judge.shtml");
        }
        super.onAuthenticationSuccess(request, response, authentication);
    }

}

spring security配置文件中

 <beans:property name="authenticationSuccessHandler">  
      <beans:bean class="com.service.security.MyAuthenticationSuccessHandler">  
        </beans:bean>   
    </beans:property> 

但是这种方式却没有解决iframe嵌套问题,只是很好处理了不同情况下的登入成功的跳转问题,扩展一下,例如根据用户权限控制登入成功访问首页


后来我想了一下,服务端是无法做到iframe跳出的,只能前端可以做,因此想到了js,在登入页面,我判断如果页面不是一级页面就让他变成一级,总而言之登入页面不能嵌套iframe

  if (window.parent != window){
   window.top.location.href = location.href;

  }

但是这样还是不能很好解决问题,又出现了新问题,spring security 会记住上次访问路径信息,如果我这样处理,假如我访问的是iframe的一个子页面,这时候让我从新登入,我登入,但是登入成功之后就直接跳到了那个子页面。对于这个问题,spring security中需要配置一下。

如果你复写了org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler这个类,你就需要如下配置

<beans:property name="authenticationSuccessHandler">  
<beans:bean class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">  
            <beans:property name="defaultTargetUrl" value="/user/judge.shtml"></beans:property>  
            <beans:property name="alwaysUseDefaultTargetUrl" value="true"></beans:property>  
        </beans:bean>  
    </beans:property> 

defaultTargetUrl指定默认登入页面,alwaysUseDefaultTargetUrl总是访问默认登入成功页面,设置成ture

这样就可以解决iframe嵌套问题

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值