/** * Returns the instance of {@link SecurityContext} in current request of returns <code>null</code> if the context not exists. */ public static SecurityContext tryGetCurrent(){ RequestContext rc = RequestContext.tryGetCurrent(); if(null == rc) { return null; }else{ return (SecurityContext)rc.getAttribute(CONTEXT_ATTRIBUTE_NAME); } }
static SecurityContextHolder current() { SecurityContextHolder context = (SecurityContextHolder)RequestContext.current().getAttribute(CONTEXT_HOLDER_ATTRIBUTE_NAME); if(null == context){ throw new IllegalStateException("Current security context holder does not exists"); } return context; }
/** * Returns the instance of {@link SecurityContext} in current request. * * @throws IllegalStateException if the context not exists in current request. */ public static SecurityContext current() { SecurityContext context = (SecurityContext)RequestContext.current().getAttribute(CONTEXT_ATTRIBUTE_NAME); if(null == context){ throw new IllegalStateException("Current security context does not exists"); } return context; }
@Override public Object require() { RequestContext rc = RequestContext.tryGetCurrent(); if(null == rc) { OAuth2ModuleImpl module = (OAuth2ModuleImpl)rc.getAttribute(OAuth2ModuleImpl.class.getName()); if(null != module) { return module; } } SecurityContext sc = SecurityContext.tryGetCurrent(); if(null == sc) { return EMPTY; } Authentication authc = sc.getAuthentication(); if(!(authc instanceof OAuth2Authentication)) { return EMPTY; } OAuth2ModuleImpl module = new OAuth2ModuleImpl(this, (OAuth2Authentication)authc); rc.setAttribute(OAuth2ModuleImpl.class.getName(), module); return module; }