Example usage for org.apache.shiro.authz UnauthenticatedException UnauthenticatedException

List of usage examples for org.apache.shiro.authz UnauthenticatedException UnauthenticatedException

Introduction

In this page you can find the example usage for org.apache.shiro.authz UnauthenticatedException UnauthenticatedException.

Prototype

public UnauthenticatedException(Throwable cause) 

Source Link

Document

Constructs a new UnauthenticatedException.

Usage

From source file:br.com.diego.shiro.ShiroSecuredInterceptor.java

@AroundInvoke
public Object interceptShiroSecurity(InvocationContext context) throws Exception {
    Subject subject = SecurityUtils.getSubject();
    Class<?> c = context.getTarget().getClass();
    Method m = context.getMethod();

    if (!subject.isAuthenticated() && hasAnnotation(c, m, RequiresAuthentication.class)) {
        throw new UnauthenticatedException("Authentication required");
    }/*from  w w  w  .ja va  2s. co  m*/

    if (subject.getPrincipal() != null && hasAnnotation(c, m, RequiresGuest.class)) {
        throw new UnauthenticatedException("Guest required");
    }

    if (subject.getPrincipal() == null && hasAnnotation(c, m, RequiresUser.class)) {
        throw new UnauthenticatedException("User required");
    }

    RequiresRoles roles = getAnnotation(c, m, RequiresRoles.class);

    if (roles != null) {
        subject.checkRoles(Arrays.asList(roles.value()));
    }

    RequiresPermissions permissions = getAnnotation(c, m, RequiresPermissions.class);

    if (permissions != null) {
        subject.checkPermissions(permissions.value());
    }

    return context.proceed();
}

From source file:cn.dreampie.common.plugin.shiro.plugin.AuthenticatedAuthzHandler.java

License:Apache License

@Override
public void assertAuthorized() throws AuthorizationException {
    if (!getSubject().isAuthenticated()) {
        throw new UnauthenticatedException("The current Subject is not authenticated.  Access denied.");
    }/*from ww  w.ja  va  2 s  .c  o  m*/
}

From source file:cn.dreampie.common.plugin.shiro.plugin.GuestAuthzHandler.java

License:Apache License

@Override
public void assertAuthorized() throws AuthorizationException {
    Subject subject = getSubject();

    if (subject.getPrincipal() == null) {
        return;//from w  ww  . ja  v  a2 s  .  c o  m
    }
    throw new UnauthenticatedException("Attempting to perform a guest-only operation.  The current Subject is "
            + "not a guest (they have been authenticated or remembered from a previous login).  Access "
            + "denied.");
}

From source file:cn.dreampie.common.plugin.shiro.plugin.UserAuthzHandler.java

License:Apache License

@Override
public void assertAuthorized() throws AuthorizationException {
    Subject subject = getSubject();
    if (subject.getPrincipal() == null) {
        throw new UnauthenticatedException(
                "Attempting to perform a user-only operation.  The current Subject is "
                        + "not a user (they haven't been authenticated or remembered from a previous login).  "
                        + "Access denied.");
    }/*from  ww  w  . ja  v  a 2 s  .co  m*/
}

From source file:com.caricah.iotracah.bootstrap.security.realm.state.IOTSubject.java

License:Apache License

protected void assertAuthzCheckPossible() throws AuthorizationException {
    if (!hasPrincipals()) {
        String msg = "This subject is anonymous - it does not have any identifying principals and "
                + "authorization operations require an identity to check against.  A Subject instance will "
                + "acquire these identifying principals automatically after a successful login is performed "
                + "be executing " + Subject.class.getName()
                + ".login(AuthenticationToken) or when 'Remember Me' "
                + "functionality is enabled by the SecurityManager.  This exception can also occur when a "
                + "previously logged-in Subject has logged out which "
                + "makes it anonymous again.  Because an identity is currently not known due to any of these "
                + "conditions, authorization is denied.";
        throw new UnauthenticatedException(msg);
    }//from  w w w  . j a  v  a  2s  .c o  m
}

From source file:com.caricah.iotracah.core.handlers.RequestHandler.java

License:Apache License

public Observable<IOTClient> checkPermission(String sessionId, String authKey, AuthorityRole role,
        List<String> topicList) {

    return Observable.create(observable -> {

        IotClientKey clientKey = new IotClientKey();
        clientKey.setSessionId(sessionId);

        Subject subject = new Subject.Builder().sessionId(clientKey).buildSubject();

        final IOTClient session = (IOTClient) subject.getSession(false);

        if (session != null && subject.isAuthenticated()) {

            try {

                if (!AuthorityRole.CONNECT.equals(role)) {

                    if (Protocol.fromString(session.getProtocol()).isNotPersistent()) {

                        String session_auth_key = session.getAuthKey();

                        /**
                         * Make sure for non persistent connections the authKey matches
                         * the stored authKey. Otherwise fail the request.
                         *//*  w  w w  .j  a va2 s  .c om*/
                        if (!StringUtils.isEmpty(session_auth_key)) {
                            if (!session_auth_key.equals(authKey))
                                throw new UnauthenticatedException("Client fails auth key assertion.");

                        }
                    }

                    List<Permission> permissions = topicList.stream()
                            .map(topic -> getPermission(session.getPartitionId(), session.getUsername(),
                                    session.getClientIdentification(), role, topic))
                            .collect(Collectors.toList());

                    subject.checkPermissions(permissions);
                }

                //Update session last accessed time.
                session.touch();

                observable.onNext(session);
                observable.onCompleted();

            } catch (AuthorizationException e) {
                //Notify failure to authorize user.
                observable.onError(e);
            }

        } else {
            observable.onError(new AuthenticationException(
                    "Client must be authenticated {Try connecting first} found : " + session));
        }

    });

}

From source file:com.dbumama.market.web.core.plugin.shiro.AuthenticatedAuthzHandler.java

License:Apache License

public void assertAuthorized() throws AuthorizationException {
    if (!getSubject().isAuthenticated()) {
        throw new UnauthenticatedException("The current Subject is not authenticated.  Access denied.");
    }//from w  w w.j a v  a2s .co m
}

From source file:com.dbumama.market.web.core.plugin.shiro.GuestAuthzHandler.java

License:Apache License

public void assertAuthorized() throws AuthorizationException {
    if (getSubject().getPrincipal() != null) {
        throw new UnauthenticatedException(
                "Attempting to perform a guest-only operation.  The current Subject is "
                        + "not a guest (they have been authenticated or remembered from a previous login).  Access "
                        + "denied.");
    }//from  ww w.  java 2 s . c  om
}

From source file:com.dbumama.market.web.core.plugin.shiro.UserAuthzHandler.java

License:Apache License

public void assertAuthorized() throws AuthorizationException {
    if (getSubject().getPrincipal() == null) {
        throw new UnauthenticatedException(
                "Attempting to perform a user-only operation.  The current Subject is "
                        + "not a user (they haven't been authenticated or remembered from a previous login).  "
                        + "Access denied.");
    }//from   w  w  w .  ja  va 2 s  . c  o  m
}

From source file:com.ftww.basic.plugin.shiro.core.handler.AuthenticatedAuthzHandler.java

License:Apache License

@Override
public void assertAuthorized() throws AuthorizationException {
    if (!getSubject().isAuthenticated()) {
        throw new UnauthenticatedException("???");
    }//from   ww w .  ja  va 2  s .  c  om

}