Search in sources :

Example 1 with AgileAuthException

use of com.jeeagile.core.exception.AgileAuthException in project jeeagile by jeeagile.

the class AgileUserDetailsServiceImpl method userLogin.

@Override
public AgileBaseUser userLogin(String loginName, String userPassword) {
    try {
        AgileSysUser agileSysUser = this.getAgileSysUser(loginName);
        if (agileSysUser == null) {
            throw new AgileAuthException("用户《" + loginName + "》不存在,请核实!");
        }
        this.checkAgileSysUser(agileSysUser);
        String md5Password = AgileSecurityUtil.encryptPassword(userPassword);
        if (!md5Password.equals(agileSysUser.getUserPwd())) {
            throw new AgileAuthException(AgileResultCode.FAIL_USER_PWD, "用户密码错误!");
        }
        return getAgileUserData(agileSysUser);
    } catch (AgileBaseException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new AgileAuthException("用户登录异常!");
    }
}
Also used : AgileBaseException(com.jeeagile.core.exception.AgileBaseException) AgileSysUser(com.jeeagile.system.entity.AgileSysUser) AgileAuthException(com.jeeagile.core.exception.AgileAuthException) AgileAuthException(com.jeeagile.core.exception.AgileAuthException) AgileBaseException(com.jeeagile.core.exception.AgileBaseException)

Example 2 with AgileAuthException

use of com.jeeagile.core.exception.AgileAuthException in project jeeagile by jeeagile.

the class AgileUserDetailsServiceImpl method getUserPerm.

@Override
public List<String> getUserPerm(AgileBaseUser agileBaseUser) {
    try {
        if (agileBaseUser != null) {
            if (agileBaseUser.isSuperAdmin()) {
                List<String> userPermList = new ArrayList<>();
                userPermList.add("*:*:*");
                return userPermList;
            } else {
                return agileUserDetailsMapper.getUserPermByUserId(agileBaseUser.getUserId());
            }
        } else {
            throw new AgileAuthException(AgileResultCode.FAIL_USER_INFO);
        }
    } catch (AgileBaseException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new AgileAuthException("加载用户权限信息异常!");
    }
}
Also used : AgileBaseException(com.jeeagile.core.exception.AgileBaseException) ArrayList(java.util.ArrayList) AgileAuthException(com.jeeagile.core.exception.AgileAuthException) AgileAuthException(com.jeeagile.core.exception.AgileAuthException) AgileBaseException(com.jeeagile.core.exception.AgileBaseException)

Example 3 with AgileAuthException

use of com.jeeagile.core.exception.AgileAuthException in project jeeagile by jeeagile.

the class AgileSecurityInterceptor method checkUserSecurity.

/**
 * 权限校验
 *
 * @param handlerMethod
 */
private void checkUserSecurity(HandlerMethod handlerMethod) {
    try {
        // 获取当前用户安全认证
        IAgileSecurity agileSecurity = AgileSecurityUtil.getAgileSecurity();
        if (agileSecurity == null) {
            throw new AgileAuthException("请设置用户安全接口类《UserSecurity》");
        }
        // 当前线程存放用户信息
        AgileSecurityContext.putCurrentUser(agileSecurity.getUserData());
        // 演示模式拦截
        AgileDemo agileDemo = handlerMethod.getMethodAnnotation(AgileDemo.class);
        if (agileDemo != null && AgileUtil.isDemoEnabled()) {
            throw new AgileDemoException();
        }
        // 如果为超管用户则不在进行权限校验
        if (agileSecurity.getUserData().isSuperAdmin()) {
            return;
        }
        AgileRequiresGuest agileRequiresGuest = handlerMethod.getBeanType().getAnnotation(AgileRequiresGuest.class);
        if (agileRequiresGuest != null) {
            return;
        }
        agileRequiresGuest = handlerMethod.getMethodAnnotation(AgileRequiresGuest.class);
        if (agileRequiresGuest != null) {
            return;
        }
        AgileRequiresAuthentication agileRequiresAuthentication = handlerMethod.getMethodAnnotation(AgileRequiresAuthentication.class);
        if (agileRequiresAuthentication != null && !agileSecurity.checkAuthenticated()) {
            throw new AgileAuthException("用户未验证通过!");
        }
        AgileRequiresUser agileRequiresUser = handlerMethod.getMethodAnnotation(AgileRequiresUser.class);
        if (agileRequiresUser != null) {
            agileSecurity.checkUser();
        }
        AgileRequiresRoles agileRequiresRoles = handlerMethod.getMethodAnnotation(AgileRequiresRoles.class);
        if (agileRequiresRoles != null) {
            agileSecurity.checkRole(agileRequiresRoles);
        }
        AgilePermissionsPrefix agilePermissionsPrefix = handlerMethod.getBeanType().getAnnotation(AgilePermissionsPrefix.class);
        AgileRequiresPermissions agileRequiresPermissions = handlerMethod.getMethodAnnotation(AgileRequiresPermissions.class);
        if (agileRequiresPermissions != null) {
            if (agilePermissionsPrefix != null) {
                agileSecurity.checkPermission(agilePermissionsPrefix, agileRequiresPermissions);
            } else {
                agileSecurity.checkPermission(agileRequiresPermissions);
            }
        }
    } catch (AgileBaseException ex) {
        throw ex;
    } catch (Exception ex) {
        logger.error("用户权限验证异常", ex);
        throw new AgileAuthException("用户权限验证异常!");
    }
}
Also used : IAgileSecurity(com.jeeagile.core.security.IAgileSecurity) AgileBaseException(com.jeeagile.core.exception.AgileBaseException) AgileDemoException(com.jeeagile.core.exception.AgileDemoException) AgileAuthException(com.jeeagile.core.exception.AgileAuthException) AgileDemo(com.jeeagile.frame.annotation.AgileDemo) AgileDemoException(com.jeeagile.core.exception.AgileDemoException) AgileAuthException(com.jeeagile.core.exception.AgileAuthException) AgileBaseException(com.jeeagile.core.exception.AgileBaseException)

Example 4 with AgileAuthException

use of com.jeeagile.core.exception.AgileAuthException 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 5 with AgileAuthException

use of com.jeeagile.core.exception.AgileAuthException in project jeeagile by jeeagile.

the class AgileSpringSecurity method userLogin.

@Override
public void userLogin(AgileLoginUser agileLoginUser) {
    try {
        UsernamePasswordAuthenticationToken passwordAuthenticationToken = new UsernamePasswordAuthenticationToken(agileLoginUser.getUserName(), agileLoginUser.getPassword());
        Authentication authentication = authenticationManager.authenticate(passwordAuthenticationToken);
        AgileUserDetails agileUserDetails = (AgileUserDetails) authentication.getPrincipal();
        if (agileUserDetails != null && AgileStringUtil.isNotEmpty(agileUserDetails.getUsername())) {
            String userToken = agileUserDetails.getUserData().getUserToken();
            AgileCacheUtil.put(AgileCacheConstants.AGILE_CACHE_SESSION_NAME, userToken, agileUserDetails);
            sessionRegistry.registerNewSession(userToken, userToken);
            SecurityContextHolder.getContext().setAuthentication(authentication);
        }
    } catch (AgileBaseException ex) {
        throw ex;
    } catch (Exception ex) {
        if (ex.getCause() instanceof AgileBaseException) {
            throw (AgileBaseException) ex.getCause();
        } else if (ex instanceof BadCredentialsException) {
            throw new AgileAuthException("用户登录密码错误!");
        } else {
            log.error("Spring Security用户登录认证出现异常", ex);
            throw new AgileAuthException("Spring Security用户登录认证出现异常!");
        }
    }
}
Also used : AgileUserDetails(com.jeeagile.springsecurity.userdetails.AgileUserDetails) AgileBaseException(com.jeeagile.core.exception.AgileBaseException) Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) AgileAuthException(com.jeeagile.core.exception.AgileAuthException) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) AgileAuthException(com.jeeagile.core.exception.AgileAuthException) AgileBaseException(com.jeeagile.core.exception.AgileBaseException)

Aggregations

AgileAuthException (com.jeeagile.core.exception.AgileAuthException)12 AgileBaseException (com.jeeagile.core.exception.AgileBaseException)12 AgileFrameException (com.jeeagile.core.exception.AgileFrameException)3 AgileBaseUser (com.jeeagile.core.security.user.AgileBaseUser)3 AgileSysUser (com.jeeagile.system.entity.AgileSysUser)3 UserAgent (eu.bitwalker.useragentutils.UserAgent)2 ArrayList (java.util.ArrayList)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 AgileDemoException (com.jeeagile.core.exception.AgileDemoException)1 AgileReference (com.jeeagile.core.protocol.annotation.AgileReference)1 AgileResultCode (com.jeeagile.core.result.AgileResultCode)1 IAgileSecurity (com.jeeagile.core.security.IAgileSecurity)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 AgileDemo (com.jeeagile.frame.annotation.AgileDemo)1 AgileUserDetails (com.jeeagile.springsecurity.userdetails.AgileUserDetails)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1