Rewrite + Apache Shiro

Splash Forums Rewrite Users Rewrite + Apache Shiro

Tagged: ,

This topic contains 10 replies, has 2 voices, and was last updated by  naska.om 3 years, 2 months ago.

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #23795

    naska.om
    Participant

    I use apache shiro to secure a JSF application. How do I manage redirection to Login.xhtml page when a request is made to access /admin and then back to /admin/index.xhtml when the user has logged in using rewrite? The first part works but it does not redirect back to /admin/index.xhtml after successful login and returns back to Login instead and no successful login takes place. I have configured rewrite for other pages.

    #23796

    Could you share the relevant parts of your Rewrite and Shiro configuration?

    #23797

    naska.om
    Participant

    Sure.
    shiro.ini
    [main]
    authc.loginUrl = /Login.xhtml
    user.loginUrl = /Login.xhtml
    authc.successUrl = ../admin/index.xhtml

    [users]
    naska = naska

    [urls]
    /Login.xhtml = authc
    /admin/** = authc

    CustomRewriteConfigurationProvider:
    public Configuration getConfiguration(final ServletContext context)
    {
    return ConfigurationBuilder.begin()
    .addRule()
    .when(Direction.isInbound().and(Path.matches("/some/{page}/")))
    .perform(Forward.to("/new-{page}/"))
    // initial redirect to /home
    .addRule()
    .when(Path.matches("/"))
    .perform(Redirect.temporary(context.getContextPath() + "/Home"))

    .addRule(Join.path("/{Home}").to("/{Home}.xhtml")
    .withInboundCorrection())
    .when(DispatchType.isRequest())
    .where("Home").matches("[^/.]+");
    }

    #23798

    naska.om
    Participant

    Sure.
    shiro.ini`[main]
    authc.loginUrl = /Login.xhtml
    user.loginUrl = /Login.xhtml
    authc.successUrl = ../admin/index.xhtml

    [users]
    admin = password

    [urls]
    /Login.xhtml = authc
    /admin/** = authc`

    CustomRewriteConfigurationProvider:

    public Configuration getConfiguration(final ServletContext context)
       {
         return ConfigurationBuilder.begin()                 
        // initial redirect to /Home
         .addRule()
         .when(Path.matches("/"))
         .perform(Redirect.temporary(context.getContextPath() + "/Home"))  
                 
         .addRule(Join.path("/{Home}").to("/{Home}.xhtml")             
             .withInboundCorrection())
             .when(DispatchType.isRequest())
             .where("Home").matches("[^/.]+");
    }
    • This reply was modified 3 years, 2 months ago by  naska.om.
    • This reply was modified 3 years, 2 months ago by  Lincoln Baxter III.
    #23831

    It looks weird that you are using Home as a parameter. Do you only want to map /Home? In this case, you should try to use a literal value instead of a parameter:

    
    .addRule(Join.path("/Home").to("/Home.xhtml").withInboundCorrection())
    

    Does it work this way?

    #23836

    naska.om
    Participant

    Thanks Christian. Fixing the home link didn’t solve my problem.

    #23840

    naska.om
    Participant

    And by the way I had used home as a parameter to use the code to rewrite all my pages.

    #23888

    Hmmm. I’m a bit confused. Perhaps because I don’t know Shiro very much. So if I understand it correctly, Shiro should automatically redirect you to a target URL after the login was successful, correct? That’s what you configure using authc.successUrl, correct?

    Could you share your web.xml please?

    You could try to change the order of the RewriteFilter and the Shiro filter. You may also change the configuration of the Shiro filter by adding <dispatcher>FORWARD</dispatcher>. Or you have to change the URLs in shiro.ini to the rewritten ones.

    #23890

    naska.om
    Participant

    Yes Christian, Shiro automatically redirects you to the page you’ve been first or the one you are trying to reach(You entered the URL of that page in the address bar). I already changed the URLs in shiro.ini to the rewriten ones and my web.xml Shiro fileter includes<dispatcher>FORWARD</dispatcher>. but it didn’t solve the issue. This is Shiro filter in my web.xml file and I don’t have RewriteFilter?:
    <listener>
    <listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
    </listener>

    <filter>
    <filter-name>ShiroFilter</filter-name>
    <filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
    </filter>

    <filter-mapping>
    <filter-name>ShiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>ERROR</dispatcher>
    </filter-mapping>
    Btw I am sorry for late replies. We have 8 hours difference in our time zones.

    • This reply was modified 3 years, 2 months ago by  naska.om.
    • This reply was modified 3 years, 2 months ago by  naska.om.
    #23893

    The RewriteFilter is automatically registered in Servlet 3.0 environments. But if you want to have more control in regard to filter ordering, you will have to add it yourself.

    You could try to place the Rewrite filter after the Shiro filter like this:

    
    <!-- Shiro first, no dispatcher setting required -->
    <filter>
      <filter-name>ShiroFilter</filter-name>
      <filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
    </filter>
    <filter-mapping>
      <filter-name>ShiroFilter</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <!-- Rewrite after that -->
    <filter>
      <filter-name>OCPsoft Rewrite Filter</filter-name>
      <filter-class>org.ocpsoft.rewrite.servlet.RewriteFilter</filter-class>
      <async-supported>true</async-supported>
    </filter>
    <filter-mapping>
      <filter-name>OCPsoft Rewrite Filter</filter-name>
      <url-pattern>/*</url-pattern>
      <dispatcher>FORWARD</dispatcher>
      <dispatcher>REQUEST</dispatcher>
      <dispatcher>INCLUDE</dispatcher>
      <dispatcher>ASYNC</dispatcher>
      <dispatcher>ERROR</dispatcher>
    </filter-mapping>
    

    In this case Shiro only “sees” the pretty URLs. So you also have to use the pretty URLs in shiro.ini

    #23895

    naska.om
    Participant

    Thanks very much Christian. This solved the problem.

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.

Comments are closed.