org.jooby.Request.ifSession()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(6.1k)|赞(0)|评价(0)|浏览(618)

本文整理了Java中org.jooby.Request.ifSession方法的一些代码示例,展示了Request.ifSession的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Request.ifSession方法的具体详情如下:
包路径:org.jooby.Request
类名称:Request
方法名:ifSession

Request.ifSession介绍

暂无

代码示例

代码示例来源:origin: jooby-project/jooby

  1. @Override
  2. public Optional<Session> ifSession() {
  3. return req.ifSession();
  4. }

代码示例来源:origin: jooby-project/jooby

  1. @Override public boolean destroySession(WebContext ctx) {
  2. req.ifSession().ifPresent(Session::destroy);
  3. return true;
  4. }

代码示例来源:origin: jooby-project/jooby

  1. @Override public boolean renewSession(WebContext ctx) {
  2. req.ifSession().ifPresent(Session::renewId);
  3. return true;
  4. }

代码示例来源:origin: jooby-project/jooby

  1. @Override public Object get(WebContext context, String key) {
  2. return req.ifSession()
  3. .map(session -> {
  4. String value = session.get(key).toOptional().orElse(null);
  5. return strToObject(value);
  6. }).orElse(null);
  7. }

代码示例来源:origin: jooby-project/jooby

  1. static Provider profileProvider(AtomicReference<Registry> registry, Class profile,
  2. Function<Request, UserProfile> unauthenticated) {
  3. return () -> {
  4. Request req = registry.get().require(Request.class);
  5. ProfileManager pm = req.require(ProfileManager.class);
  6. Object result = pm.getAll(req.ifSession().isPresent()).stream()
  7. .filter(profile::isInstance)
  8. .findFirst()
  9. .orElse(null);
  10. if (result == null) {
  11. if (unauthenticated == null) {
  12. throw new Err(Status.FORBIDDEN, "Not found: " + profile.getSimpleName());
  13. }
  14. result = unauthenticated.apply(req);
  15. }
  16. return result;
  17. };
  18. }
  19. }

代码示例来源:origin: jooby-project/jooby

  1. @Override public void set(WebContext context, String key, Object value) {
  2. if (value == null) {
  3. req.ifSession().ifPresent(session -> session.unset(key));
  4. } else {
  5. req.session().set(key, objToStr(value));
  6. }
  7. }

代码示例来源:origin: jooby-project/jooby

  1. @SuppressWarnings("unchecked")
  2. @Override
  3. public void handle(final Request req, final Response rsp) throws Throwable {
  4. // DON'T create a session for JWT/param/header auth (a.k.a stateless)
  5. Optional<Session> ifSession = req.ifSession();
  6. if (ifSession.isPresent()) {
  7. Session session = ifSession.get();
  8. Optional<String> profileId = session.unset(Auth.ID).toOptional();
  9. if (profileId.isPresent()) {
  10. Optional<CommonProfile> profile = req.require(AuthStore.class).unset(profileId.get());
  11. log.debug("logout {}", profile);
  12. session.destroy();
  13. }
  14. } else {
  15. log.debug("nothing to logout from session");
  16. }
  17. String redirectTo = req.<String> ifGet("auth.logout.redirectTo").orElse(this.redirectTo);
  18. rsp.redirect(redirectTo);
  19. }

代码示例来源:origin: jooby-project/jooby

  1. private Route.After saveCookie() {
  2. return (req, rsp, result) -> {
  3. req.ifSession().ifPresent(session -> {
  4. Optional<String> value = req.cookie(cookie.name().get()).toOptional();
  5. Map<String, String> initial = value
  6. .map(this::attributes)
  7. .orElse(Collections.emptyMap());
  8. Map<String, String> attributes = session.attributes();
  9. // is dirty?
  10. boolean dirty = !initial.equals(attributes);
  11. log.debug("session dirty: {}", dirty);
  12. if (dirty) {
  13. log.debug("saving session cookie");
  14. String encoded = Cookie.URL_ENCODER.apply(attributes);
  15. String signed = Cookie.Signature.sign(encoded, secret);
  16. rsp.cookie(new Cookie.Definition(cookie).value(signed));
  17. } else if (timeout > 0) {
  18. // touch session
  19. value.ifPresent(raw -> rsp.cookie(new Cookie.Definition(cookie).value(raw)));
  20. }
  21. });
  22. return result;
  23. };
  24. }

代码示例来源:origin: jooby-project/jooby

  1. @Override public void handle(Request req, Response rsp, Route.Chain chain) throws Throwable {
  2. try {
  3. WebContext context = req.require(WebContext.class);
  4. /** 1: don't save authentication urls: */
  5. String existingRequestedUrl = (String) context
  6. .getSessionAttribute(Pac4jConstants.REQUESTED_URL);
  7. boolean resetRequestedUrl = excludes.stream()
  8. .filter(it -> !it.endsWith("/**") && req.matches(it))
  9. .findFirst()
  10. .isPresent();
  11. conf.getSecurityLogic()
  12. .perform(context, conf, new Pac4jGrantAccessAdapter(req, rsp, chain),
  13. conf.getHttpActionAdapter(), clients, authorizers, matchers, multiProfile);
  14. /** 2: don't save authentication urls: */
  15. if (resetRequestedUrl && req.ifSession().isPresent()) {
  16. // log.info("ignoring {} by {}", ctx.g, existingRequestedUrl);
  17. context.setSessionAttribute(Pac4jConstants.REQUESTED_URL, existingRequestedUrl);
  18. }
  19. } catch (TechnicalException x) {
  20. Throwable cause = x.getCause();
  21. if (!(cause instanceof Err)) {
  22. // Pac4j wrap everything as TechnicalException, it makes stacktrace ugly, so we rethrow
  23. // Err
  24. cause = x;
  25. }
  26. throw cause;
  27. }
  28. // }
  29. }

代码示例来源:origin: jooby-project/jooby

  1. req.ifSession().ifPresent(s -> envdata.put("session", dump(s::attributes)));

代码示例来源:origin: org.jooby/jooby

  1. @Override
  2. public Optional<Session> ifSession() {
  3. return req.ifSession();
  4. }

代码示例来源:origin: org.jooby/jooby-pac4j

  1. @SuppressWarnings("unchecked")
  2. @Override
  3. public void handle(final Request req, final Response rsp) throws Throwable {
  4. // DON'T create a session for JWT/param/header auth (a.k.a stateless)
  5. Optional<Session> ifSession = req.ifSession();
  6. if (ifSession.isPresent()) {
  7. Session session = ifSession.get();
  8. Optional<String> profileId = session.unset(Auth.ID).toOptional();
  9. if (profileId.isPresent()) {
  10. Optional<CommonProfile> profile = req.require(AuthStore.class).unset(profileId.get());
  11. log.debug("logout {}", profile);
  12. session.destroy();
  13. }
  14. } else {
  15. log.debug("nothing to logout from session");
  16. }
  17. String redirectTo = req.<String> ifGet("auth.logout.redirectTo").orElse(this.redirectTo);
  18. rsp.redirect(redirectTo);
  19. }

代码示例来源:origin: org.jooby/jooby

  1. private Route.After saveCookie() {
  2. return (req, rsp, result) -> {
  3. req.ifSession().ifPresent(session -> {
  4. Optional<String> value = req.cookie(cookie.name().get()).toOptional();
  5. Map<String, String> initial = value
  6. .map(this::attributes)
  7. .orElse(Collections.emptyMap());
  8. Map<String, String> attributes = session.attributes();
  9. // is dirty?
  10. boolean dirty = !initial.equals(attributes);
  11. log.debug("session dirty: {}", dirty);
  12. if (dirty) {
  13. log.debug("saving session cookie");
  14. String encoded = Cookie.URL_ENCODER.apply(attributes);
  15. String signed = Cookie.Signature.sign(encoded, secret);
  16. rsp.cookie(new Cookie.Definition(cookie).value(signed));
  17. } else if (timeout > 0) {
  18. // touch session
  19. value.ifPresent(raw -> rsp.cookie(new Cookie.Definition(cookie).value(raw)));
  20. }
  21. });
  22. return result;
  23. };
  24. }

代码示例来源:origin: org.jooby/jooby-whoops

  1. req.ifSession().ifPresent(s -> envdata.put("session", dump(s::attributes)));

相关文章