【问题标题】:Spring Security - url rewritingSpring Security - url重写
【发布时间】:2011-08-09 11:04:52
【问题描述】:

我使用org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint 和useForward=true 和loginFormUrl="/login.jsp",因此我的应用程序的登录网址是-http://sub.domain.com

成功登录后,我重定向到http://sub.domain.com/app。 Spring MVC Dispatcher Servlet 映射到所有/app/* 请求。

我的问题是:

  1. 注销时,我正在重定向用户 到login.jsp,浏览器中的url 更改为 http://sub.domain.com/login.jsp 但是 我希望网址是 http://sub.domain.com 和以前一样 第一次登录。我怎样才能实现 这?
  2. 一旦登录,如果用户 试图访问 - http://sub.domain.com,结果是 404 错误。我怎样才能重定向 用户http://sub.domain.com/app 每当请求 http://sub.domain.com 进来。

【问题讨论】:

    标签: spring spring-mvc spring-security


    【解决方案1】:

    对于 1) 在 spring security <logout> 元素中指定 logout-success-url。

    @参见第 B.1.13 章 Spring Security Reference 的元素

    对于2)指定一个默认的目标url(用于成功登录)

     default-target-url="/app" 
     always-use-default-target="true" 
    

    @参见第 B.1.5 章 Spring Security Reference 的元素

    或者对于一般解决方案,@see Stackoverflow 问题:Redirect from index page in Spring and Tomcat

    【讨论】:

    • 对于我的第一个问题的答案,注销确实有效,但我想要的是注销后sub.domain.com 的地址栏网址,而不是sub.domain.com/login.jsp。对于第二个答案,default-target-url 用于指定用户在登录后应立即重定向到的 url。我想要的是在他输入 url 时将经过身份验证的用户重定向到该 url (sub.domain.com/app) - sub.domain.com(只要经过身份验证的 Principal 在会话中可用)。
    • @Joji:对于第二个:“我想要的是在经过身份验证的用户输入 url - sub.domain.com 时将他重定向到该 url (sub.domain.com/app)(如只要经过身份验证的主体在会话中可用)” - 将所有用户重定向到此 url,并保护它仅适用于登录用户。
    【解决方案2】:

    对于您的第一个问题,请在注销期间将重定向发送到应用程序的根目录

    @RequestMapping(value="/logout", method={RequestMethod.GET, RequestMethod.POST} )
        public void logout(HttpServletResponse response) {
    
            //Do the real logout, clear session.
    
            try {
                response.sendRedirect("/");
                } catch (IOException e) {
                   //handle the exception
                }
        }
    

    【讨论】:

      猜你喜欢
      • 2010-12-18
      • 2011-12-07
      • 1970-01-01
      • 2014-06-18
      • 2017-11-06
      • 2014-03-26
      • 2017-11-27
      • 1970-01-01
      • 2021-07-11
      相关资源
      最近更新 更多