Search in sources :

Example 1 with AgileBaseUser

use of com.jeeagile.core.security.user.AgileBaseUser in project jeeagile by jeeagile.

the class AgileLoggerAspect method getAgileLoggerOperate.

/**
 * 封装操作日志对象参数
 *
 * @param httpServletRequest
 * @param throwable
 * @return
 */
private AgileLoggerOperate getAgileLoggerOperate(HttpServletRequest httpServletRequest, Throwable throwable) {
    AgileLoggerOperate agileLoggerOperate = new AgileLoggerOperate();
    AgileBaseUser userData = AgileSecurityContext.getCurrentUser();
    if (userData != null) {
        agileLoggerOperate.setOperateUser(userData.getUserName());
        agileLoggerOperate.setCreateUser(userData.getUserId());
        agileLoggerOperate.setUpdateUser(userData.getUserId());
    }
    agileLoggerOperate.setRequestUri(httpServletRequest.getRequestURI());
    agileLoggerOperate.setRequestMethod(httpServletRequest.getMethod());
    agileLoggerOperate.setOperateIp(AgileAgentUtil.getUserClientIp(httpServletRequest));
    String serverAddress = AgileSystemUtil.getHostInfo().getAddress() + ":" + this.serverPort;
    agileLoggerOperate.setServerAddress(serverAddress);
    UserAgent userAgent = AgileAgentUtil.getUserAgent(httpServletRequest);
    agileLoggerOperate.setOperateOs(userAgent.getOperatingSystem().getName());
    agileLoggerOperate.setOperateDevice(userAgent.getOperatingSystem().getDeviceType().getName());
    agileLoggerOperate.setOperateBrowser(userAgent.getBrowser().getName());
    if (throwable != null) {
        agileLoggerOperate.setStatus(AgileCommonStatus.FAIL.getCode());
        String excMessage = throwable.getMessage();
        if (excMessage.length() > 200) {
            agileLoggerOperate.setMessage(excMessage.substring(200));
        } else {
            agileLoggerOperate.setMessage(excMessage);
        }
    } else {
        agileLoggerOperate.setStatus(AgileCommonStatus.SUCCESS.getCode());
    }
    return agileLoggerOperate;
}
Also used : AgileLoggerOperate(com.jeeagile.logger.entity.AgileLoggerOperate) UserAgent(eu.bitwalker.useragentutils.UserAgent) AgileBaseUser(com.jeeagile.core.security.user.AgileBaseUser)

Example 2 with AgileBaseUser

use of com.jeeagile.core.security.user.AgileBaseUser in project jeeagile by jeeagile.

the class AgileAuthorizingRealm method doGetAuthenticationInfo.

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) {
    if (agileUserDetailsService == null) {
        throw new AgileFrameException(AgileResultCode.FAIL_SERVER_EXCEPTION, "请设置用户验证接口实现类!");
    }
    // 获取用户名
    String loginName = (String) authenticationToken.getPrincipal();
    // 字符类型密码获取(用户输入的密码)
    char[] credentials = (char[]) authenticationToken.getCredentials();
    if (credentials == null || credentials.length < 1) {
        return null;
    }
    // 把字符数组转换为String类型(用户输入的密码)
    String password = new String(credentials);
    try {
        AgileBaseUser userData = agileUserDetailsService.getUserDataByLoginName(loginName);
        if (userData != null && AgileStringUtil.isNotEmpty(userData.getUserId())) {
            if (AgileSecurityUtil.encryptPassword(password).equals(userData.getPassword())) {
                userData.setUserToken(SecurityUtils.getSubject().getSession().getId().toString());
                userData.setUserPerm(agileUserDetailsService.getUserPerm(userData));
                userData.setUserRole(agileUserDetailsService.getUserRole(userData));
                HttpServletRequest httpServletRequest = AgileServletUtil.getHttpServletRequest();
                if (httpServletRequest != null) {
                    UserAgent userAgent = AgileAgentUtil.getUserAgent(httpServletRequest);
                    userData.setLoginIp(AgileAgentUtil.getUserClientIp(httpServletRequest));
                    userData.setLoginAddress(AgileNetUtil.getAddressByIp(userData.getLoginIp()));
                    userData.setOsName(userAgent.getOperatingSystem().getName());
                    userData.setDeviceName(userAgent.getOperatingSystem().getDeviceType().getName());
                    userData.setBrowserName(userAgent.getBrowser().getName());
                }
                return new SimpleAuthenticationInfo(userData, password, userData.getUserName());
            } else {
                throw new AgileAuthException(AgileResultCode.FAIL_USER_PWD);
            }
        } else {
            throw new AgileAuthException(AgileResultCode.FAIL_USER_NAME);
        }
    } catch (AgileBaseException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new AgileAuthException(AgileResultCode.FAIL_AUTH_EXCEPTION, ex);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) SimpleAuthenticationInfo(org.apache.shiro.authc.SimpleAuthenticationInfo) AgileBaseException(com.jeeagile.core.exception.AgileBaseException) UserAgent(eu.bitwalker.useragentutils.UserAgent) AgileFrameException(com.jeeagile.core.exception.AgileFrameException) AgileBaseUser(com.jeeagile.core.security.user.AgileBaseUser) AgileAuthException(com.jeeagile.core.exception.AgileAuthException) AgileAuthException(com.jeeagile.core.exception.AgileAuthException) AgileFrameException(com.jeeagile.core.exception.AgileFrameException) AgileBaseException(com.jeeagile.core.exception.AgileBaseException)

Example 3 with AgileBaseUser

use of com.jeeagile.core.security.user.AgileBaseUser in project jeeagile by jeeagile.

the class AgileShiroSecurity method getOnlineUserList.

@Override
public List<AgileOnlineUser> getOnlineUserList() {
    List<AgileOnlineUser> onlineUserList = new ArrayList<>();
    Collection<Session> sessionCollection = sessionDAO.getActiveSessions();
    for (Session session : sessionCollection) {
        Object pc = session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
        if (pc instanceof PrincipalCollection) {
            AgileBaseUser userData = (AgileBaseUser) ((PrincipalCollection) pc).getPrimaryPrincipal();
            if (userData == null || AgileStringUtil.isEmpty(userData.getUserName())) {
                continue;
            }
            AgileOnlineUser agileOnlineUser = new AgileOnlineUser();
            BeanUtils.copyProperties(userData, agileOnlineUser);
            agileOnlineUser.setStartAccessTime(session.getStartTimestamp());
            agileOnlineUser.setLastAccessTime(session.getLastAccessTime());
            onlineUserList.add(agileOnlineUser);
        }
    }
    return onlineUserList;
}
Also used : ArrayList(java.util.ArrayList) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) AgileOnlineUser(com.jeeagile.core.security.user.AgileOnlineUser) AgileBaseUser(com.jeeagile.core.security.user.AgileBaseUser) Session(org.apache.shiro.session.Session)

Example 4 with AgileBaseUser

use of com.jeeagile.core.security.user.AgileBaseUser in project jeeagile by jeeagile.

the class AgileUserDetailsServiceImpl method loadUserByUsername.

@Override
public UserDetails loadUserByUsername(String loginName) throws UsernameNotFoundException {
    try {
        if (agileUserDetailsService == null) {
            throw new AgileFrameException(AgileResultCode.FAIL_SERVER_EXCEPTION, "请设置用户验证接口实现类!");
        }
        AgileBaseUser userData = agileUserDetailsService.getUserDataByLoginName(loginName);
        if (userData != null && AgileStringUtil.isNotEmpty(userData.getUserId())) {
            userData.setUserToken(AgileStringUtil.getUuid());
            userData.setUserPerm(agileUserDetailsService.getUserPerm(userData));
            userData.setUserRole(agileUserDetailsService.getUserRole(userData));
            HttpServletRequest httpServletRequest = AgileServletUtil.getHttpServletRequest();
            if (httpServletRequest != null) {
                UserAgent userAgent = AgileAgentUtil.getUserAgent(httpServletRequest);
                userData.setLoginIp(AgileAgentUtil.getUserClientIp(httpServletRequest));
                userData.setOsName(userAgent.getOperatingSystem().getName());
                userData.setDeviceName(userAgent.getOperatingSystem().getDeviceType().getName());
                userData.setBrowserName(userAgent.getBrowser().getName());
            }
            List<SimpleGrantedAuthority> authorities = userData.getUserRole().stream().map(role -> new SimpleGrantedAuthority(role)).collect(Collectors.toList());
            AgileUserDetails agileUserDetails = new AgileUserDetails();
            agileUserDetails.setUserData(userData);
            agileUserDetails.setAuthorities(authorities);
            return agileUserDetails;
        } else {
            throw new AgileAuthException(AgileResultCode.FAIL_USER_NAME);
        }
    } catch (AgileBaseException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new AgileAuthException(AgileResultCode.FAIL_AUTH_EXCEPTION, ex);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) AgileStringUtil(com.jeeagile.core.util.AgileStringUtil) AgileReference(com.jeeagile.core.protocol.annotation.AgileReference) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) AgileResultCode(com.jeeagile.core.result.AgileResultCode) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) AgileServletUtil(com.jeeagile.core.util.spring.AgileServletUtil) AgileBaseUser(com.jeeagile.core.security.user.AgileBaseUser) Collectors(java.util.stream.Collectors) UserAgent(eu.bitwalker.useragentutils.UserAgent) HttpServletRequest(javax.servlet.http.HttpServletRequest) List(java.util.List) AgileAuthException(com.jeeagile.core.exception.AgileAuthException) AgileFrameException(com.jeeagile.core.exception.AgileFrameException) IAgileUserDetailsService(com.jeeagile.core.security.userdetails.IAgileUserDetailsService) UserDetails(org.springframework.security.core.userdetails.UserDetails) Lazy(org.springframework.context.annotation.Lazy) AgileBaseException(com.jeeagile.core.exception.AgileBaseException) AgileAgentUtil(com.jeeagile.core.util.AgileAgentUtil) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) AgileBaseException(com.jeeagile.core.exception.AgileBaseException) UserAgent(eu.bitwalker.useragentutils.UserAgent) AgileFrameException(com.jeeagile.core.exception.AgileFrameException) AgileBaseUser(com.jeeagile.core.security.user.AgileBaseUser) AgileAuthException(com.jeeagile.core.exception.AgileAuthException) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) AgileAuthException(com.jeeagile.core.exception.AgileAuthException) AgileFrameException(com.jeeagile.core.exception.AgileFrameException) AgileBaseException(com.jeeagile.core.exception.AgileBaseException)

Example 5 with AgileBaseUser

use of com.jeeagile.core.security.user.AgileBaseUser in project jeeagile by jeeagile.

the class AgilePermissionEvaluator method checkPermission.

private boolean checkPermission(Authentication authentication, Object permission) {
    if (AgileStringUtil.isEmpty(permission)) {
        return false;
    }
    AgileUserDetails agileUserDetails = (AgileUserDetails) authentication.getPrincipal();
    AgileBaseUser userData = null;
    if (agileUserDetails != null) {
        userData = agileUserDetails.getUserData();
    }
    if (AgileStringUtil.isEmpty(userData) || CollectionUtils.isEmpty(userData.getUserPerm())) {
        return false;
    }
    return userData.getUserPerm().contains("*:*:*") || userData.getUserPerm().contains(permission);
}
Also used : AgileUserDetails(com.jeeagile.springsecurity.userdetails.AgileUserDetails) AgileBaseUser(com.jeeagile.core.security.user.AgileBaseUser)

Aggregations

AgileBaseUser (com.jeeagile.core.security.user.AgileBaseUser)10 AgileAuthException (com.jeeagile.core.exception.AgileAuthException)3 AgileBaseException (com.jeeagile.core.exception.AgileBaseException)3 AgileFrameException (com.jeeagile.core.exception.AgileFrameException)3 AgileSysUser (com.jeeagile.system.entity.AgileSysUser)3 UserAgent (eu.bitwalker.useragentutils.UserAgent)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 AgileValidateException (com.jeeagile.core.exception.AgileValidateException)1 AgileReference (com.jeeagile.core.protocol.annotation.AgileReference)1 AgileResultCode (com.jeeagile.core.result.AgileResultCode)1 AgileOnlineUser (com.jeeagile.core.security.user.AgileOnlineUser)1 IAgileUserDetailsService (com.jeeagile.core.security.userdetails.IAgileUserDetailsService)1 AgileAgentUtil (com.jeeagile.core.util.AgileAgentUtil)1 AgileStringUtil (com.jeeagile.core.util.AgileStringUtil)1 AgileServletUtil (com.jeeagile.core.util.spring.AgileServletUtil)1 AgileLoggerOperate (com.jeeagile.logger.entity.AgileLoggerOperate)1 AgileUserDetails (com.jeeagile.springsecurity.userdetails.AgileUserDetails)1 AgilePersonInfo (com.jeeagile.system.vo.AgilePersonInfo)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1