java - Spring 应用程序中的 Okta 自动注销

标签 java spring session okta

如何在 Spring 应用程序中配置 okta 自动注销,从而破坏用户 session 并在 okta 一侧注销?

最佳答案

结果如下:

1)在 Spring 应用程序中注册 logoutHandler 并设置 session 超时,如此处回答: How to log out automatically with Spring Security

2) 您需要从 Spring SimpleUrlLogoutSuccessHandler 扩展并放置所有 SAML 逻辑,如下面的代码:

public class SamlAutomaticLogout extends SimpleUrlLogoutSuccessHandler {
/**
 * Name of parameter of HttpRequest indicating whether this call should perform only local logout.
 * In case the value is true no global logout will be invoked.
 */
private static final String LOGOUT_PARAMETER = "local";

@Autowired
private SingleLogoutProfile profile;

@Autowired
private SAMLContextProvider contextProvider;

@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response,
                            Authentication authentication) throws IOException, ServletException {
    try {
        if (authentication != null && isGlobalLogout(request, authentication)) {

            Assert.isInstanceOf(SAMLCredential.class, authentication.getCredentials(),
                "Authentication object doesn't contain SAML credential, cannot perform global logout");

            // Terminate the session first
            HttpSession session = request.getSession(false);
            SecurityContextHolder.clearContext();
            if (session != null) {
                session.invalidate();
            }

            // Notify session participants using SAML Single Logout profile
            SAMLCredential credential = (SAMLCredential) authentication.getCredentials();
            request.setAttribute(SAMLConstants.LOCAL_ENTITY_ID, credential.getLocalEntityID());
            request.setAttribute(SAMLConstants.PEER_ENTITY_ID, credential.getRemoteEntityID());
            SAMLMessageContext context = contextProvider.getLocalAndPeerEntity(request, response);
            profile.sendLogoutRequest(context, credential);
        }
    } catch (SAMLException e) {
        logger.debug("Error initializing global logout", e);
        throw new ServletException("Error initializing global logout", e);
    } catch (MetadataProviderException e) {
        logger.debug("Error processing metadata", e);
        throw new ServletException("Error processing metadata", e);
    } catch (MessageEncodingException e) {
        logger.debug("Error encoding outgoing message", e);
        throw new ServletException("Error encoding outgoing message", e);
    }

    super.onLogoutSuccess(request, response, authentication);
}

private boolean isGlobalLogout(HttpServletRequest request, Authentication auth) {
    String localLogout = request.getParameter(LOGOUT_PARAMETER);
    return (localLogout == null || !"true".equals(localLogout.toLowerCase().trim()))
        && (auth.getCredentials() instanceof SAMLCredential);
}

}

关于java - Spring 应用程序中的 Okta 自动注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52700650/

相关文章:

javascript - 我该如何修复 : InvalidOperationException upon Session timeout in Ajax WebService call

java - java管理系统,无文件和数据库

java - 如何一对一匹配元素?

java - 将预先构造的 Bean 添加到 Spring 应用程序上下文

java - 我如何在java中对这个方法进行单元测试?

session - 如何在 Spring MVC 中的 Controller 之间共享 SessionAttributes?

java - 如何使用jdbc和tomcat连接MySQL数据库

java - 如何使用opencv java识别黑色

Spring:在自定义 ViewResolver 中访问 HttpServletRequest

php - 我的 MySQL 查询、while 循环和 SESSIONS 无法正常工作 PHP