【问题标题】:Spring boot expiredURL not workingSpring Boot expiredURL 不起作用
【发布时间】:2018-08-06 09:57:28
【问题描述】:

我有“带有 Spring Boot 的 Vaadin 10”应用程序。我想允许用户一次从一个地方访问应用程序。所以我使用了maximumSessions(1)。例如,从 Chrome 浏览器中,我使用用户“XYZ”登录。现在使用同一个用户(即“XYZ”)我尝试登录到 Opera 浏览器。因此,按照下面显示的配置,它将使 Chrome 浏览器的会话过期,但不会重定向到“/login”。它显示消息“来自服务器的无效 JSON 响应”。下面是 Spring 的安全配置:

@Override
protected void configure(HttpSecurity http) throws Exception {
    // Not using Spring CSRF here to be able to use plain HTML for the login page
    http.csrf().disable()

            // Register our CustomRequestCache, that saves unauthorized access attempts, so
            // the user is redirected after login.
            .requestCache().requestCache(new CustomRequestCache())

            // Restrict access to our application.
            .and().authorizeRequests()
            .antMatchers("/ForgetPassword","/ChangePassword","/login").permitAll()
            // Allow all flow internal requests.
            .requestMatchers(SecurityUtils::isFrameworkInternalRequest).permitAll()

            // Allow all requests by logged in users.
            .anyRequest().authenticated()
            // Configure the login page.
            .and().formLogin().loginPage("/login").permitAll().loginProcessingUrl("/login")
            .failureUrl("/login?error")

            // Register the success handler that redirects users to the page they last tried
            // to access
            .successHandler(new SavedRequestAwareAuthenticationSuccessHandler())

            // Configure logout
            .and().logout().logoutSuccessUrl(LOGOUT_SUCCESS_URL)
            .deleteCookies("JSESSIONID")
            //.invalidateHttpSession(true)
            .and()
            .sessionManagement()
            //.invalidSessionUrl("/login")
            .maximumSessions(1)
            //.maxSessionsPreventsLogin(false)
            .sessionRegistry(sessionRegistry())
            .expiredUrl("/login");

【问题讨论】:

    标签: spring-boot spring-security vaadin session-management


    【解决方案1】:

    问题在于,通过重定向请求,Vaadin 接收登录页面作为对内部请求的响应。

    这似乎有效:

            .expiredSessionStrategy(e -> {
                final String redirectUrl = e.getRequest().getContextPath() + "/login";
                if(SecurityUtils.isFrameworkInternalRequest(e.getRequest())) {
                    e.getResponse().setHeader("Content-Type", "text/plain");
                    e.getResponse().getWriter().write("Vaadin-Refresh: " + redirectUrl);
                } else {
                    e.getResponse().sendRedirect(redirectUrl);
                }
            });
    

    它记录在 连接状态处理程序

    public interface ConnectionStateHandler {
    
    /**
     * A string that, if found in a non-JSON response to a UIDL request, will
     * cause the browser to refresh the page. If followed by a colon, optional
     * whitespace, and a URI, causes the browser to synchronously load the URI.
     *
     * <p>
     * This allows, for instance, a servlet filter to redirect the application
     * to a custom login page when the session expires. For example:
     * </p>
     *
     * <pre>
     * if (sessionExpired) {
     *     response.setHeader(&quot;Content-Type&quot;, &quot;text/html&quot;);
     *     response.getWriter().write(myLoginPageHtml + &quot;&lt;!-- Vaadin-Refresh: &quot;
     *             + request.getContextPath() + &quot; --&gt;&quot;);
     * }
     * </pre>
     */
    String UIDL_REFRESH_TOKEN = "Vaadin-Refresh";
    

    【讨论】:

    • 嗨 Tulio,我试过上面的代码。 它在以下条件下工作: 步骤 1:使用 user1 登录浏览器 chrome。步骤 2:使用同一用户从 Mozilla 登录。步骤 3:单击 chrome 浏览器中的任何按钮。这样可行。 但它在以下情况下不起作用: 步骤 1:使用 user1 登录浏览器 chrome。步骤 2:使用同一用户从 Mozilla 登录。步骤 3:在 chrome 浏览器中重新加载页面。它在.expiredSessionStrategy(e -&gt; e.getRequest().getSession(false).invalidate()) 行给出 NullPointerException
    • 嗨,哈里塔。我更改了答案以涵盖该条件。
    • 嗨 Tulio...非常感谢您的回复!我试过上面的代码。唯一的问题是:它将响应重定向到 localhost:8080/login。但它应该重定向到 localhost:8181/AppName/login。请指教。
    • 我将 redirectUrl 变量添加到答案中,因此重定向是上下文相关的。我无法重现端口从 localhost:8181 到 localhost:8080 的更改。您是否正在重定向到同一个 Web 应用程序?
    • Hurrey....它有效:) 非常感谢您的大力帮助。抱歉...编写不同的端口是我的错误。我只需要重定向到相同的 Web 应用程序。再次非常感谢你:)
    猜你喜欢
    • 2016-08-20
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-30
    相关资源
    最近更新 更多