spring is not redirecting to default target url?

后端 未结 5 1523
滥情空心
滥情空心 2020-12-19 06:15

I am using spring security for authentication. authentication is working fine. but after authentication it is not redirecting to the html that have mentioned using default t

相关标签:
5条回答
  • 2020-12-19 06:45

    An interesting gotcha here is if the browser requests a resource that requires an authenticated session - e.g. requesting a javascript file which requires the user to be logged in, this would generate a 403 error which if you have error pages configured in spring or web.xml would redirect the user to - however the user would never see this.

    Then when the user does log in, the last thing spring thinks the user requested was an error page and so the user gets directed to the error page and not the default page as you configure in spring

    0 讨论(0)
  • 2020-12-19 07:01

    I had similar problem and above answer gave me a hint, but since I am using configs directly from Java class following helped me:

    In docs.spring.io you can find:

    defaultSuccessUrl(String defaultSuccessUrl, boolean alwaysUse)

    Hence when changed

    .defaultSuccessUrl("/home")
    

    to

    .defaultSuccessUrl("/home", true)
    

    problem was solved.

    0 讨论(0)
  • 2020-12-19 07:06

    I know this is a rather old question, but some users can have this problem now too.

    1. Make sure that you can access the welcome.html page (relative to the context path). You can remove intercept-url or access it directly after login.
    2. Make sure you also specified the always-use-default-target="true"
    3. If you specified session-management make sure you're not redirected first to the invalid-session-url.
    0 讨论(0)
  • 2020-12-19 07:07

    If a user is sent to the login page after requesting a protected resource, they will be sent to the originally requested page after successful login. The default-target-url will only be used if the user logged in without requesting a protected resource first (i.e. they navigated directly to the login page). If you always want to go to the default-target-url you can specify always-use-default-target="true" as shown in the example below

    <form-login login-page="/login.jsp" 
                default-target-url="/welcome.html" 
                always-use-default-target="true"/>
    
    0 讨论(0)
  • 2020-12-19 07:09

    As you can see in the image, there is some kind of bad design (IMO It should always redirect to the default-target-url). When you go to the login url from a forbidden resource, it will redirect you to that URL and not going thru the default-target-url. Just use always-use-default-target="true" and you will have the desire behavior

    http://i.stack.imgur.com/fj9ou.png

    0 讨论(0)
提交回复
热议问题