如何在prettyFaces中使用captureWith方法

时间:2018-08-19 14:15:32

标签: jsf prettyfaces

我想在请求http url时强制使用https url。我在prettyfaces论坛中找到了this。但是此代码会给出cannot find symbol错误。我该如何解决?

return ConfigurationBuilder.begin()
    .addRule()
    .when(URL.captureIn("url").and(Path.matches("/login")).and(Scheme.matches("http")))
    .perform(Redirect.permanent(URL.capturedWith("url").toScheme("https")));

1 个答案:

答案 0 :(得分:1)

尝试使用参数“换位”:

return ConfigurationBuilder.begin()
    .addRule()
    .when(URL.captureIn("url").and(Path.matches("/login")).and(Scheme.matches("http")))
    .perform(Redirect.permanent("{url}"))
    .where("url").transposedBy(new Transposition() { ... convert to HTTPS HERE ... });

https://github.com/ocpsoft/rewrite/blob/master/api/src/main/java/org/ocpsoft/rewrite/param/Transposition.java

您还可以通过自定义操作执行以下操作来实现相同的目的:

https://github.com/ocpsoft/rewrite/blob/master/config-servlet/src/test/java/org/ocpsoft/rewrite/servlet/config/SchemeChangeConfigurationProvider.java

public class SchemeChangeConfigurationProvider extends HttpConfigurationProvider
{
   @Override
   public int priority()
   {
      return 0;
   }

   @Override
   public Configuration getConfiguration(final ServletContext context)
   {
      Configuration config = ConfigurationBuilder.begin()

               .addRule().when(Scheme.matches("http")).perform(new HttpOperation() {

                  @Override
                  public void performHttp(HttpServletRewrite event, EvaluationContext context)
                  {
                     String url = event.getRequest().getRequestURL().toString().replaceFirst("http", "https");
                     Redirect.temporary(url).perform(event, context);
                  }
               });
      return config;
   }
}