Search in sources :

Example 1 with UnauthorizedException

use of org.apache.shiro.authz.UnauthorizedException in project cals-api by ca-cwds.

the class RFA1aFormService method submitApplication.

/**
 * There is using XA Transaction
 */
private void submitApplication(Long formId, RFAApplicationStatus newStatus) throws NotSupportedException, SystemException, DroolsException {
    RFA1aFormDTO expandedFormDTO = getExpandedFormDTO(formId);
    performSubmissionValidation(expandedFormDTO);
    // Start transaction here
    UserTransaction userTransaction = new UserTransactionImp();
    userTransaction.setTransactionTimeout(3600);
    userTransaction.begin();
    PlacementHome storedPlacementHome = null;
    try {
        storedPlacementHome = storePlaceMentHome(expandedFormDTO);
        updateFormAfterPlacementHomeCreation(formId, storedPlacementHome.getIdentifier(), newStatus);
        userTransaction.commit();
    } catch (BusinessValidationException e) {
        userTransaction.rollback();
        LOG.error("Can not create Placement Home because of BusinessValidationException", e);
        throw e;
    } catch (UnauthorizedException e) {
        userTransaction.rollback();
        LOG.error("Can not create Placement Home because of UnauthorizedException", e);
        throw e;
    } catch (Exception e) {
        try {
            userTransaction.rollback();
        } catch (Exception re) {
            LOG.warn(re.getMessage(), re);
        }
        StringBuilder sb = new StringBuilder();
        sb.append(e.getMessage());
        sb.append('\n');
        Throwable cause = e.getCause();
        while (cause != null) {
            sb.append(" Cause: ");
            sb.append(cause.getMessage());
            sb.append('\n');
            cause = cause.getCause();
        }
        LOG.error("Can not create Placement Home: \n", e);
        throw new SystemException(sb.toString());
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) SystemException(javax.transaction.SystemException) PlacementHome(gov.ca.cwds.data.legacy.cms.entity.PlacementHome) RFA1aFormDTO(gov.ca.cwds.cals.service.dto.rfa.RFA1aFormDTO) BusinessValidationException(gov.ca.cwds.rest.exception.BusinessValidationException) UnauthorizedException(org.apache.shiro.authz.UnauthorizedException) UserTransactionImp(com.atomikos.icatch.jta.UserTransactionImp) DataAccessServicesException(gov.ca.cwds.cms.data.access.service.DataAccessServicesException) ExpectedException(gov.ca.cwds.rest.exception.ExpectedException) NotSupportedException(javax.transaction.NotSupportedException) DroolsException(gov.ca.cwds.drools.DroolsException) ConstraintViolationException(javax.validation.ConstraintViolationException) UnauthorizedException(org.apache.shiro.authz.UnauthorizedException) SystemException(javax.transaction.SystemException) BusinessValidationException(gov.ca.cwds.rest.exception.BusinessValidationException)

Example 2 with UnauthorizedException

use of org.apache.shiro.authz.UnauthorizedException in project shiro by apache.

the class AuthorizingRealmTest method testNullAuthzInfo.

@Test
public void testNullAuthzInfo() {
    AuthorizingRealm realm = new AuthorizingRealm() {

        protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
            return null;
        }

        protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
            return null;
        }
    };
    Principal principal = new UsernamePrincipal("blah");
    PrincipalCollection pCollection = new SimplePrincipalCollection(principal, "nullAuthzRealm");
    List<Permission> permList = new ArrayList<Permission>();
    permList.add(new WildcardPermission("stringPerm1"));
    permList.add(new WildcardPermission("stringPerm2"));
    List<String> roleList = new ArrayList<String>();
    roleList.add("role1");
    roleList.add("role2");
    boolean thrown = false;
    try {
        realm.checkPermission(pCollection, "stringPermission");
    } catch (UnauthorizedException e) {
        thrown = true;
    }
    assertTrue(thrown);
    thrown = false;
    try {
        realm.checkPermission(pCollection, new WildcardPermission("stringPermission"));
    } catch (UnauthorizedException e) {
        thrown = true;
    }
    assertTrue(thrown);
    thrown = false;
    try {
        realm.checkPermissions(pCollection, "stringPerm1", "stringPerm2");
    } catch (UnauthorizedException e) {
        thrown = true;
    }
    assertTrue(thrown);
    thrown = false;
    try {
        realm.checkPermissions(pCollection, permList);
    } catch (UnauthorizedException e) {
        thrown = true;
    }
    assertTrue(thrown);
    thrown = false;
    try {
        realm.checkRole(pCollection, "role1");
    } catch (UnauthorizedException e) {
        thrown = true;
    }
    assertTrue(thrown);
    thrown = false;
    try {
        realm.checkRoles(pCollection, roleList);
    } catch (UnauthorizedException e) {
        thrown = true;
    }
    assertTrue(thrown);
    assertFalse(realm.hasAllRoles(pCollection, roleList));
    assertFalse(realm.hasRole(pCollection, "role1"));
    assertArrayEquals(new boolean[] { false, false }, realm.hasRoles(pCollection, roleList));
    assertFalse(realm.isPermitted(pCollection, "perm1"));
    assertFalse(realm.isPermitted(pCollection, new WildcardPermission("perm1")));
    assertArrayEquals(new boolean[] { false, false }, realm.isPermitted(pCollection, "perm1", "perm2"));
    assertArrayEquals(new boolean[] { false, false }, realm.isPermitted(pCollection, permList));
    assertFalse(realm.isPermittedAll(pCollection, "perm1", "perm2"));
    assertFalse(realm.isPermittedAll(pCollection, permList));
}
Also used : PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) Permission(org.apache.shiro.authz.Permission) WildcardPermission(org.apache.shiro.authz.permission.WildcardPermission) UnauthorizedException(org.apache.shiro.authz.UnauthorizedException) WildcardPermission(org.apache.shiro.authz.permission.WildcardPermission) Principal(java.security.Principal) Test(org.junit.Test)

Example 3 with UnauthorizedException

use of org.apache.shiro.authz.UnauthorizedException in project wechat by dllwh.

the class ShiroHelper method login.

/**
 * ----------------------------------------------------- Fields end
 */
public static AjaxJson login(String userName, String passWord) {
    // 用户名密码令牌
    UsernamePasswordToken token = new UsernamePasswordToken(userName, passWord);
    token.setRememberMe(false);
    String logMsg = "", resultMsg = "";
    AjaxJson ajaxJson = new AjaxJson();
    boolean suc = false;
    // 获得当前登录用户对象Subject,现在状态为 “未认证”
    Subject subject = SecurityUtils.getSubject();
    try {
        subject.login(token);
    } catch (UnknownAccountException uae) {
        logMsg = "对用户[" + userName + "]进行登录验证..验证未通过,未知账户";
        resultMsg = MessageConstant.LOGIN_USER_UNKNOWN;
    } catch (IncorrectCredentialsException ice) {
        logMsg = "对用户[" + userName + "]进行登录验证..验证未通过,错误的凭证";
        resultMsg = MessageConstant.LOGIN_USER_REEOE;
    } catch (LockedAccountException lae) {
        logMsg = "对用户[" + userName + "]进行登录验证..验证未通过,账户已锁定";
        resultMsg = MessageConstant.LOGIN_USER_LOCK;
    } catch (DisabledAccountException dae) {
        logMsg = "对用户[" + userName + "]进行登录验证..验证未通过,帐号已被禁用";
        resultMsg = MessageConstant.LOGIN_USER_DISABLED;
    } catch (ExpiredCredentialsException ece) {
        logMsg = "对用户[" + userName + "]进行登录验证..验证未通过,帐号已过期";
        resultMsg = MessageConstant.LOGIN_USER_EXPIRED;
    } catch (ExcessiveAttemptsException eae) {
        logMsg = "对用户[" + userName + "]进行登录验证..验证未通过,用户名或密码错误次数过多";
        resultMsg = MessageConstant.LOGIN_USER_MORE;
    } catch (UnauthorizedException e) {
        logMsg = "对用户[" + userName + "]进行登录验证..验证未通过,您没有得到相应的授权!";
        resultMsg = MessageConstant.LOGIN_USER_UNAUTHORIZED;
    } catch (AuthenticationException ae) {
        logMsg = "对用户[" + userName + "]进行登录验证..验证未通过," + ae.getMessage();
        resultMsg = MessageConstant.LOGIN_ERROR;
    }
    if (subject.isAuthenticated()) {
        logMsg = "对用户[" + userName + "]进行登录验证..验证通过";
        suc = true;
    } else {
        token.clear();
    }
    ajaxJson.setSuccess(suc);
    ajaxJson.setMsg(resultMsg);
    ajaxJson.setObj(logMsg);
    return ajaxJson;
}
Also used : DisabledAccountException(org.apache.shiro.authc.DisabledAccountException) IncorrectCredentialsException(org.apache.shiro.authc.IncorrectCredentialsException) AuthenticationException(org.apache.shiro.authc.AuthenticationException) UnknownAccountException(org.apache.shiro.authc.UnknownAccountException) ExcessiveAttemptsException(org.apache.shiro.authc.ExcessiveAttemptsException) AjaxJson(com.cdeledu.common.base.AjaxJson) Subject(org.apache.shiro.subject.Subject) ExpiredCredentialsException(org.apache.shiro.authc.ExpiredCredentialsException) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken) UnauthorizedException(org.apache.shiro.authz.UnauthorizedException) LockedAccountException(org.apache.shiro.authc.LockedAccountException)

Aggregations

UnauthorizedException (org.apache.shiro.authz.UnauthorizedException)3 UserTransactionImp (com.atomikos.icatch.jta.UserTransactionImp)1 AjaxJson (com.cdeledu.common.base.AjaxJson)1 RFA1aFormDTO (gov.ca.cwds.cals.service.dto.rfa.RFA1aFormDTO)1 DataAccessServicesException (gov.ca.cwds.cms.data.access.service.DataAccessServicesException)1 PlacementHome (gov.ca.cwds.data.legacy.cms.entity.PlacementHome)1 DroolsException (gov.ca.cwds.drools.DroolsException)1 BusinessValidationException (gov.ca.cwds.rest.exception.BusinessValidationException)1 ExpectedException (gov.ca.cwds.rest.exception.ExpectedException)1 Principal (java.security.Principal)1 NotSupportedException (javax.transaction.NotSupportedException)1 SystemException (javax.transaction.SystemException)1 UserTransaction (javax.transaction.UserTransaction)1 ConstraintViolationException (javax.validation.ConstraintViolationException)1 AuthenticationException (org.apache.shiro.authc.AuthenticationException)1 DisabledAccountException (org.apache.shiro.authc.DisabledAccountException)1 ExcessiveAttemptsException (org.apache.shiro.authc.ExcessiveAttemptsException)1 ExpiredCredentialsException (org.apache.shiro.authc.ExpiredCredentialsException)1 IncorrectCredentialsException (org.apache.shiro.authc.IncorrectCredentialsException)1 LockedAccountException (org.apache.shiro.authc.LockedAccountException)1