程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在Spring Boot中将GenericFilterBean数据传递到WebSecurityConfigurerAdapter?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何在Spring Boot中将GenericFilterBean数据传递到WebSecurityConfigurerAdapter??

开发过程中遇到如何在Spring Boot中将GenericFilterBean数据传递到WebSecurityConfigurerAdapter?的问题如何解决?下面主要结合日常开发的经验,给出你关于如何在Spring Boot中将GenericFilterBean数据传递到WebSecurityConfigurerAdapter?的解决方法建议,希望对你解决如何在Spring Boot中将GenericFilterBean数据传递到WebSecurityConfigurerAdapter?有所启发或帮助;

这是解决此问题的解决方案。根据调查,由于8080和8082用于识别http通信和httpS通信,因此添加了一些代码来检查端口号,而不是“ isSecure”,以确定是否重定向http请求。代码如下:

public class IsSecureFilter extends GenericFilterBean {

private Boolean isSecure;
privatE int port;

@OverrIDe
public voID doFilter(Servletrequest request, ServletResponse response,
        FilterChain chain) throws IOException, servletexception {
    httpServletrequest req = new requestWrapper((httpServletrequest) request);
    httpServletResponse res = (httpServletResponsE) response;
    this.isSecure = req.isSecure();
    this.port = req.getLocalPort();


    System.out.println("[DEBUG] : isSecure FILTER :: " + isSecurE);
    System.out.println("[DEBUG] : port FILTER :: " + port);
    System.out.println("[DEBUG] : URL :: " + req.getrequestuRL());
    String url = req.getrequestuRL().toString().tolowerCase();
    if(url.endsWith("/login") && url.startsWith("http:") && port == 8080){
        url = url.replace("http:", "https:");
        String querIEs = req.getqueryString();
        if (querIEs == null) {
            querIEs = "";
        } else {
            querIEs = "?" + querIEs;
        }
        url += querIEs;
        res.sendRedirect(url);
    }
    else {
        chain.doFilter(req, responsE);
    }
}

public Boolean isSecure() {
    return this.isSecure;
}

public Boolean setIsSecure(Boolean isSecurE) {
    return this.isSecure = isSecure;
}

public int getPort() {
    return port;
}

public voID setPort(int port) {
    this.port = port;
}

}

并在WebSecurityConfiguration类中删除http.requiresChAnnel()。anyrequest()。requiresSecure()。

解决方法

我正在尝试使用以下命令在我的Spring Boot应用程序中将http重定向到https:

http.requiresChAnnel().anyrequest().requiresSecure();

但是我越来越ERR_TOO_MANY_REDIRECTS。原因是负载均衡器将所有https都转换为http并将http定向到端口8082,因此该应用似乎从未看到过https。

我尝试通过在httphttps重定向之前添加isSecure来解决此问题,例如在我的配置中:

public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
        //variables
    @Override
    protected void configure(httpSecurity http) throws Exception {
        http.authorizerequests().antMatchers("/css/**","/js/**","/admin/**")
                .permitAll().anyrequest().authenticated().and()
                .addFilterBefore(ssoFilter(),BasicAuthenticationFilter.class)
                .formLogin().loginPage("/login").permitAll().and()
                .logout().logoutsuccessUrl("/");

        //hsts
        http.headers().httpStrictTransportSecurity()
        .includeSubDomains(true).maxAgeInSeconds(31536000);

        http.addFilterBefore(new IsSecureFilter(),ChAnnelProcessingFilter.class);

        //https compulsion
        if(!isSecureFilter.isSecure()) {
                http.requiresChAnnel().anyrequest().requiresSecure();
        }           
    }
       //rest of the code
}

我尝试使用httpServletrequestWrapper,以便可以通过下面创建的IsSecureFilter在WebSecurityConfiguration中重复使用isSecure,以防止无限重定向:

public class requestWrapper extends httpServletrequestWrapper {
    private Boolean isSecure;

    public requestWrapper(httpServletrequest request) throws IOException
    {
        //So that other request method behave just like before
        super(request);
        this.isSecure = request.isSecure();       
    }

    //Use this method to read the request isSecure N times
    public Boolean isSecure() {
        return this.isSecure;
    }  
}

下面是我尝试在WebSecurityConfiguration中注入的过滤器,以使用上面的isSecure值:

@Component
public class IsSecureFilter extends GenericFilterBean {

    private Boolean isSecure;

    @Override
    public void doFilter(Servletrequest request,ServletResponse response,FilterChain chain) throws IOException,ServletException {
        httpServletrequest req = new requestWrapper((httpServletrequest) request);

        this.isSecure = req.isSecure();

        chain.doFilter(req,responsE);
    }

    public Boolean isSecure() {
        return this.isSecure;
    }
}

所以运行上面的代码并放入example.com/login浏览器确实重定向到了https://example.com/login,但是我仍然可以ERR_TOO_MANY_REDIRECTS。我不明白我在做什么错?我的第一个想法是:

  • 我可以在WebSecurityConfiguration中注入IsSecureFilter来检索isSecure吗?

  • 我是否以正确的方式将IsSecureFilter筛选器添加到配置中。

  • 包装过滤器关系是否正确定义?

编辑

1) 我更改http.addFilterAfter(new isSecureFilter(),ChAnnelProcessingFilter.class);http.addFilterAfter(isSecureFilter,ChAnnelProcessingFilter.class);,仍然没有效果。

2) 我尝试更改http.addFilterBefore(isSecureFilter,ChAnnelProcessingFilter.class);为,http.addFilterAfter(isSecureFilter,ChAnnelProcessingFilter.class);但仍然没有任何更改。

大佬总结

以上是大佬教程为你收集整理的如何在Spring Boot中将GenericFilterBean数据传递到WebSecurityConfigurerAdapter?全部内容,希望文章能够帮你解决如何在Spring Boot中将GenericFilterBean数据传递到WebSecurityConfigurerAdapter?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。