Search in sources :

Example 1 with UnauthenticatedException

use of org.apache.shiro.authz.UnauthenticatedException in project ART-TIME by Artezio.

the class ShiroSecuredInterceptor method interceptShiroSecurity.

@AroundInvoke
public Object interceptShiroSecurity(InvocationContext context) throws Exception {
    Subject subject = SecurityUtils.getSubject();
    Class<?> clas = context.getTarget().getClass();
    Method method = context.getMethod();
    if (!subject.isAuthenticated() && hasAnnotation(clas, method, RequiresAuthentication.class)) {
        throw new UnauthenticatedException("Authentication required");
    }
    if (subject.getPrincipal() != null && hasAnnotation(clas, method, RequiresGuest.class)) {
        throw new UnauthenticatedException("Guest required");
    }
    if (subject.getPrincipal() == null && hasAnnotation(clas, method, RequiresUser.class)) {
        throw new UnauthenticatedException("User required");
    }
    RequiresRoles roles = getAnnotation(clas, method, RequiresRoles.class);
    if (roles != null) {
        subject.checkRoles(Arrays.asList(roles.value()));
    }
    RequiresPermissions permissions = getAnnotation(clas, method, RequiresPermissions.class);
    if (permissions != null) {
        subject.checkPermissions(permissions.value());
    }
    return context.proceed();
}
Also used : RequiresGuest(org.apache.shiro.authz.annotation.RequiresGuest) RequiresUser(org.apache.shiro.authz.annotation.RequiresUser) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) UnauthenticatedException(org.apache.shiro.authz.UnauthenticatedException) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) Method(java.lang.reflect.Method) RequiresRoles(org.apache.shiro.authz.annotation.RequiresRoles) Subject(org.apache.shiro.subject.Subject) AroundInvoke(javax.interceptor.AroundInvoke)

Example 2 with UnauthenticatedException

use of org.apache.shiro.authz.UnauthenticatedException in project fruit-manage by liuzhaozhao.

the class ShiroInterceptor method intercept.

public void intercept(Invocation ai) {
    AuthzHandler ah = ShiroKit.getAuthzHandler(ai.getActionKey());
    // 存在访问控制处理器。
    if (ah != null) {
        Controller c = ai.getController();
        try {
            // 执行权限检查。
            ah.assertAuthorized();
        } catch (UnauthenticatedException lae) {
            // 如果没有进行身份验证,返回HTTP401状态码,或者跳转到默认登录页面
            if (StrKit.notBlank(this.loginUrl)) {
                // 保存登录前的页面信息,只保存GET请求。其他请求不处理。
                if (c.getRequest().getMethod().equalsIgnoreCase("GET")) {
                    // SecurityUtils.getSubject().getSession().setAttribute(this.savedRequestKey, ai.getControllerKey()+"/" + ai.getMethodName() + this.extName);
                    if (c.getSessionAttr(this.savedRequestKey) == null) {
                        /*getRequestURL: http://localhost:10086/dafei/index.do
								getRequestURI: /dafei/index.do
								getQueryString: tt=121212&32323*/
                        HttpServletRequest req = c.getRequest();
                        String saveUrl = req.getRequestURI().substring(req.getContextPath().length());
                        String saveQs = req.getQueryString();
                        if (StrKit.notBlank(saveQs)) {
                            saveUrl = saveUrl + "?" + saveQs;
                        }
                        if (StrKit.notBlank(saveUrl)) {
                            c.setSessionAttr(this.savedRequestKey, saveUrl);
                        }
                    }
                }
                c.redirect(this.loginUrl);
            } else {
                ai.getController().renderError(401);
            }
            return;
        } catch (AuthorizationException ae) {
            // 如果没有权限访问对应的资源,返回HTTP状态码403,或者调转到为授权页面
            if (StrKit.notBlank(this.unauthorizedUrl)) {
                c.redirect(this.unauthorizedUrl);
            } else {
                c.renderError(403);
            }
            return;
        }
    }
    // 执行正常逻辑
    ai.invoke();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) UnauthenticatedException(org.apache.shiro.authz.UnauthenticatedException) AuthorizationException(org.apache.shiro.authz.AuthorizationException) Controller(com.jfinal.core.Controller)

Aggregations

UnauthenticatedException (org.apache.shiro.authz.UnauthenticatedException)2 Controller (com.jfinal.core.Controller)1 Method (java.lang.reflect.Method)1 AroundInvoke (javax.interceptor.AroundInvoke)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 AuthorizationException (org.apache.shiro.authz.AuthorizationException)1 RequiresAuthentication (org.apache.shiro.authz.annotation.RequiresAuthentication)1 RequiresGuest (org.apache.shiro.authz.annotation.RequiresGuest)1 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)1 RequiresRoles (org.apache.shiro.authz.annotation.RequiresRoles)1 RequiresUser (org.apache.shiro.authz.annotation.RequiresUser)1 Subject (org.apache.shiro.subject.Subject)1