Search in sources :

Example 1 with Connection

use of com.haulmont.cuba.portal.Connection in project cuba by cuba-platform.

the class PortalLogoutHandler method onLogoutSuccess.

@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
    Connection connection = (Connection) request.getSession().getAttribute(Connection.NAME);
    try {
        if (connection != null) {
            SecurityContext portalSecurityContext = new PortalSecurityContext(connection.getSession());
            AppContext.setSecurityContext(portalSecurityContext);
            PortalSession session = connection.getSession();
            if (session != null && session.isAuthenticated())
                connection.logout();
        }
    } catch (Exception e) {
        log.warn("Exception while logout", e);
    } finally {
        AppContext.setSecurityContext(null);
    }
    request.getSession().invalidate();
    super.onLogoutSuccess(request, response, authentication);
}
Also used : PortalSession(com.haulmont.cuba.portal.security.PortalSession) Connection(com.haulmont.cuba.portal.Connection) SecurityContext(com.haulmont.cuba.core.sys.SecurityContext) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 2 with Connection

use of com.haulmont.cuba.portal.Connection in project cuba by cuba-platform.

the class SecurityContextHandlerInterceptor method preHandle.

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    // filter resource requests
    if (ClassUtils.isAssignableValue(ResourceHttpRequestHandler.class, handler)) {
        return true;
    }
    PortalSecurityContext portalSecurityContext;
    HttpSession httpSession = request.getSession();
    Connection connection = (Connection) httpSession.getAttribute(Connection.NAME);
    if (connection == null || connection.getSession() == null || !connection.isConnected()) {
        connection = AppBeans.get(Connection.NAME);
        connection.login(request.getLocale(), request.getRemoteAddr(), request.getHeader("User-Agent"));
        httpSession.setAttribute(Connection.NAME, connection);
        portalSecurityContext = new PortalSecurityContext(connection.getSession());
        AppContext.setSecurityContext(portalSecurityContext);
    } else {
        PortalSession session = connection.getSession();
        portalSecurityContext = new PortalSecurityContext(session);
        AppContext.setSecurityContext(portalSecurityContext);
        // ping only authenticated sessions
        if (session != null && session.isAuthenticated()) {
            UserSessionService userSessionSource = AppBeans.get(UserSessionService.NAME);
            try {
                userSessionSource.getMessages();
            } catch (NoUserSessionException e) {
                httpSession.invalidate();
                response.sendRedirect(request.getRequestURI());
                return false;
            }
        }
    }
    App app = new App(connection, request, response);
    portalSecurityContext.setPortalApp(app);
    return true;
}
Also used : App(com.haulmont.cuba.portal.App) HttpSession(javax.servlet.http.HttpSession) PortalSession(com.haulmont.cuba.portal.security.PortalSession) UserSessionService(com.haulmont.cuba.security.app.UserSessionService) Connection(com.haulmont.cuba.portal.Connection) NoUserSessionException(com.haulmont.cuba.security.global.NoUserSessionException)

Example 3 with Connection

use of com.haulmont.cuba.portal.Connection in project cuba by cuba-platform.

the class PortalAuthenticationProvider method authenticate.

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    if (authentication instanceof UsernamePasswordAuthenticationToken) {
        UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) authentication;
        PortalSession session;
        String login = null;
        String ipAddress = null;
        try {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            login = (String) token.getPrincipal();
            ipAddress = request.getRemoteAddr();
            HttpSession httpSession = request.getSession();
            Connection connection = (Connection) httpSession.getAttribute(Connection.NAME);
            if (connection == null || connection.getSession() == null || !connection.isConnected()) {
                connection = AppBeans.get(Connection.NAME);
            }
            PasswordEncryption passwordEncryption = AppBeans.get(PasswordEncryption.NAME);
            connection.login(login, passwordEncryption.getPlainHash((String) token.getCredentials()), request.getLocale(), ipAddress, request.getHeader("User-Agent"));
            httpSession.setAttribute(Connection.NAME, connection);
            session = connection.getSession();
        } catch (AccountLockedException e) {
            log.info("Blocked user login attempt: login={}, ip={}", login, ipAddress);
            throw new LockedException(e.getMessage());
        } catch (UserIpRestrictedException e) {
            log.info("Incorrect user IP: {} {} - {}", login, ipAddress);
            throw new BadCredentialsException(e.getMessage());
        } catch (LoginException e) {
            log.info("Authentication failed: {} {} - {}", login, ipAddress, e.getMessage());
            throw new BadCredentialsException(e.getMessage());
        }
        return new UsernamePasswordAuthenticationToken(session, session.getId(), getRoleUserAuthorities(session));
    }
    return null;
}
Also used : AccountLockedException(com.haulmont.cuba.security.global.AccountLockedException) LockedException(org.springframework.security.authentication.LockedException) AccountLockedException(com.haulmont.cuba.security.global.AccountLockedException) HttpSession(javax.servlet.http.HttpSession) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) Connection(com.haulmont.cuba.portal.Connection) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) HttpServletRequest(javax.servlet.http.HttpServletRequest) PasswordEncryption(com.haulmont.cuba.core.global.PasswordEncryption) UserIpRestrictedException(com.haulmont.cuba.security.global.UserIpRestrictedException) LoginException(com.haulmont.cuba.security.global.LoginException)

Aggregations

Connection (com.haulmont.cuba.portal.Connection)3 PortalSession (com.haulmont.cuba.portal.security.PortalSession)2 HttpSession (javax.servlet.http.HttpSession)2 PasswordEncryption (com.haulmont.cuba.core.global.PasswordEncryption)1 SecurityContext (com.haulmont.cuba.core.sys.SecurityContext)1 App (com.haulmont.cuba.portal.App)1 UserSessionService (com.haulmont.cuba.security.app.UserSessionService)1 AccountLockedException (com.haulmont.cuba.security.global.AccountLockedException)1 LoginException (com.haulmont.cuba.security.global.LoginException)1 NoUserSessionException (com.haulmont.cuba.security.global.NoUserSessionException)1 UserIpRestrictedException (com.haulmont.cuba.security.global.UserIpRestrictedException)1 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)1 LockedException (org.springframework.security.authentication.LockedException)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)1