Search in sources :

Example 6 with AgileBaseException

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

use of com.jeeagile.core.exception.AgileBaseException 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)

Example 8 with AgileBaseException

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

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

the class AgileAuthorizingRealm method doGetAuthorizationInfo.

/**
 * 用户授权
 *
 * @param principalCollection
 * @return
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    try {
        AgileBaseUser userData = (AgileBaseUser) principalCollection.getPrimaryPrincipal();
        if (userData == null || AgileStringUtil.isEmpty(userData.getUserId())) {
            throw new AgileAuthException(AgileResultCode.FAIL_USER_INFO);
        }
        SimpleAuthorizationInfo authenticationInfo = new SimpleAuthorizationInfo();
        List<String> userPermList = userData.getUserPerm();
        if (userPermList == null || userPermList.isEmpty()) {
            userPermList = agileUserDetailsService.getUserPerm(userData);
            userData.setUserPerm(userPermList);
        }
        authenticationInfo.addStringPermissions(userPermList);
        List<String> userRoleList = userData.getUserRole();
        if (userRoleList == null || userRoleList.isEmpty()) {
            userRoleList = agileUserDetailsService.getUserRole(userData);
            userData.setUserRole(userRoleList);
        }
        authenticationInfo.addRoles(userRoleList);
        return authenticationInfo;
    } catch (AgileBaseException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new AgileAuthException(AgileResultCode.FAIL_AUTH_EXCEPTION, ex.getMessage());
    }
}
Also used : SimpleAuthorizationInfo(org.apache.shiro.authz.SimpleAuthorizationInfo) AgileBaseException(com.jeeagile.core.exception.AgileBaseException) 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 10 with AgileBaseException

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

the class AgileShiroSecurity method userLogin.

@Override
public void userLogin(AgileLoginUser agileLoginUser) {
    try {
        UsernamePasswordToken token = new UsernamePasswordToken(agileLoginUser.getUserName(), agileLoginUser.getPassword());
        token.setRememberMe(agileLoginUser.isRememberMe());
        SecurityUtils.getSubject().login(token);
    } catch (AgileBaseException ex) {
        throw ex;
    } catch (Exception ex) {
        if (ex.getCause() instanceof AgileBaseException) {
            throw (AgileBaseException) ex.getCause();
        }
        throw new AgileAuthException("SHIRO用户登录认证出现异常!");
    }
}
Also used : AgileBaseException(com.jeeagile.core.exception.AgileBaseException) AgileAuthException(com.jeeagile.core.exception.AgileAuthException) AgileAuthException(com.jeeagile.core.exception.AgileAuthException) AgileBaseException(com.jeeagile.core.exception.AgileBaseException) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken)

Aggregations

AgileBaseException (com.jeeagile.core.exception.AgileBaseException)13 AgileAuthException (com.jeeagile.core.exception.AgileAuthException)11 AgileFrameException (com.jeeagile.core.exception.AgileFrameException)4 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 Method (java.lang.reflect.Method)1 List (java.util.List)1