com.ruoyi.common.constant.ShiroConstants Java Examples

The following examples show how to use com.ruoyi.common.constant.ShiroConstants. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OnlineWebSessionManager.java    From supplierShop with MIT License 6 votes vote down vote up
private boolean needMarkAttributeChanged(Object attributeKey)
{
    if (attributeKey == null)
    {
        return false;
    }
    String attributeKeyStr = attributeKey.toString();
    // 优化 flash属性没必要持久化
    if (attributeKeyStr.startsWith("org.springframework"))
    {
        return false;
    }
    if (attributeKeyStr.startsWith("javax.servlet"))
    {
        return false;
    }
    if (attributeKeyStr.equals(ShiroConstants.CURRENT_USERNAME))
    {
        return false;
    }
    return true;
}
 
Example #2
Source File: OnlineWebSessionManager.java    From ruoyiplus with MIT License 6 votes vote down vote up
private boolean needMarkAttributeChanged(Object attributeKey)
{
    if (attributeKey == null)
    {
        return false;
    }
    String attributeKeyStr = attributeKey.toString();
    // 优化 flash属性没必要持久化
    if (attributeKeyStr.startsWith("org.springframework"))
    {
        return false;
    }
    if (attributeKeyStr.startsWith("javax.servlet"))
    {
        return false;
    }
    if (attributeKeyStr.equals(ShiroConstants.CURRENT_USERNAME))
    {
        return false;
    }
    return true;
}
 
Example #3
Source File: OnlineSessionFilter.java    From RuoYi with Apache License 2.0 5 votes vote down vote up
/**
 * 表示是否允许访问;mappedValue就是[urls]配置中拦截器参数部分,如果允许访问返回true,否则false;
 */
@Override
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue){
    Subject subject = getSubject(request, response);
    if (subject == null || subject.getSession() == null) {
        return true;
    }
    Session session = onlineSessionDAO.readSession(subject.getSession().getId());
    if (session instanceof OnlineSession) {
        OnlineSession onlineSession = (OnlineSession) session;
        request.setAttribute(ShiroConstants.ONLINE_SESSION, onlineSession);
        // 把user对象设置进去
        boolean isGuest = onlineSession.getUserId() == null || onlineSession.getUserId() == 0L;
        if (isGuest) {
            SysUser user = ShiroUtils.getSysUser();
            if (user != null) {
                onlineSession.setUserId(user.getUserId());
                onlineSession.setLoginName(user.getLoginName());
                onlineSession.setAvatar(user.getAvatar());
                onlineSession.setDeptName(user.getDept().getDeptName());
                onlineSession.markAttributeChanged();
            }
        }

        return onlineSession.getStatus() != OnlineStatus.OFF_LINE;
    }
    return true;
}
 
Example #4
Source File: OnlineWebSessionManager.java    From RuoYi with Apache License 2.0 5 votes vote down vote up
private boolean needMarkAttributeChanged(Object attributeKey) {
    if (attributeKey == null) {
        return false;
    }
    String attributeKeyStr = attributeKey.toString();
    // 优化 flash属性没必要持久化
    if (attributeKeyStr.startsWith("org.springframework")) {
        return false;
    }
    if (attributeKeyStr.startsWith("javax.servlet")) {
        return false;
    }
    return !attributeKeyStr.equals(ShiroConstants.CURRENT_USERNAME);
}
 
Example #5
Source File: CaptchaValidateFilter.java    From RuoYi with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue){
    HttpServletRequest httpServletRequest = (HttpServletRequest) request;
    // 验证码禁用 或不是表单提交 允许访问
    if (!captchaEnabled || !"post".equalsIgnoreCase(httpServletRequest.getMethod().toLowerCase())) {
        return true;
    }
    return validateResponse(httpServletRequest.getParameter(ShiroConstants.CURRENT_VALIDATECODE));
}
 
Example #6
Source File: SyncOnlineSessionFilter.java    From ruoyiplus with MIT License 5 votes vote down vote up
/**
 * 同步会话数据到DB 一次请求最多同步一次 防止过多处理 需要放到Shiro过滤器之前
 */
@Override
protected boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception
{
    OnlineSession session = (OnlineSession) request.getAttribute(ShiroConstants.ONLINE_SESSION);
    // 如果session stop了 也不同步
    // session停止时间,如果stopTimestamp不为null,则代表已停止
    if (session != null && session.getUserId() != null && session.getStopTimestamp() == null)
    {
        onlineSessionDAO.syncToDb(session);
    }
    return true;
}
 
Example #7
Source File: CaptchaValidateFilter.java    From ruoyiplus with MIT License 5 votes vote down vote up
@Override
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue)
        throws Exception
{
    HttpServletRequest httpServletRequest = (HttpServletRequest) request;
    // 验证码禁用 或不是表单提交 允许访问
    if (captchaEnabled == false || !"post".equals(httpServletRequest.getMethod().toLowerCase()))
    {
        return true;
    }
    return validateResponse(httpServletRequest, httpServletRequest.getParameter(ShiroConstants.CURRENT_VALIDATECODE));
}
 
Example #8
Source File: CaptchaValidateFilter.java    From ruoyiplus with MIT License 5 votes vote down vote up
@Override
public boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception
{
    request.setAttribute(ShiroConstants.CURRENT_ENABLED, captchaEnabled);
    request.setAttribute(ShiroConstants.CURRENT_TYPE, captchaType);
    return super.onPreHandle(request, response, mappedValue);
}
 
Example #9
Source File: OnlineSessionFilter.java    From ruoyiplus with MIT License 5 votes vote down vote up
/**
 * 表示是否允许访问;mappedValue就是[urls]配置中拦截器参数部分,如果允许访问返回true,否则false;
 */
@Override
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue)
        throws Exception
{
    Subject subject = getSubject(request, response);
    if (subject == null || subject.getSession() == null)
    {
        return true;
    }
    Session session = onlineSessionDAO.readSession(subject.getSession().getId());
    if (session != null && session instanceof OnlineSession)
    {
        OnlineSession onlineSession = (OnlineSession) session;
        request.setAttribute(ShiroConstants.ONLINE_SESSION, onlineSession);
        // 把user对象设置进去
        boolean isGuest = onlineSession.getUserId() == null || onlineSession.getUserId() == 0L;
        if (isGuest == true)
        {
            SysUser user = ShiroUtils.getSysUser();
            if (user != null)
            {
                onlineSession.setUserId(user.getUserId());
                onlineSession.setLoginName(user.getLoginName());
                onlineSession.setDeptName(user.getDept().getDeptName());
                onlineSession.markAttributeChanged();
            }
        }

        if (onlineSession.getStatus() == OnlineStatus.off_line)
        {
            return false;
        }
    }
    return true;
}
 
Example #10
Source File: SyncOnlineSessionFilter.java    From supplierShop with MIT License 5 votes vote down vote up
/**
 * 同步会话数据到DB 一次请求最多同步一次 防止过多处理 需要放到Shiro过滤器之前
 */
@Override
protected boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception
{
    OnlineSession session = (OnlineSession) request.getAttribute(ShiroConstants.ONLINE_SESSION);
    // 如果session stop了 也不同步
    // session停止时间,如果stopTimestamp不为null,则代表已停止
    if (session != null && session.getUserId() != null && session.getStopTimestamp() == null)
    {
        onlineSessionDAO.syncToDb(session);
    }
    return true;
}
 
Example #11
Source File: SyncOnlineSessionFilter.java    From RuoYi with Apache License 2.0 5 votes vote down vote up
/**
 * 同步会话数据到DB 一次请求最多同步一次 防止过多处理 需要放到Shiro过滤器之前
 */
@Override
protected boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue){
    OnlineSession session = (OnlineSession) request.getAttribute(ShiroConstants.ONLINE_SESSION);
    // 如果session stop了 也不同步
    // session停止时间,如果stopTimestamp不为null,则代表已停止
    if (session != null && session.getUserId() != null && session.getStopTimestamp() == null) {
        onlineSessionDAO.syncToDb(session);
    }
    return true;
}
 
Example #12
Source File: CaptchaValidateFilter.java    From supplierShop with MIT License 5 votes vote down vote up
@Override
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue)
        throws Exception
{
    HttpServletRequest httpServletRequest = (HttpServletRequest) request;
    // 验证码禁用 或不是表单提交 允许访问
    if (captchaEnabled == false || !"post".equals(httpServletRequest.getMethod().toLowerCase()))
    {
        return true;
    }
    return validateResponse(httpServletRequest, httpServletRequest.getParameter(ShiroConstants.CURRENT_VALIDATECODE));
}
 
Example #13
Source File: CaptchaValidateFilter.java    From supplierShop with MIT License 5 votes vote down vote up
@Override
public boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception
{
    request.setAttribute(ShiroConstants.CURRENT_ENABLED, captchaEnabled);
    request.setAttribute(ShiroConstants.CURRENT_TYPE, captchaType);
    return super.onPreHandle(request, response, mappedValue);
}
 
Example #14
Source File: OnlineSessionFilter.java    From supplierShop with MIT License 5 votes vote down vote up
/**
 * 表示是否允许访问;mappedValue就是[urls]配置中拦截器参数部分,如果允许访问返回true,否则false;
 */
@Override
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue)
        throws Exception
{
    Subject subject = getSubject(request, response);
    if (subject == null || subject.getSession() == null)
    {
        return true;
    }
    Session session = onlineSessionDAO.readSession(subject.getSession().getId());
    if (session != null && session instanceof OnlineSession)
    {
        OnlineSession onlineSession = (OnlineSession) session;
        request.setAttribute(ShiroConstants.ONLINE_SESSION, onlineSession);
        // 把user对象设置进去
        boolean isGuest = onlineSession.getUserId() == null || onlineSession.getUserId() == 0L;
        if (isGuest == true)
        {
            SysUser user = ShiroUtils.getSysUser();
            if (user != null)
            {
                onlineSession.setUserId(user.getUserId());
                onlineSession.setLoginName(user.getLoginName());
	onlineSession.setAvatar(user.getAvatar());
                onlineSession.setDeptName(user.getDept().getDeptName());
                onlineSession.markAttributeChanged();
            }
        }

        if (onlineSession.getStatus() == OnlineStatus.off_line)
        {
            return false;
        }
    }
    return true;
}
 
Example #15
Source File: CaptchaValidateFilter.java    From RuoYi with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) {
    request.setAttribute(ShiroConstants.CURRENT_CAPTCHA, ShiroConstants.CAPTCHA_ERROR);
    return true;
}
 
Example #16
Source File: SysLoginService.java    From RuoYi with Apache License 2.0 4 votes vote down vote up
/**
 * 登录
 */
public SysUser login(String username, String password) {
    // 验证码校验
    if (ObjectUtil.isNotEmpty(ServletUtils.getRequest().getAttribute(ShiroConstants.CURRENT_CAPTCHA))) {
        AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
        throw new CaptchaException();
    }
    // 用户名或密码为空 错误
    if (StrUtil.isEmpty(username) || StrUtil.isEmpty(password)) {
        AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("not.null")));
        throw new UserNotExistsException();
    }
    // 密码如果不在指定范围内 错误
    if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
            || password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
        AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
        throw new UserPasswordNotMatchException();
    }

    // 用户名不在指定范围内 错误
    if (username.length() < UserConstants.USERNAME_MIN_LENGTH
            || username.length() > UserConstants.USERNAME_MAX_LENGTH) {
        AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
        throw new UserPasswordNotMatchException();
    }

    // 查询用户信息
    SysUser user = userService.selectUserByLoginName(username);

    if (user == null && maybeMobilePhoneNumber(username)) {
        user = userService.selectUserByPhoneNumber(username);
    }

    if (user == null && maybeEmail(username)) {
        user = userService.selectUserByEmail(username);
    }

    if (user == null) {
        AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.not.exists")));
        throw new UserNotExistsException();
    }

    if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
        AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.delete")));
        throw new UserDeleteException();
    }

    if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
        AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.blocked" , user.getRemark())));
        throw new UserBlockedException();
    }

    passwordService.validate(user, password);

    AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
    recordLoginInfo(user);
    return user;
}
 
Example #17
Source File: SysPasswordService.java    From RuoYi with Apache License 2.0 4 votes vote down vote up
@PostConstruct
public void init() {
    loginRecordCache = cacheManager.getCache(ShiroConstants.LOGINRECORDCACHE);
}
 
Example #18
Source File: CaptchaValidateFilter.java    From RuoYi with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
    request.setAttribute(ShiroConstants.CURRENT_ENABLED, captchaEnabled);
    request.setAttribute(ShiroConstants.CURRENT_TYPE, captchaType);
    return super.onPreHandle(request, response, mappedValue);
}
 
Example #19
Source File: CaptchaValidateFilter.java    From ruoyiplus with MIT License 4 votes vote down vote up
@Override
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception
{
    request.setAttribute(ShiroConstants.CURRENT_CAPTCHA, ShiroConstants.CAPTCHA_ERROR);
    return true;
}
 
Example #20
Source File: KickoutSessionFilter.java    From RuoYi with Apache License 2.0 4 votes vote down vote up
/**
 * 设置Cache的key的前缀
 */
public void setCacheManager(CacheManager cacheManager) {
    // 必须和ehcache缓存配置中的缓存name一致
    this.cache = cacheManager.getCache(ShiroConstants.SYS_USERCACHE);
}
 
Example #21
Source File: LogoutFilter.java    From RuoYi with Apache License 2.0 4 votes vote down vote up
/**
 * 设置Cache的key的前缀
 * @param cacheManager 缓存管理器
 */
public void setCacheManager(CacheManager cacheManager) {
    // 必须和ehcache缓存配置中的缓存name一致
    this.cache = cacheManager.getCache(ShiroConstants.SYS_USERCACHE);
}
 
Example #22
Source File: SysLoginService.java    From ruoyiplus with MIT License 4 votes vote down vote up
/**
 * 登录
 */
public SysUser login(String username, String password)
{
    // 验证码校验
    if (!StringUtils.isEmpty(ServletUtils.getRequest().getAttribute(ShiroConstants.CURRENT_CAPTCHA)))
    {
        AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
        throw new CaptchaException();
    }
    // 用户名或密码为空 错误
    if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password))
    {
        AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("not.null")));
        throw new UserNotExistsException();
    }
    // 密码如果不在指定范围内 错误
    if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
            || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
    {
        AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
        throw new UserPasswordNotMatchException();
    }

    // 用户名不在指定范围内 错误
    if (username.length() < UserConstants.USERNAME_MIN_LENGTH
            || username.length() > UserConstants.USERNAME_MAX_LENGTH)
    {
        AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
        throw new UserPasswordNotMatchException();
    }

    // 查询用户信息
    SysUser user = userService.selectUserByLoginName(username);

    if (user == null && maybeMobilePhoneNumber(username))
    {
        user = userService.selectUserByPhoneNumber(username);
    }

    if (user == null && maybeEmail(username))
    {
        user = userService.selectUserByEmail(username);
    }

    if (user == null)
    {
        AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.not.exists")));
        throw new UserNotExistsException();
    }
    
    if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
    {
        AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.delete")));
        throw new UserDeleteException();
    }
    
    if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
    {
        AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.blocked", user.getRemark())));
        throw new UserBlockedException();
    }

    passwordService.validate(user, password);

    AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
    recordLoginInfo(user);
    return user;
}
 
Example #23
Source File: SysPasswordService.java    From supplierShop with MIT License 4 votes vote down vote up
@PostConstruct
public void init()
{
    loginRecordCache = cacheManager.getCache(ShiroConstants.LOGINRECORDCACHE);
}
 
Example #24
Source File: CaptchaValidateFilter.java    From supplierShop with MIT License 4 votes vote down vote up
@Override
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception
{
    request.setAttribute(ShiroConstants.CURRENT_CAPTCHA, ShiroConstants.CAPTCHA_ERROR);
    return true;
}
 
Example #25
Source File: KickoutSessionFilter.java    From supplierShop with MIT License 4 votes vote down vote up
public void setCacheManager(CacheManager cacheManager)
{
    // 必须和ehcache缓存配置中的缓存name一致
    this.cache = cacheManager.getCache(ShiroConstants.SYS_USERCACHE);
}
 
Example #26
Source File: LogoutFilter.java    From supplierShop with MIT License 4 votes vote down vote up
public void setCacheManager(CacheManager cacheManager)
{
    // 必须和ehcache缓存配置中的缓存name一致
    this.cache = cacheManager.getCache(ShiroConstants.SYS_USERCACHE);
}