【问题标题】:Is it possible to remotely delete cookies in frontEnd of app?是否可以远程删除应用程序前端的 cookie?
【发布时间】:2020-09-17 20:53:33
【问题描述】:

我正在根据其他应用提供的 API 调用从我的应用远程注销。 通常,当用户使用我的应用程序注销时,我使用 Spring .logout().deleteCookies()。

现在我们为许多应用程序使用一个授权服务器,我们需要确保从其中注销也会注销其他应用程序。

所以基本的想法是授权服务器将调用我的 API 比如说 /user/logout 当我接到这个电话时,我想要和 spring .logout().deleteCookies() 一样的东西。我怎样才能做到这一点?

是否可以将新 API 添加到 Spring 配置中?说改变这个配置:

.logout()
.logoutUrl(logoutUrl)
.invalidateHttpSession(true)
.clearAuthentication(true)
.logoutSuccessUrl("/")
.deleteCookies("JSESSIONID")

类似于:

.logout()
.logoutUrl(logoutUrl || 'api/user/logout)
.invalidateHttpSession(true)
.clearAuthentication(true)
.logoutSuccessUrl("/")
.deleteCookies("JSESSIONID")

?

我的整个spring配置是:

http
                        .addFilterBefore(new RedirectUrlFilter(projectDomainPath), OAuth2LoginAuthenticationFilter.class)
                        .csrf().disable().cors()
                        .and()
                        .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
                        .and()
                        .authorizeRequests().antMatchers("/static/**", "/logout", "/api/user/k_push_not_before", "/api/user/k_logout").permitAll()
                        .anyRequest().authenticated()
                        .and()
                        .addFilter(new JwtAuthorizationFilter(authenticationManager(), secret, clientId, "nblues", tokenVerificationUrl))
                        .logout()
                        .logoutUrl(logoutUrl)
                        .logoutRequestMatcher(new AntPathRequestMatcher("/api/user/k_push_not_before"))
                        .invalidateHttpSession(true)
                        .clearAuthentication(true)
                        .logoutSuccessHandler(new CustomLogoutSuccessHandler())
                        .logoutSuccessUrl("/")
                        .deleteCookies("JSESSIONID")
                        .and()
                        .oauth2Login()
                        .loginPage(DEFAULT_AUTHORIZATION_REQUEST_BASE_URI + "/" + realm);

【问题讨论】:

    标签: spring cookies spring-security session-cookies


    【解决方案1】:

    你可以试试这样的

    .logout().permitAll()
             .deleteCookies("JSESSIONID")
             .invalidateHttpSession(true)
             .clearAuthentication(true)
             .logoutUrl("/logout")
             .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
    

    甚至添加.logoutSuccessHandler(new CustomLogoutSuccessHandler())

    可能看起来像这样

    public class CustomLogoutSuccessHandler implements LogoutSuccessHandler{
        @Override
        public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                                    Authentication authentication) throws IOException, ServletException {
            if (authentication != null && authentication.getDetails() != null) {
                try {
                    httpServletRequest.getSession().invalidate();
                    System.out.println("User Successfully Logout");
                    //you can add more codes here when the user successfully logs out,
                    //such as updating the database for last active.
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
     
            httpServletResponse.setStatus(HttpServletResponse.SC_OK);
        }
    }
           
    

    【讨论】:

    • 我认为它在 Postman 中不起作用 出现一些看起来像 html 页面的奇怪错误,我仍然登录到我的应用程序
    • 不确定您的所有配置,但您可能需要在安全配置类中添加一些注释,如 @EnableSpringHttpSession 或 @EnableJdbcHttpSession 并添加`@Bean public HttpSessionIdResolver httpSessionIdResolver() { return new HeaderHttpSessionIdResolver(" X-Auth-Token"); }`
    • 实际上,当我添加这个成功处理程序时,问题是我的身份验证为空。所以 try 块中的代码永远不会被调用。即使我尝试你做 authentication = SecurityContextHolder.getContext().getAuthentication();当我从 Postman 打来电话时,它仍然为空,同时在我的浏览器中登录到应用程序
    • 也许发布您的安全配置类,以便有人会看到问题可能出在哪里
    • 我已经发布了我的整个配置(HttpSecurity http)方法
    猜你喜欢
    • 2023-03-11
    • 2021-08-15
    • 2020-03-17
    • 1970-01-01
    • 2015-05-20
    • 1970-01-01
    • 2010-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多