本文整理汇总了Java中org.pac4j.core.context.J2EContext类的典型用法代码示例。如果您正苦于以下问题:Java J2EContext类的具体用法?Java J2EContext怎么用?Java J2EContext使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
J2EContext类属于org.pac4j.core.context包,在下文中一共展示了J2EContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: constructCredentialsFromRequest
import org.pac4j.core.context.J2EContext; //导入依赖的package包/类
@Override
protected Credential constructCredentialsFromRequest(final RequestContext requestContext) {
final HttpServletRequest request = WebUtils.getHttpServletRequest(requestContext);
final HttpServletResponse response = WebUtils.getHttpServletResponse(requestContext);
final BasicAuthExtractor extractor = new BasicAuthExtractor(this.getClass().getSimpleName());
final WebContext webContext = new J2EContext(request, response);
try {
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;
}
示例2: doAuthentication
import org.pac4j.core.context.J2EContext; //导入依赖的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);
}
示例3: getAuthenticatedUsername
import org.pac4j.core.context.J2EContext; //导入依赖的package包/类
/**
* Return the username of the authenticated user (based on pac4j security).
*
* @return the authenticated username.
*/
public static String getAuthenticatedUsername() {
final HttpServletRequest request = getHttpServletRequest();
final HttpServletResponse response = getHttpServletResponse();
if (request != null && response != null) {
final J2EContext context = new J2EContext(request, response);
final ProfileManager manager = new ProfileManager(context);
final UserProfile profile = manager.get(true);
if (profile != null) {
final String id = profile.getId();
if (id != null) {
return id;
}
}
}
return UNKNOWN_USER;
}
示例4: login
import org.pac4j.core.context.J2EContext; //导入依赖的package包/类
/**
* app rest 登录获取token
* eg:http://localhost:8081/user/login?cilent_name=rest&username=hsjhsj&password=hsjhsj
* 然后获取资源:http://localhost:8081/user/1?token=eyJjdHkiOiJKV1QiLCJlbmMiOiJBMjU2R0NNIiwiYWxnIjoiZGlyIn0..7usGh1GK3jl5_wPH.QJdYqNp81zRyAs6OHmN4573l67z_UgxQ7WXJ7OUsDw50Dato2X9Tyh5kXBAJF5l9LmmKe8y-kHrhyx9gcEIa6PC97mo5fPbCw9WoOypyTqdWkE1Q9mM44Zn8CZZVH9PTml7_0jwln0W_bzDWjN3f-0Pk2etxU6lXwz5insFVz4nGt5SEmykhvOdKlscLsYbHGQVqze4nlXuAtVXQ08CuphRsZ2FmSaK-LFR8Ivs.DkqbT-PgEjE0ZS6pgNVqGA
* @Description:TODO
* @author:hsj qq:2356899074
* @time:2017年12月11日 下午2:36:30
* @param request
* @param response
* @return
*/
@RequestMapping("/user/login")
public Object login(HttpServletRequest request, HttpServletResponse response) {
Map<String, Object> model = new HashMap<>();
J2EContext context = new J2EContext(request, response);
final ProfileManager<CasRestProfile> manager = new ProfileManager(context);
final Optional<CasRestProfile> profile = manager.get(true);
//获取ticket
TokenCredentials tokenCredentials = casRestFormClient.requestServiceTicket(serviceUrl, profile.get(), context);
//根据ticket获取用户信息
final CasProfile casProfile = casRestFormClient.validateServiceTicket(serviceUrl, tokenCredentials, context);
//生成jwt token
String token = generator.generate(casProfile);
model.put("token", token);
return new HttpEntity<>(model);
}
示例5: prepareForLoginPage
import org.pac4j.core.context.J2EContext; //导入依赖的package包/类
/**
* Prepare the data for the login page.
*
* @param context The current webflow context
*/
protected void prepareForLoginPage(final RequestContext context) {
final HttpServletRequest request = WebUtils.getHttpServletRequest(context);
final HttpServletResponse response = WebUtils.getHttpServletResponse(context);
final HttpSession session = request.getSession();
// web context
final WebContext webContext = new J2EContext(request, response);
// save parameters in web session
final WebApplicationService service = WebUtils.getService(context);
logger.debug("save service: {}", service);
session.setAttribute(SERVICE, service);
saveRequestParameter(request, session, THEME);
saveRequestParameter(request, session, LOCALE);
saveRequestParameter(request, session, METHOD);
// for all clients, generate redirection urls
for (final Client client : this.clients.findAllClients()) {
final String key = client.getName() + "Url";
final BaseClient baseClient = (BaseClient) client;
final String redirectionUrl = baseClient.getRedirectionUrl(webContext);
logger.debug("{} -> {}", key, redirectionUrl);
context.getFlowScope().put(key, redirectionUrl);
}
}
示例6: buildCallbackUrlForTokenResponseType
import org.pac4j.core.context.J2EContext; //导入依赖的package包/类
@Override
protected String buildCallbackUrlForTokenResponseType(final J2EContext context, final Authentication authentication,
final Service service, final String redirectUri,
final String responseType,
final String clientId) {
if (!OAuth20Utils.isResponseType(responseType, OAuth20ResponseTypes.IDTOKEN_TOKEN)) {
return super.buildCallbackUrlForTokenResponseType(context, authentication, service,
redirectUri, responseType, clientId);
}
LOGGER.debug("Handling callback for response type [{}]", responseType);
final TicketGrantingTicket ticketGrantingTicket = CookieUtils.getTicketGrantingTicketFromRequest(
ticketGrantingTicketCookieGenerator, this.ticketRegistry, context.getRequest());
return buildCallbackUrlForImplicitTokenResponseType(context, authentication,
service, redirectUri, clientId, OAuth20ResponseTypes.IDTOKEN_TOKEN, ticketGrantingTicket);
}
示例7: buildCallbackUrlForImplicitTokenResponseType
import org.pac4j.core.context.J2EContext; //导入依赖的package包/类
private String buildCallbackUrlForImplicitTokenResponseType(final J2EContext context,
final Authentication authentication,
final Service service,
final String redirectUri,
final String clientId,
final OAuth20ResponseTypes responseType,
final TicketGrantingTicket ticketGrantingTicket) {
try {
final OidcRegisteredService oidcService = (OidcRegisteredService)
OAuth20Utils.getRegisteredOAuthService(this.servicesManager, clientId);
final AccessTokenRequestDataHolder holder = new AccessTokenRequestDataHolder(service, authentication, oidcService, ticketGrantingTicket);
final AccessToken accessToken = generateAccessToken(holder);
LOGGER.debug("Generated OAuth access token: [{}]", accessToken);
final long timeout = casProperties.getTicket().getTgt().getTimeToKillInSeconds();
final String idToken = this.idTokenGenerator.generate(context.getRequest(),
context.getResponse(),
accessToken, timeout, responseType, oidcService);
LOGGER.debug("Generated id token [{}]", idToken);
final List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair(OidcConstants.ID_TOKEN, idToken));
return buildCallbackUrlResponseType(authentication, service, redirectUri, accessToken, params);
} catch (final Exception e) {
throw Throwables.propagate(e);
}
}
示例8: filter
import org.pac4j.core.context.J2EContext; //导入依赖的package包/类
@Override
public Principal filter(final Service service, final Principal profile,
final RegisteredService registeredService, final J2EContext context) {
final Principal principal = super.filter(service, profile, registeredService, context);
if (registeredService instanceof OidcRegisteredService) {
final OidcRegisteredService oidcService = (OidcRegisteredService) registeredService;
final Collection<String> scopes = new ArrayList<>(OAuth20Utils.getRequestedScopes(context));
scopes.addAll(oidcService.getScopes());
if (!scopes.contains(OidcConstants.OPENID)) {
LOGGER.debug("Request does not indicate a scope [{}] that can identify OpenID Connect", scopes);
return principal;
}
final Map<String, Object> attributes = new HashMap<>();
filterAttributesByScope(scopes, attributes, principal, service, oidcService);
return this.principalFactory.createPrincipal(profile.getId(), attributes);
}
return principal;
}
示例9: generate
import org.pac4j.core.context.J2EContext; //导入依赖的package包/类
/**
* Generate string.
*
* @param request the request
* @param response the response
* @param accessTokenId the access token id
* @param timeout the timeout
* @param responseType the response type
* @param registeredService the registered service
* @return the string
* @throws Exception the exception
*/
public String generate(final HttpServletRequest request,
final HttpServletResponse response,
final AccessToken accessTokenId,
final long timeout,
final OAuth20ResponseTypes responseType,
final OAuthRegisteredService registeredService) throws Exception {
final OidcRegisteredService oidcRegisteredService = (OidcRegisteredService) registeredService;
final J2EContext context = WebUtils.getPac4jJ2EContext(request, response);
final ProfileManager manager = WebUtils.getPac4jProfileManager(request, response);
final Optional<UserProfile> profile = manager.get(true);
LOGGER.debug("Attempting to produce claims for the id token [{}]", accessTokenId);
final JwtClaims claims = produceIdTokenClaims(request, accessTokenId, timeout,
oidcRegisteredService, profile.get(), context, responseType);
LOGGER.debug("Produce claims for the id token [{}] as [{}]", accessTokenId, claims);
return this.signingService.encode(oidcRegisteredService, claims);
}
示例10: doExecute
import org.pac4j.core.context.J2EContext; //导入依赖的package包/类
@Override
protected Event doExecute(final RequestContext requestContext) throws Exception {
try {
final HttpServletRequest request = WebUtils.getHttpServletRequest(requestContext);
final HttpServletResponse response = WebUtils.getHttpServletResponse(requestContext);
final J2EContext context = WebUtils.getPac4jJ2EContext(request, response);
final SAML2Client client = clients.findClient(SAML2Client.class);
if (client != null) {
LOGGER.debug("Located SAML2 client [{}]", client);
final RedirectAction action = client.getLogoutAction(context, null, null);
LOGGER.debug("Preparing logout message to send is [{}]", action.getLocation());
action.perform(context);
}
} catch (final Exception e) {
LOGGER.warn(e.getMessage(), e);
}
return null;
}
示例11: prepareForLoginPage
import org.pac4j.core.context.J2EContext; //导入依赖的package包/类
/**
* Prepare the data for the login page.
*
* @param context The current webflow context
*/
protected void prepareForLoginPage(final RequestContext context) {
final HttpServletRequest request = WebUtils.getHttpServletRequest(context);
final HttpServletResponse response = WebUtils.getHttpServletResponse(context);
final HttpSession session = request.getSession();
// web context
final WebContext webContext = new J2EContext(request, response);
// save parameters in web session
final Service service = (Service) context.getFlowScope().get(SERVICE);
logger.info("save service: {}", service);
session.setAttribute(SERVICE, service);
saveRequestParameter(request, session, THEME);
saveRequestParameter(request, session, LOCALE);
saveRequestParameter(request, session, METHOD);
// for all clients, generate redirection urls
for (final Client client : this.clients.findAllClients()) {
final String key = client.getName() + "Url";
final BaseClient baseClient = (BaseClient) client;
final String redirectionUrl = baseClient.getRedirectionUrl(webContext);
logger.info("{} -> {}", key, redirectionUrl);
context.getFlowScope().put(key, redirectionUrl);
}
}
示例12: buildCallbackUrlForImplicitTokenResponseType
import org.pac4j.core.context.J2EContext; //导入依赖的package包/类
private String buildCallbackUrlForImplicitTokenResponseType(J2EContext context, Authentication authentication, Service service, String redirectUri, String clientId, OAuth20ResponseTypes responseType, TicketGrantingTicket ticketGrantingTicket) {
try {
OidcRegisteredService e = (OidcRegisteredService) OAuth20Utils.getRegisteredOAuthService(this.servicesManager, clientId);
AccessTokenRequestDataHolder holder = new AccessTokenRequestDataHolder(service, authentication, e, ticketGrantingTicket);
AccessToken accessToken = this.generateAccessToken(holder);
LOGGER.debug("Generated OAuth access token: [{}]", accessToken);
long timeout = (long) this.casProperties.getTicket().getTgt().getTimeToKillInSeconds();
String idToken = this.idTokenGenerator.generate(context.getRequest(), context.getResponse(), accessToken, timeout, responseType, e);
LOGGER.debug("Generated id token [{}]", idToken);
ArrayList params = new ArrayList();
params.add(new BasicNameValuePair("id_token", idToken));
return this.buildCallbackUrlResponseType(authentication, service, redirectUri, accessToken, params);
} catch (Exception var15) {
throw Throwables.propagate(var15);
}
}
示例13: generate
import org.pac4j.core.context.J2EContext; //导入依赖的package包/类
/**
* Generate string.
*
* @param request the request
* @param response the response
* @param accessTokenId the access token id
* @param timeout the timeout
* @param responseType the response type
* @param registeredService the registered service
* @return the string
* @throws Exception the exception
*/
public String generate(final HttpServletRequest request,
final HttpServletResponse response,
final AccessToken accessTokenId,
final long timeout,
final OAuth20ResponseTypes responseType,
final OAuthRegisteredService registeredService) throws Exception {
final OidcRegisteredService oidcRegisteredService = (OidcRegisteredService) registeredService;
final J2EContext context = WebUtils.getPac4jJ2EContext(request, response);
final ProfileManager manager = WebUtils.getPac4jProfileManager(request, response);
final Optional<UserProfile> profile = manager.get(true);
LOGGER.debug("Attempting to produce claims for the id token [{}]", accessTokenId);
final JwtClaims claims = produceIdTokenClaims(request, accessTokenId, timeout,
oidcRegisteredService, profile.get(), context, responseType);
LOGGER.debug("Produce claims for the id token [{}] as [{}]", accessTokenId, claims);
return this.signingService.encode(oidcRegisteredService, claims);
}
示例14: handleRequest
import org.pac4j.core.context.J2EContext; //导入依赖的package包/类
@GetMapping(
path = {"/oauth2.0/authorize"}
)
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
J2EContext context = WebUtils.getPac4jJ2EContext(request, response);
ProfileManager manager = WebUtils.getPac4jProfileManager(request, response);
if(this.verifyAuthorizeRequest(request) && isRequestAuthenticated(manager, context)) {
String clientId = context.getRequestParameter("client_id");
OAuthRegisteredService registeredService = this.getRegisteredServiceByClientId(clientId);
try {
RegisteredServiceAccessStrategyUtils.ensureServiceAccessIsAllowed(clientId, registeredService);
} catch (Exception var8) {
LOGGER.error(var8.getMessage(), var8);
return OAuth20Utils.produceUnauthorizedErrorView();
}
ModelAndView mv = this.consentApprovalViewResolver.resolve(context, registeredService);
return !mv.isEmpty() && mv.hasView()?mv:this.redirectToCallbackRedirectUrl(manager, registeredService, context, clientId);
} else {
LOGGER.error("Authorize request verification failed");
return OAuth20Utils.produceUnauthorizedErrorView();
}
}
示例15: removeCookie
import org.pac4j.core.context.J2EContext; //导入依赖的package包/类
private void removeCookie(J2EContext context) {
Cookie cookie = new Cookie(ticketGrantingTicketCookieGenerator.getCookieName(), null); // Not necessary, but saves bandwidth.
cookie.setPath(ticketGrantingTicketCookieGenerator.getCookiePath());
cookie.setHttpOnly(true);
cookie.setSecure(true);
cookie.setMaxAge(0);
context.getResponse().addCookie(cookie);
}