Example usage for org.apache.shiro.session.mgt SimpleSession SimpleSession

List of usage examples for org.apache.shiro.session.mgt SimpleSession SimpleSession

Introduction

In this page you can find the example usage for org.apache.shiro.session.mgt SimpleSession SimpleSession.

Prototype

public SimpleSession() 

Source Link

Usage

From source file:com.app.test.controller.UserControllerTest.java

License:Open Source License

@Test
public void testGetLogInWithUnauthenticatedUser() throws Exception {
    PowerMockito.spy(SecurityUtils.class);

    Session session = new SimpleSession();

    session.setAttribute("loginAttempts", 0);

    Subject mockSubject = Mockito.mock(Subject.class);

    PowerMockito.doReturn(mockSubject).when(SecurityUtils.class, "getSubject");

    PowerMockito.doReturn(session).when(mockSubject).getSession();

    this.mockMvc.perform(get("/log_in")).andExpect(status().isOk()).andExpect(view().name("log_in"))
            .andExpect(forwardedUrl("/WEB-INF/jsp/log_in.jsp"));
}

From source file:com.app.test.controller.UserControllerTest.java

License:Open Source License

@Test
public void testGetLogInExceedingLoginAttemptLimit() throws Exception {
    PowerMockito.spy(SecurityUtils.class);

    Session session = new SimpleSession();

    session.setAttribute("loginAttempts", PropertiesValues.LOGIN_ATTEMPT_LIMIT + 1);

    Subject mockSubject = Mockito.mock(Subject.class);

    PowerMockito.doReturn(mockSubject).when(SecurityUtils.class, "getSubject");

    PowerMockito.doReturn(session).when(mockSubject).getSession();

    this.mockMvc.perform(get("/log_in")).andExpect(status().isOk()).andExpect(view().name("log_in"))
            .andExpect(forwardedUrl("/WEB-INF/jsp/log_in.jsp"))
            .andExpect(model().attribute("recaptchaSiteKey", PropertiesValues.RECAPTCHA_SITE_KEY));
}

From source file:com.app.test.controller.UserControllerTest.java

License:Open Source License

@Test
public void testPostLogInWithUnauthenticatedUser() throws Exception {
    PowerMockito.spy(SecurityUtils.class);

    Session session = new SimpleSession();

    Subject mockSubject = Mockito.mock(Subject.class);

    PowerMockito.doReturn(mockSubject).when(SecurityUtils.class, "getSubject");

    PowerMockito.doReturn(session).when(mockSubject).getSession();

    Mockito.doNothing().when(mockSubject).login(Mockito.any(AuthenticationToken.class));

    MockHttpServletRequestBuilder request = post("/log_in");

    request.param("emailAddress", "test@test.com");
    request.param("password", "password");

    this.mockMvc.perform(request).andExpect(status().is3xxRedirection()).andExpect(view().name("redirect:home"))
            .andExpect(redirectedUrl("home"));
}

From source file:com.app.test.controller.UserControllerTest.java

License:Open Source License

@Test
public void testPostLogInWithUnknownAccountException() throws Exception {
    PowerMockito.spy(SecurityUtils.class);

    Session session = new SimpleSession();

    Subject mockSubject = Mockito.mock(Subject.class);

    PowerMockito.doReturn(mockSubject).when(SecurityUtils.class, "getSubject");

    PowerMockito.doReturn(session).when(mockSubject).getSession();

    Mockito.doThrow(new UnknownAccountException()).when(mockSubject)
            .login(Mockito.any(AuthenticationToken.class));

    MockHttpServletRequestBuilder request = post("/log_in");

    request.param("emailAddress", "test@test.com");
    request.param("password", "password");

    this.mockMvc.perform(request).andExpect(status().is3xxRedirection())
            .andExpect(view().name("redirect:log_in")).andExpect(redirectedUrl("log_in"))
            .andExpect(flash().attributeExists("error"))
            .andExpect(flash().attribute("error", LanguageUtil.getMessage("log-in-failure")));
}

From source file:com.app.test.controller.UserControllerTest.java

License:Open Source License

@Test
public void testPostLogInWithIncorrectCredentialsException() throws Exception {

    PowerMockito.spy(SecurityUtils.class);

    Session session = new SimpleSession();

    Subject mockSubject = Mockito.mock(Subject.class);

    PowerMockito.doReturn(mockSubject).when(SecurityUtils.class, "getSubject");

    PowerMockito.doReturn(session).when(mockSubject).getSession();

    Mockito.doThrow(new IncorrectCredentialsException()).when(mockSubject)
            .login(Mockito.any(AuthenticationToken.class));

    MockHttpServletRequestBuilder request = post("/log_in");

    request.param("emailAddress", "test@test.com");
    request.param("password", "password");

    this.mockMvc.perform(request).andExpect(status().is3xxRedirection())
            .andExpect(view().name("redirect:log_in")).andExpect(redirectedUrl("log_in"))
            .andExpect(flash().attributeExists("error"))
            .andExpect(flash().attribute("error", LanguageUtil.getMessage("log-in-failure")));
}

From source file:com.app.test.controller.UserControllerTest.java

License:Open Source License

@Test
public void testPostLogInExceedingLoginLimitAndFailingRecaptcha() throws Exception {

    PowerMockito.spy(SecurityUtils.class);

    Session session = new SimpleSession();

    session.setAttribute("loginAttempts", PropertiesValues.LOGIN_ATTEMPT_LIMIT + 1);

    Subject mockSubject = Mockito.mock(Subject.class);

    PowerMockito.doReturn(mockSubject).when(SecurityUtils.class, "getSubject");

    PowerMockito.doReturn(session).when(mockSubject).getSession();

    Mockito.doNothing().when(mockSubject).login(Mockito.any(AuthenticationToken.class));

    MockHttpServletRequestBuilder request = post("/log_in");

    request.param("emailAddress", "test@test.com");
    request.param("password", "password");

    this.mockMvc.perform(request).andExpect(status().is3xxRedirection())
            .andExpect(view().name("redirect:log_in")).andExpect(redirectedUrl("log_in"))
            .andExpect(flash().attributeExists("error"))
            .andExpect(flash().attribute("error", LanguageUtil.getMessage("recaptcha-failure")));
}

From source file:com.app.test.controller.UserControllerTest.java

License:Open Source License

@Test
public void testPostLogInExceedingLoginLimitAndPassingRecaptcha() throws Exception {

    setUpRecaptchaUtil(true);//from w w  w  .  ja v  a 2s  .com

    PowerMockito.spy(SecurityUtils.class);

    Session session = new SimpleSession();

    session.setAttribute("loginAttempts", PropertiesValues.LOGIN_ATTEMPT_LIMIT + 1);

    Subject mockSubject = Mockito.mock(Subject.class);

    PowerMockito.doReturn(mockSubject).when(SecurityUtils.class, "getSubject");

    PowerMockito.doReturn(session).when(mockSubject).getSession();

    Mockito.doNothing().when(mockSubject).login(Mockito.any(AuthenticationToken.class));

    MockHttpServletRequestBuilder request = post("/log_in");

    request.param("emailAddress", "test@test.com");
    request.param("password", "password");

    this.mockMvc.perform(request).andExpect(status().is3xxRedirection()).andExpect(view().name("redirect:home"))
            .andExpect(redirectedUrl("home"));
}

From source file:com.aquenos.scm.ssh.server.ScmPasswordAuthenticator.java

License:Open Source License

@Override
public boolean authenticate(String username, String password, ServerSession session) {
    if (username == null || password == null) {
        return false;
    }/*ww w  .j a  v  a2s  . c o m*/
    SimpleSession shiroSession = new SimpleSession();
    shiroSession.setTimeout(-1L);
    Subject subject = new Subject.Builder(securityManager).session(shiroSession)
            .host(session.getIoSession().getRemoteAddress().toString()).buildSubject();
    try {
        subject.login(new UsernamePasswordToken(username, password));
    } catch (AuthenticationException e) {
        return false;
    }
    // Store subject in session.
    session.setAttribute(ScmSshServer.SUBJECT_SESSION_ATTRIBUTE_KEY, subject);
    return true;
}

From source file:com.aquenos.scm.ssh.server.ScmPublickeyAuthenticator.java

License:Open Source License

@Override
public boolean authenticate(String username, PublicKey publicKey, ServerSession session) {
    if (username == null || publicKey == null) {
        return false;
    }/* ww w.ja  v a2s.  co m*/
    SimpleSession shiroSession = new SimpleSession();
    shiroSession.setTimeout(-1L);
    Subject subject = new Subject.Builder(securityManager).session(shiroSession)
            .host(session.getIoSession().getRemoteAddress().toString()).buildSubject();
    try {
        subject.login(new PublicKeyToken(username, publicKey));
    } catch (AuthenticationException e) {
        return false;
    }
    // Store subject in session.
    session.setAttribute(ScmSshServer.SUBJECT_SESSION_ATTRIBUTE_KEY, subject);
    return true;
}

From source file:com.fengduo.spark.commons.shiro.session.SessionManager.java

License:Open Source License

@Override
public Session start(SessionContext context) {
    try {/*w  ww.j av  a  2 s  .  com*/
        return super.start(context);
    } catch (NullPointerException e) {
        SimpleSession session = new SimpleSession();
        session.setId(0);
        return session;
    }
}