本文整理汇总了Java中org.pac4j.core.context.WebContext类的典型用法代码示例。如果您正苦于以下问题:Java WebContext类的具体用法?Java WebContext怎么用?Java WebContext使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
WebContext类属于org.pac4j.core.context包,在下文中一共展示了WebContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: validate
import org.pac4j.core.context.WebContext; //导入依赖的package包/类
@Override
public void validate(final UsernamePasswordCredentials credentials, final WebContext context) throws HttpAction {
if (credentials == null) {
throwsException("No credential");
}
String username = credentials.getUsername();
String password = credentials.getPassword();
if (CommonHelper.isBlank(username)) {
throwsException("Username cannot be blank");
}
if (CommonHelper.isBlank(password)) {
throwsException("Password cannot be blank");
}
if (CommonHelper.areNotEquals(username, password)) {
throwsException("Username : '" + username + "' does not match password");
}
final CommonProfile profile = new CommonProfile();
profile.setId(username);
profile.addAttribute(Pac4jConstants.USERNAME, username);
credentials.setUserProfile(profile);
}
示例2: compute
import org.pac4j.core.context.WebContext; //导入依赖的package包/类
@Override
public String compute(final String callbackUrl, WebContext context) {
if (context != null && callbackUrl != null && !callbackUrl.startsWith("http://") && !callbackUrl.startsWith("https://")) {
final StringBuilder sb = new StringBuilder();
sb.append(context.getScheme()).append("://").append(context.getServerName());
final boolean notDefaultHttpPort = ContextHelper.isHttp(context) && context.getServerPort() != HttpConstants.DEFAULT_HTTP_PORT;
final boolean notDefaultHttpsPort = ContextHelper.isHttps(context) && context.getServerPort() != HttpConstants.DEFAULT_HTTPS_PORT;
if (notDefaultHttpPort || notDefaultHttpsPort) {
sb.append(":").append(context.getServerPort());
}
sb.append(callbackUrl.startsWith("/") ? callbackUrl : "/" + callbackUrl);
return sb.toString();
}
return callbackUrl;
}
示例3: check
import org.pac4j.core.context.WebContext; //导入依赖的package包/类
@Override
protected boolean check(final WebContext context, final U profile, final String element) throws HttpAction {
if (!profile.containsAttribute(element)) {
return false;
}
if (CommonHelper.isBlank(this.valueToMatch)) {
return true;
}
final Object attributeValues = profile.getAttribute(element);
if (attributeValues instanceof Collection) {
return Collection.class.cast(attributeValues)
.stream()
.filter(v -> v.toString().matches(this.valueToMatch))
.findAny()
.isPresent();
}
return attributeValues.toString().matches(this.valueToMatch);
}
示例4: extract
import org.pac4j.core.context.WebContext; //导入依赖的package包/类
@Override
public UsernamePasswordCredentials extract(WebContext context) throws HttpAction {
final TokenCredentials credentials = this.extractor.extract(context);
if (credentials == null) {
return null;
}
final byte[] decoded = Base64.getDecoder().decode(credentials.getToken());
String token;
try {
token = new String(decoded, "UTF-8");
} catch (final UnsupportedEncodingException e) {
throw new CredentialsException("Bad format of the basic auth header");
}
final int delim = token.indexOf(":");
if (delim < 0) {
throw new CredentialsException("Bad format of the basic auth header");
}
return new UsernamePasswordCredentials(token.substring(0, delim),
token.substring(delim + 1), clientName);
}
示例5: constructCredentialsFromRequest
import org.pac4j.core.context.WebContext; //导入依赖的package包/类
@Override
protected Credential constructCredentialsFromRequest(final RequestContext requestContext) {
try {
final HttpServletRequest request = WebUtils.getHttpServletRequest(requestContext);
final HttpServletResponse response = WebUtils.getHttpServletResponse(requestContext);
final BasicAuthExtractor extractor = new BasicAuthExtractor(this.getClass().getSimpleName());
final WebContext webContext = WebUtils.getPac4jJ2EContext(request, response);
final UsernamePasswordCredentials credentials = extractor.extract(webContext);
if (credentials != null) {
LOGGER.debug("Received basic authentication request from credentials [{}]", credentials);
return new UsernamePasswordCredential(credentials.getUsername(), credentials.getPassword());
}
} catch (final Exception e) {
LOGGER.warn(e.getMessage(), e);
}
return null;
}
示例6: retrieveCredentials
import org.pac4j.core.context.WebContext; //导入依赖的package包/类
@Override
protected UsernamePasswordCredentials retrieveCredentials(final WebContext context) throws HttpAction {
CommonHelper.assertNotNull("credentialsExtractor", getCredentialsExtractor());
CommonHelper.assertNotNull("authenticator", getAuthenticator());
final UsernamePasswordCredentials credentials;
try {
// retrieve credentials
credentials = getCredentialsExtractor().extract(context);
logger.debug("credentials : {}", credentials);
if (credentials == null) {
throw HttpAction.unauthorized("Requires authentication", context, this.realmName, null);
}
// validate credentials
getAuthenticator().validate(credentials, context);
} catch (final CredentialsException e) {
throw HttpAction.unauthorized("Requires authentication", context, this.realmName, null);
}
return credentials;
}
示例7: isAuthorized
import org.pac4j.core.context.WebContext; //导入依赖的package包/类
@Override
public boolean isAuthorized(final WebContext context, final List<CommonProfile> profiles) throws HttpAction {
final String url = context.getFullRequestURL().toLowerCase();
if (!url.endsWith(".css")
&& !url.endsWith(".js")
&& !url.endsWith(".png")
&& !url.endsWith(".jpg")
&& !url.endsWith(".ico")
&& !url.endsWith(".jpeg")
&& !url.endsWith(".bmp")
&& !url.endsWith(".gif")) {
context.setResponseHeader("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate");
context.setResponseHeader("Pragma", "no-cache");
context.setResponseHeader("Expires", "0");
}
return true;
}
示例8: getOAuthCredentials
import org.pac4j.core.context.WebContext; //导入依赖的package包/类
@Override
protected OAuthCredentials getOAuthCredentials(final WebContext context) throws HttpAction {
final String tokenParameter = context.getRequestParameter(OAUTH_TOKEN);
final String verifierParameter = context.getRequestParameter(OAUTH_VERIFIER);
if (tokenParameter != null && verifierParameter != null) {
// get request token from session
final OAuth1RequestToken tokenSession = (OAuth1RequestToken) context.getSessionAttribute(getRequestTokenSessionAttributeName());
logger.debug("tokenRequest: {}", tokenSession);
final String token = OAuthEncoder.decode(tokenParameter);
final String verifier = OAuthEncoder.decode(verifierParameter);
logger.debug("token: {} / verifier: {}", token, verifier);
return new OAuth10Credentials(tokenSession, token, verifier, getName());
} else {
final String message = "No credential found";
throw new OAuthCredentialsException(message);
}
}
示例9: doAuthentication
import org.pac4j.core.context.WebContext; //导入依赖的package包/类
@Override
protected HandlerResult doAuthentication(final Credential credential) throws GeneralSecurityException, PreventedException {
final ClientCredential clientCredentials = (ClientCredential) credential;
logger.debug("clientCredentials {}", clientCredentials);
final Credentials credentials = clientCredentials.getCredentials();
final String clientName = credentials.getClientName();
logger.debug("clientName: {}", clientName);
// get client
final Client<Credentials, UserProfile> client = this.clients.findClient(clientName);
logger.debug("client: {}", client);
// web context
final HttpServletRequest request = WebUtils.getHttpServletRequest();
final HttpServletResponse response = WebUtils.getHttpServletResponse();
final WebContext webContext = new J2EContext(request, response);
// get user profile
final UserProfile userProfile = client.getUserProfile(credentials, webContext);
logger.debug("userProfile: {}", userProfile);
return createResult(clientCredentials, userProfile);
}
示例10: getUserProfile
import org.pac4j.core.context.WebContext; //导入依赖的package包/类
@Override
public final U getUserProfile(final C credentials, final WebContext context) throws HttpAction {
init(context);
logger.debug("credentials : {}", credentials);
if (credentials == null) {
return null;
}
final U profile = retrieveUserProfile(credentials, context);
if (profile != null) {
profile.setClientName(getName());
if (this.authorizationGenerators != null) {
for (AuthorizationGenerator<U> authorizationGenerator : this.authorizationGenerators) {
authorizationGenerator.generate(profile);
}
}
}
return profile;
}
示例11: validate
import org.pac4j.core.context.WebContext; //导入依赖的package包/类
@Override
public void validate(final TokenCredentials credentials, final WebContext context) throws HttpAction {
if (credentials == null) {
throw new CredentialsException("No credential");
}
if (!(credentials instanceof DigestCredentials)) {
throw new CredentialsException ("Unsupported credentials type " + credentials.getClass());
}
DigestCredentials digestCredentials = (DigestCredentials) credentials;
String username = digestCredentials.getUsername();
if (CommonHelper.isBlank(username)) {
throw new CredentialsException("Username cannot be blank");
}
String token = credentials.getToken();
if (CommonHelper.isBlank(token)) {
throw new CredentialsException("Token cannot be blank");
}
CommonProfile profile = new CommonProfile();
profile.setId(username);
credentials.setUserProfile(profile);
}
示例12: retrieveRedirectAction
import org.pac4j.core.context.WebContext; //导入依赖的package包/类
@Override
protected RedirectAction retrieveRedirectAction(final WebContext wc) throws HttpAction {
final SAML2MessageContext context = this.contextProvider.buildContext(wc);
final String relayState = getStateParameter(wc);
final AuthnRequest authnRequest = this.saml2ObjectBuilder.build(context);
this.profileHandler.send(context, authnRequest, relayState);
final Pac4jSAMLResponse adapter = context.getProfileRequestContextOutboundMessageTransportResponse();
if (this.configuration.getDestinationBindingType().equalsIgnoreCase(SAMLConstants.SAML2_POST_BINDING_URI)) {
final String content = adapter.getOutgoingContent();
return RedirectAction.success(content);
}
final String location = adapter.getRedirectUrl();
return RedirectAction.redirect(location);
}
示例13: extract
import org.pac4j.core.context.WebContext; //导入依赖的package包/类
/**
* Extracts digest Authorization header components.
* As per RFC 2617 :
* username is the user's name in the specified realm
* qop is quality of protection
* uri is the request uri
* response is the client response
* nonce is a server-specified data string which should be uniquely generated
* each time a 401 response is made
* cnonce is the client nonce
* nc is the nonce count
* If in the Authorization header it is not specified a username and response, we throw CredentialsException because
* the client uses an username and a password to authenticate. response is just a MD5 encoded value
* based on user provided password and RFC 2617 digest authentication encoding rules
* @param context the current web context
* @return the Digest credentials
*/
@Override
public DigestCredentials extract(WebContext context) throws HttpAction {
final TokenCredentials credentials = this.extractor.extract(context);
if (credentials == null) {
return null;
}
String token = credentials.getToken();
Map<String, String> valueMap = parseTokenValue(token);
String username = valueMap.get("username");
String response = valueMap.get("response");
if (CommonHelper.isBlank(username) || CommonHelper.isBlank(response)) {
throw new CredentialsException("Bad format of the digest auth header");
}
String realm = valueMap.get("realm");
String nonce = valueMap.get("nonce");
String uri = valueMap.get("uri");
String cnonce = valueMap.get("cnonce");
String nc = valueMap.get("nc");
String qop = valueMap.get("qop");
String method = context.getRequestMethod();
return new DigestCredentials(response, method, clientName, username, realm, nonce, uri, cnonce, nc, qop);
}
示例14: testFromBlockingCredentialsExceptionBehaviour
import org.pac4j.core.context.WebContext; //导入依赖的package包/类
@Test(timeout=1000, expected=CredentialsException.class)
public void testFromBlockingCredentialsExceptionBehaviour(final TestContext testContext) throws Exception {
when(extractor.extract(any(WebContext.class))).thenThrow(new CredentialsException("Intentional credentials exception"));
final Async async = testContext.async();
final CompletableFuture<TestCredentials> credsFuture = AsyncCredentialsExtractor.fromBlocking(extractor).extract(webContext);
assertSuccessfulEvaluation(credsFuture, creds -> {}, async);
}
示例15: matches
import org.pac4j.core.context.WebContext; //导入依赖的package包/类
@Override
public boolean matches(final WebContext context) {
if (pattern != null) {
final String path = context.getPath();
logger.debug("path to match: {}", path);
return !pattern.matcher(path).matches();
}
return true;
}