Spring 是否可以远程删除应用程序前端的Cookie?
我正在根据其他应用程序提供的API调用从我的应用程序远程注销。 通常,当用户使用我的应用程序注销时,我使用Spring.logout().deleteCookies() 现在,我们对许多应用程序使用一个授权服务器,我们需要确保从一个服务器注销也会注销其他应用程序 所以基本的想法是授权服务器将调用我的API,比如说/user/logout 当我接到这个电话时,我想要和spring.logout().deleteCookies()一样的东西。我怎样才能做到这一点 是否可以将新的API添加到Spring配置中?说更改此配置:Spring 是否可以远程删除应用程序前端的Cookie?,spring,cookies,spring-security,session-cookies,Spring,Cookies,Spring Security,Session Cookies,我正在根据其他应用程序提供的API调用从我的应用程序远程注销。 通常,当用户使用我的应用程序注销时,我使用Spring.logout().deleteCookies() 现在,我们对许多应用程序使用一个授权服务器,我们需要确保从一个服务器注销也会注销其他应用程序 所以基本的想法是授权服务器将调用我的API,比如说/user/logout 当我接到这个电话时,我想要和spring.logout().deleteCookies()一样的东西。我怎样才能做到这一点 是否可以将新的API添加到Sprin
.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);
你可以试试这样的
.logout().permitAll()
.deleteCookies("JSESSIONID")
.invalidateHttpSession(true)
.clearAuthentication(true)
.logoutUrl("/logout")
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
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);
}
}
甚至添加.logoutSuccessHandler(新的CustomLogoutSuccessHandler())
可能看起来像这样
.logout().permitAll()
.deleteCookies("JSESSIONID")
.invalidateHttpSession(true)
.clearAuthentication(true)
.logoutUrl("/logout")
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
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 HttpSessiondResolver HttpSessiondResolver(){return new HeaderHttpSessionIdResolver(“X-Auth-Token”);}`实际上,当我现在添加这个成功处理程序时,问题是我的auth为null。所以try块中的代码永远不会被调用。即使我尝试了yo do authentication=SecurityContextHolder.getContext().getAuthentication();当我从邮递员那里打电话时,它仍然是空的,在我的浏览器中同时登录到应用程序,也许是为了发布你的安全配置类,以便有人看到问题可能出在哪里;我发布了我的整个配置(HttpSecurity-http)方法