简体   繁体   中英

how to spring boot redirect http to https except admin in this code

@Configuration
@EnableConfigurationProperties
public class SSLConfig {

    @Value("${server.port.http}")
    private int httpPort;

    @Value("${server.port}")
    private int httpsPort;

    @Bean
    public ServletWebServerFactory serverFactory(){
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);
            }
        };


        tomcat.addAdditionalTomcatConnectors(createSslConnector());

        return tomcat;
    }

    private Connector createSslConnector(){
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setPort(httpPort);
        connector.setScheme("http");
        connector.setSecure(false);
        connector.setRedirectPort(httpsPort);
        return connector;
    }

I'm using this solution. Currently, all User Manager pages are redirected to https, but you want to keep the Administrator page as http. Is there a way? Administrators are included in the same project.

You must add a new security collection for administration and set the user constraint to NONE.

Try something like:

   SecurityConstraint securityConstraint = new SecurityConstraint();
   securityConstraint.setUserConstraint("NONE");
   SecurityCollection collection = new SecurityCollection();
   /*for example I will choose /admin as pattern here, you can choose whatever 
   you want*/
   collection.addPattern("/admin/*"); 
   securityConstraint.addCollection(collection);
   context.addConstraint(securityConstraint);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM