Ejemplo n.º 1
0
 /* 无需做链接,这是OpenID的回调地址 */
 @RequiresGuest
 @At("/login/?/callback")
 public View returnPoint(String providerId, HttpServletRequest request, HttpSession session)
     throws Exception {
   SocialAuthManager manager = (SocialAuthManager) session.getAttribute("openid.manager");
   if (manager == null) throw new SocialAuthException("Not manager found!");
   session.removeAttribute("openid.manager"); // 防止重复登录的可能性
   Map<String, String> paramsMap = SocialAuthUtil.getRequestParametersMap(request);
   AuthProvider provider = manager.connect(paramsMap);
   Profile p = provider.getUserProfile();
   Subject currentUser = SecurityUtils.getSubject();
   ThreadContext.bind(currentUser);
   OAuthToken token = new OAuthToken(p, request.getRemoteAddr());
   try {
     currentUser.login(token);
   } catch (UnknownAccountException uae) {
     return new ViewWrapper(new ForwardView("/admin/index"), "帐号不存在");
   } catch (IncorrectCredentialsException ice) {
     return new ViewWrapper(new ForwardView("/admin/index"), "证书验证失败");
   } catch (LockedAccountException lae) {
     return new ViewWrapper(new ForwardView("/admin/index"), "帐号已被锁定");
   } catch (ExcessiveAttemptsException eae) {
     return new ViewWrapper(new ForwardView("/admin/index"), "尝试的次数太多");
   } catch (AuthenticationException ae) {
     return new ViewWrapper(new ForwardView("/admin/index"), ae.getMessage());
   }
   return new ViewWrapper(new ServerRedirectView("/admin/main.rk"), null);
 }
  public void doFilter(
      final HttpServletRequest req, final HttpServletResponse res, final FilterChain fc)
      throws Exception {
    SASFHelper h =
        new DefaultSASFHelper(req, this.props, this.sdbSocialAuthManager, req.getSession());
    String path = lookupPath(req);
    if (path != null && path.startsWith(h.getServletMain())) {
      try {
        if (path.equals(h.getServletSuccess())) {
          SocialAuthManager manager = h.getAuthManager();
          AuthProvider provider = manager.connect(SocialAuthUtil.getRequestParametersMap(req));
          h.setProvider(provider);
          res.sendRedirect(h.getWebappSuccessAction());
          return;
        } else {
          String id = req.getParameter("id");
          SocialAuthManager socialAuthManager = null;
          synchronized (req.getSession()) {
            if (h.getAuthManager() != null) {
              socialAuthManager = h.getAuthManager();
            } else {
              socialAuthManager = h.getMgr().getSocialAuthManager();
              h.setAuthManager(socialAuthManager);
            }
          }

          res.sendRedirect(socialAuthManager.getAuthenticationUrl(id, h.getOpenidReturnUrl()));
          return;
        }
      } catch (Throwable t) {
        h.setError(t.getMessage(), t);
        res.sendRedirect(h.getErrorPage());
        return;
      }
    }
    if (!res.isCommitted()) {
      fc.doFilter(req, res);
    }
  }