Search in sources :

Example 56 with Session

use of org.apache.shiro.session.Session in project neo4j by neo4j.

the class ShiroSubjectFactory method createSubject.

@Override
public Subject createSubject(SubjectContext context) {
    SecurityManager securityManager = context.resolveSecurityManager();
    Session session = context.resolveSession();
    boolean sessionCreationEnabled = context.isSessionCreationEnabled();
    PrincipalCollection principals = context.resolvePrincipals();
    boolean authenticated = context.resolveAuthenticated();
    String host = context.resolveHost();
    ShiroAuthenticationInfo authcInfo = (ShiroAuthenticationInfo) context.getAuthenticationInfo();
    return new ShiroSubject(principals, authenticated, host, session, sessionCreationEnabled, securityManager, authcInfo.getAuthenticationResult());
}
Also used : SecurityManager(org.apache.shiro.mgt.SecurityManager) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) Session(org.apache.shiro.session.Session)

Example 57 with Session

use of org.apache.shiro.session.Session in project tesla by linking12.

the class SessionService method list.

public List<UserOnline> list() {
    List<UserOnline> list = new ArrayList<>();
    Collection<Session> sessions = sessionDAO.getActiveSessions();
    for (Session session : sessions) {
        UserOnline userOnline = new UserOnline();
        if (session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY) == null) {
            continue;
        } else {
            SimplePrincipalCollection principalCollection = (SimplePrincipalCollection) session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
            String userName = principalCollection.getRealmNames().iterator().next();
            userOnline.setUsername(userName);
        }
        userOnline.setId((String) session.getId());
        userOnline.setHost(session.getHost());
        userOnline.setStartTimestamp(session.getStartTimestamp());
        userOnline.setLastAccessTime(session.getLastAccessTime());
        userOnline.setTimeout(session.getTimeout());
        list.add(userOnline);
    }
    return list;
}
Also used : UserOnline(io.github.tesla.authz.domain.UserOnline) ArrayList(java.util.ArrayList) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) Session(org.apache.shiro.session.Session)

Example 58 with Session

use of org.apache.shiro.session.Session in project moon by gentoo111.

the class UserUtil method getSession.

public static Session getSession() {
    Subject currentUser = SecurityUtils.getSubject();
    Session session = currentUser.getSession();
    return session;
}
Also used : Subject(org.apache.shiro.subject.Subject) Session(org.apache.shiro.session.Session)

Example 59 with Session

use of org.apache.shiro.session.Session in project fruit-manage by liuzhaozhao.

the class LoginController method auth.

/**
 * 登录操作
 */
public void auth() {
    Object uid = getSessionAttr(Constant.SESSION_UID);
    if (uid != null) {
        renderJson(new DataResult<>(DataResult.CODE_SUCCESS, "登录成功"));
    }
    String userName = getPara("username");
    String password = StringUtils.isNotBlank(getPara("password")) ? HashKit.md5(getPara("password")) : getPara("password");
    Subject subject = SecurityUtils.getSubject();
    UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
    try {
        subject.login(token);
        Session session = subject.getSession();
        session.setAttribute(Constant.SESSION_UID, User.dao.getUser(userName).getId());
        renderNull();
    } catch (Exception e) {
        if (StringUtils.isAllBlank(userName, password)) {
            renderLogin("身份认证失败");
        } else {
            renderErrorText("用户名或密码错误");
        }
    }
}
Also used : Subject(org.apache.shiro.subject.Subject) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken) Session(org.apache.shiro.session.Session)

Example 60 with Session

use of org.apache.shiro.session.Session in project spring-boot-starter-samples by vindell.

the class AuthzLoginController method switchRole.

@ApiOperation(value = "switchRole", notes = "切换角色")
@ApiImplicitParams({ @ApiImplicitParam(name = "roleid", value = "角色ID", dataType = "String") })
// @BusinessLog(module = Constants.Module.LOGIN, business = "切换角色", opt = BusinessType.LOGIN)
@RequestMapping(value = "switchRole", method = { RequestMethod.POST, RequestMethod.GET })
public String switchRole(String roleid) {
    try {
        AuthzLoginModel principal = SubjectUtils.getPrincipal(AuthzLoginModel.class);
        Session session = SubjectUtils.getSession();
        if (StringUtils.isNotBlank(roleid) && (!StringUtils.equals(roleid, principal.getRoleid()))) {
            /*// 切换当前的角色信息
				getUser().setJsdm(jsdm);

				// 刷新shiro缓存
				AccountRealm shiroRealm = ServiceFactory.getService(DefaultAccountRealm.class);
				shiroRealm.clearAuthorizationCache();*/
            // 刷新shiro缓存
            // 删除用户数据范围标识
            session.removeAttribute("");
        }
    } catch (Exception e) {
        logException(this, e);
    }
    return "redirect:/index";
}
Also used : AuthzLoginModel(net.jeebiz.boot.demo.dao.entities.AuthzLoginModel) NoneRoleException(org.apache.shiro.biz.authc.exception.NoneRoleException) DisabledAccountException(org.apache.shiro.authc.DisabledAccountException) IncorrectCaptchaException(org.apache.shiro.biz.authc.exception.IncorrectCaptchaException) InvalidCaptchaException(org.apache.shiro.biz.authc.exception.InvalidCaptchaException) InvalidAccountException(org.apache.shiro.biz.authc.exception.InvalidAccountException) UnknownAccountException(org.apache.shiro.authc.UnknownAccountException) NoneCaptchaException(org.apache.shiro.biz.authc.exception.NoneCaptchaException) Session(org.apache.shiro.session.Session) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Session (org.apache.shiro.session.Session)93 Subject (org.apache.shiro.subject.Subject)34 Test (org.junit.Test)21 Serializable (java.io.Serializable)11 PrincipalCollection (org.apache.shiro.subject.PrincipalCollection)8 UsernamePasswordToken (org.apache.shiro.authc.UsernamePasswordToken)6 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 SecurityManager (org.apache.shiro.mgt.SecurityManager)5 SessionListener (org.apache.shiro.session.SessionListener)5 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)5 User (com.hfut.entity.User)4 Subject (ddf.security.Subject)4 ApiOperation (io.swagger.annotations.ApiOperation)4 Date (java.util.Date)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 AuthenticationException (org.apache.shiro.authc.AuthenticationException)4 InvalidSessionException (org.apache.shiro.session.InvalidSessionException)4 SessionListenerAdapter (org.apache.shiro.session.SessionListenerAdapter)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3