【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
需要注意的是,这种方式不适用于class view.
-
修改
horizon.base.require_auth
:def require_auth(view_func): """Performs user authentication check. Similar to Django's `login_required` decorator, except that this throws :exc:`~horizon.exceptions.NotAuthenticated` exception if the user is not signed-in. """ from horizon.exceptions import NotAuthenticated # noqa @functools.wraps(view_func, assigned=available_attrs(view_func)) def dec(request, *args, **kwargs): #此处添加一个判断,如果view_func的public属性为True则跳过认证. if getattr(view_func,'public',False): return view_func(request, *args, **kwargs) if request.user.is_authenticated(): return view_func(request, *args, **kwargs) raise NotAuthenticated(_("Please log in to continue.")) return dec
-
修改
horizon.base.require_perms
:def require_perms(view_func, required): from horizon.exceptions import NotAuthorized # noqa # We only need to check each permission once for a view, so we'll use a set current_perms = getattr(view_func, '_required_perms', set([])) view_func._required_perms = current_perms | set(required) @functools.wraps(view_func, assigned=available_attrs(view_func)) def dec(request, *args, **kwargs): #此处添加一个判断,如果view_func的public属性为True则跳过认证. if getattr(view_func,'public',False): return view_func(request, *args, **kwargs) if request.user.is_authenticated(): if request.user.has_perms(view_func._required_perms): return view_func(request, *args, **kwargs) raise NotAuthorized(_("You are not authorized to access %s") % request.path) # If we don't have any permissions, just return the original view. if required: return dec else: return view_func
-
给需要匿名访问的View设置属性
public=True
setattr(view_func,'public',True)
来源:oschina
链接:https://my.oschina.net/u/1461430/blog/527028