【问题标题】:Pac4j indirect client selectionPac4j 间接客户端选择
【发布时间】:2022-06-18 02:54:03
【问题描述】:

我想让用户在几个(间接)身份验证选项之间进行选择,类似于 stackoverflow 和许多其他网站提供的选项。将有一个表单登录以及 OIDC 选项。我可以通过设置受不同间接客户端保护的不同端点来做到这一点,就像在jee-pac4j-demo 中一样,但是我不确定如何处理原始请求的 URL。

这是我的配置:

    <!-- form authentication -->
    <filter>
      <filter-name>FormFilter</filter-name>
      <filter-class>org.pac4j.jee.filter.SecurityFilter</filter-class>
      <init-param>
        <param-name>configFactory</param-name>
        <param-value>com.mycompany.authentication.Pac4jConfigFactory</param-value>
      </init-param>
      <init-param>
        <param-name>clients</param-name>
        <param-value>FormClient</param-value>
      </init-param>
      <init-param>
        <param-name>authorizers</param-name>
        <param-value>isAuthenticated</param-value>
      </init-param>
    </filter>
    <filter-mapping>
      <filter-name>FormFilter</filter-name>
      <url-pattern>/formLogin</url-pattern>
      <url-pattern>/private-url</url-pattern>
      <dispatcher>REQUEST</dispatcher>
    </filter-mapping>

    <!-- google oidc authentication -->
    <filter>
      <filter-name>GoogleOidcFilter</filter-name>
      <filter-class>org.pac4j.jee.filter.SecurityFilter</filter-class>
      <init-param>
        <param-name>configFactory</param-name>
        <param-value>com.mycompany.authentication.Pac4jConfigFactory</param-value>
      </init-param>
      <init-param>
        <param-name>clients</param-name>
        <param-value>GoogleOidcClient</param-value>
      </init-param>
      <init-param>
        <param-name>authorizers</param-name>
        <param-value>isAuthenticated</param-value>
      </init-param>
    </filter>
    <filter-mapping>
      <filter-name>GoogleOidcFilter</filter-name>
      <url-pattern>/googleOidcLogin</url-pattern>
      <dispatcher>REQUEST</dispatcher>
    </filter-mapping>

如果用户调用受保护的 url (/private-url),他将被重定向到我的 FormClient 登录页面,其中我有用户名/密码字段以及指向 /googleOidcLogin 的链接。

如果用户使用用户名/密码登录,一切都很好,并且在身份验证后提供初始请求。但是,如果用户点击 googleOidcLogin 按钮,现在该页面将被视为初始请求,并且是身份验证后恢复的页面。

如何用 pac4j 实现?

【问题讨论】:

    标签: java authentication openid-connect pac4j


    【解决方案1】:

    好的,我找到了实现这个的方法。

    由于在我们可以选择任何其他IndirectClients 之前我们总是会去FormClient,所以我们需要做的就是指示其他IndirectClients 不要保存初始请求,因此不覆盖FormClient记录的那个。这样,当另一个IndirectClient恢复初始url时,它会恢复FormClient保存的那个。

    为此,我们需要 SavedRequestHandler 的自定义实现,它只存储 FormClient 的原始 url。

    FormClientOnlySavedRequestHandler:

    public class FormClientOnlySavedRequestHandler extends DefaultSavedRequestHandler {
    
        @Override
        public void save(WebContext webContext, SessionStore sessionStore) {
            // if oidcLogin, don't save anything, we will reuse the previous one (from form client)
            if (webContext.getPath().endsWith("/oidcLogin"))
                return;
    
            super.save(webContext, sessionStore);
        }
        
    }
    

    然后你在你的ConfigFactory注册它:

            final Config config = new Config(clients);
    
            DefaultCallbackLogic callbackLogic = new DefaultCallbackLogic();
            callbackLogic.setSavedRequestHandler(new FormClientOnlySavedRequestHandler());
            config.setCallbackLogic(callbackLogic);
    
            DefaultSecurityLogic securityLogic = new DefaultSecurityLogic();
            securityLogic.setSavedRequestHandler(new FormClientOnlySavedRequestHandler());
            config.setSecurityLogic(securityLogic);
    
            return config;
    

    【讨论】:

      猜你喜欢
      • 2021-09-26
      • 1970-01-01
      • 1970-01-01
      • 2021-01-05
      • 2015-04-06
      • 1970-01-01
      • 2020-10-30
      • 2017-03-20
      • 1970-01-01
      相关资源
      最近更新 更多