Why are cookies unrecognized when a link is clicked from an external source (i.e. Excel, Word, etc…)

后端 未结 17 2544
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-28 04:39

I noticed that when a link is clicked externally from the web browser, such as from Excel or Word, that my session cookie is initially unrecognized, even if the link opens u

17条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 05:05

    Here is how to workaround this with Java and Spring via a Filter:

    /**
     * To see why this is necessary, check out this page:
     * https://support.microsoft.com/en-gb/help/899927.
     */
    public class MicrosoftFilter extends OncePerRequestFilter {
      @Override
      protected void doFilterInternal(final HttpServletRequest request,
          final HttpServletResponse response,
          final FilterChain filterChain) throws ServletException, IOException {
        //Serve up a blank page to anything with a Microsoft Office user agent, forcing it to open the
        //URL in a browser instead of trying to pre-fetch it, getting redirected to SSO, and losing
        //the path of the original link.
        if (!request.getHeader("User-Agent").contains("ms-office")) {
          filterChain.doFilter(request, response);
        }
      }
    }
    
    /**
     * Security configuration.
     */
    @Configuration
    public class SecurityConfiguration {
      @Bean
      public FilterRegistrationBean microsoftFilterRegistrationBean() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new MicrosoftFilter());
        registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return registrationBean;
      }
    }
    

提交回复
热议问题