1

在我的场景中,用户需要在第一次登录之前更改密码。如何在登录页面之前使 cas 重定向 url?

在我的应用程序中,我已经定义了自己的控制器来处理某些 URL,并且还在 authenticationHandlers 下实现了我自己的自定义身份验证。

现在只留下这个机制在登录前重置密码。

谁能给我一个线索怎么做?

我的maven项目文件: https ://qeyg6a.bay.livefilestore.com/y2msQyFts1aCgGkYR4ybMses2mGFVNJBLCX_xQ9pDi8gVdnhx9P5ibVVBlg6p6T9RM3BAN8Qcz2Izo5JGdQzbyU-kSMTIwBVOYhkdG1RvsyE50/cas-server=1-webapp.rar?download&psid=1

我的登录 webflow.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--

    Licensed to Jasig under one or more contributor license
    agreements. See the NOTICE file distributed with this work
    for additional information regarding copyright ownership.
    Jasig licenses this file to you under the Apache License,
    Version 2.0 (the "License"); you may not use this file
    except in compliance with the License.  You may obtain a
    copy of the License at the following location:

      http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.

-->
<flow xmlns="http://www.springframework.org/schema/webflow"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/webflow
                          http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">

    <var name="credentials" class="org.jasig.cas.authentication.principal.UsernamePasswordCredentials" />
    <on-start>
        <evaluate expression="initialFlowSetupAction" />
    </on-start>

    <decision-state id="ticketGrantingTicketExistsCheck">
        <if test="flowScope.ticketGrantingTicketId != null" then="hasServiceCheck" else="gatewayRequestCheck" />
    </decision-state>

    <decision-state id="gatewayRequestCheck">
        <if test="requestParameters.gateway != '' and requestParameters.gateway != null and flowScope.service != null" then="gatewayServicesManagementCheck" else="serviceAuthorizationCheck" />
    </decision-state>

    <decision-state id="hasServiceCheck">
        <if test="flowScope.service != null" then="renewRequestCheck" else="viewGenericLoginSuccess" />
    </decision-state>

    <decision-state id="renewRequestCheck">
        <if test="requestParameters.renew != '' and requestParameters.renew != null" then="serviceAuthorizationCheck" else="generateServiceTicket" />
    </decision-state>

    <!-- Do a service authorization check early without the need to login first -->
    <action-state id="serviceAuthorizationCheck">
        <evaluate expression="serviceAuthorizationCheck"/>
        <transition to="generateLoginTicket"/>
    </action-state>

    <!-- 
        The "warn" action makes the determination of whether to redirect directly to the requested
        service or display the "confirmation" page to go back to the server.
    -->
    <decision-state id="warn">
        <if test="flowScope.warnCookieValue" then="showWarningView" else="redirect" />
    </decision-state>

    <!-- 
    <action-state id="startAuthenticate">
        <action bean="x509Check" />
        <transition on="success" to="sendTicketGrantingTicket" />
        <transition on="warn" to="warn" />
        <transition on="error" to="generateLoginTicket" />
    </action-state>
     -->

    <!--
        LPPE transitions begin here: You will also need to
        move over the 'lppe-configuration.xml' file from the
        'unused-spring-configuration' folder to the 'spring-configuration' folder
        so CAS can pick up the definition for the bean 'passwordPolicyAction'.
    -->
    <action-state id="passwordPolicyCheck">
        <evaluate expression="passwordPolicyAction" />
        <transition on="showWarning" to="passwordServiceCheck" />
        <transition on="success" to="sendTicketGrantingTicket" />
        <transition on="error" to="viewLoginForm" />
    </action-state>

    <action-state id="passwordServiceCheck">
        <evaluate expression="sendTicketGrantingTicketAction" />
        <transition to="passwordPostCheck" />
    </action-state>

    <decision-state id="passwordPostCheck">
        <if test="flowScope.service != null" then="warnPassRedirect" else="pwdWarningPostView" />
    </decision-state>

    <action-state id="warnPassRedirect">
        <evaluate expression="generateServiceTicketAction" />
        <transition on="success" to="pwdWarningPostView" />
        <transition on="error" to="generateLoginTicket" />
        <transition on="gateway" to="gatewayServicesManagementCheck" />
    </action-state>

    <end-state id="pwdWarningAbstractView">
        <on-entry>
            <set name="flowScope.passwordPolicyUrl" value="passwordPolicyAction.getPasswordPolicyUrl()" />
        </on-entry>
    </end-state>
    <end-state id="pwdWarningPostView" view="casWarnPassView" parent="#pwdWarningAbstractView" />
    <end-state id="casExpiredPassView" view="casExpiredPassView" parent="#pwdWarningAbstractView" />
    <end-state id="casMustChangePassView" view="casMustChangePassView" parent="#pwdWarningAbstractView" />
    <end-state id="casAccountDisabledView" view="casAccountDisabledView" />
    <end-state id="casAccountLockedView" view="casAccountLockedView" />
    <end-state id="casBadHoursView" view="casBadHoursView" />
    <end-state id="casBadWorkstationView" view="casBadWorkstationView" />
    <!-- LPPE transitions end here... -->

    <action-state id="generateLoginTicket">
        <evaluate expression="generateLoginTicketAction.generate(flowRequestContext)" />
        <transition on="generated" to="viewLoginForm" />
    </action-state>

    <view-state id="viewLoginForm" view="casLoginView" model="credentials">
        <binder>
            <binding property="username" />
            <binding property="password" />
        </binder>
        <on-entry>
            <set name="viewScope.commandName" value="'credentials'" />
        </on-entry>
        <transition on="submit" bind="true" validate="true" to="realSubmit">
            <evaluate expression="authenticationViaFormAction.doBind(flowRequestContext, flowScope.credentials)" />
        </transition>
    </view-state>

    <action-state id="realSubmit">
        <evaluate expression="authenticationViaFormAction.submit(flowRequestContext, flowScope.credentials, messageContext)" />
        <!--
          To enable LPPE on the 'warn' replace the below transition with:
          <transition on="warn" to="passwordPolicyCheck" />

          CAS will attempt to transition to the 'warn' when there's a 'renew' parameter
          and there exists a ticketGrantingId and a service for the incoming request.
        -->
        <transition on="warn" to="warn" />
        <!--
          To enable LPPE on the 'success' replace the below transition with:
          <transition on="success" to="passwordPolicyCheck" />
        -->
        <transition on="success" to="sendTicketGrantingTicket" />
        <transition on="error" to="generateLoginTicket" />
        <transition on="accountDisabled" to="casAccountDisabledView" />
        <transition on="mustChangePassword" to="casMustChangePassView" />
        <transition on="accountLocked" to="casAccountLockedView" />
        <transition on="badHours" to="casBadHoursView" />
        <transition on="badWorkstation" to="casBadWorkstationView" />
        <transition on="passwordExpired" to="casExpiredPassView" />
    </action-state>

    <action-state id="sendTicketGrantingTicket">
        <evaluate expression="sendTicketGrantingTicketAction" />
        <transition to="serviceCheck" />
    </action-state>

    <decision-state id="serviceCheck">
        <if test="flowScope.service != null" then="generateServiceTicket" else="viewGenericLoginSuccess" />
    </decision-state>

    <action-state id="generateServiceTicket">
        <evaluate expression="generateServiceTicketAction" />
        <transition on="success" to ="warn" />
        <transition on="error" to="generateLoginTicket" />
        <transition on="gateway" to="gatewayServicesManagementCheck" />
    </action-state>

    <action-state id="gatewayServicesManagementCheck">
        <evaluate expression="gatewayServicesManagementCheck" />
        <transition on="success" to="redirect" />
    </action-state>

    <action-state id="redirect">
        <evaluate expression="flowScope.service.getResponse(requestScope.serviceTicketId)" result-type="org.jasig.cas.authentication.principal.Response" result="requestScope.response" />
        <transition to="postRedirectDecision" />
    </action-state>

    <decision-state id="postRedirectDecision">
        <if test="requestScope.response.responseType.name() == 'POST'" then="postView" else="redirectView" />
    </decision-state>

    <!-- 
        the "viewGenericLogin" is the end state for when a user attempts to login without coming directly from a service.
        They have only initialized their single-sign on session.
    -->
    <end-state id="viewGenericLoginSuccess" view="casLoginGenericSuccessView" />

    <!-- 
        The "showWarningView" end state is the end state for when the user has requested privacy settings (to be "warned") to be turned on.  It delegates to a 
        view defines in default_views.properties that display the "Please click here to go to the service." message.
    -->
    <end-state id="showWarningView" view="casLoginConfirmView" />

    <end-state id="postView" view="postResponseView">
        <on-entry>
            <set name="requestScope.parameters" value="requestScope.response.attributes" />
            <set name="requestScope.originalUrl" value="flowScope.service.id" />
        </on-entry>
    </end-state>

    <!-- 
        The "redirect" end state allows CAS to properly end the workflow while still redirecting
        the user back to the service required.
    -->
    <end-state id="redirectView" view="externalRedirect:${requestScope.response.url}" />

    <end-state id="viewServiceErrorView" view="viewServiceErrorView" />

    <end-state id="viewServiceSsoErrorView" view="viewServiceSsoErrorView" />

    <global-transitions>
        <!-- CAS-1023 This one is simple - redirects to a login page (same as renew) when 'ssoEnabled' flag is unchecked
             instead of showing an intermediate unauthorized view with a link to login page -->
        <transition to="viewLoginForm" on-exception="org.jasig.cas.services.UnauthorizedSsoServiceException"/>
        <transition to="viewServiceErrorView" on-exception="org.springframework.webflow.execution.repository.NoSuchFlowExecutionException" />
        <transition to="viewServiceErrorView" on-exception="org.jasig.cas.services.UnauthorizedServiceException" />
    </global-transitions>
</flow>

我的解决方案

  1. 创造
resetPasswordView.(class)=org.springframework.web.servlet.view.JstlView
resetPasswordView.url=/WEB-INF/view/jsp/default/ui/ResetPassword.jsp

在 default_views.properties

  1. 创造
public class testflowAction extends AbstractAction{
    private String origin;

    @Override
    protected Event doExecute(RequestContext rc) throws Exception {
        origin = rc.getRequestParameters().get("service");
        if(true)
            return result("setNew");
        else
            return result("setNew");
    }

}
  1. 在 login-wenflow.xml 中,替换

<transition on="success" to="sendTicketGrantingTicket" /><transition on="success" to="customFlowCheck" />

还添加

<action-state id="customFlowCheck">
        <evaluate expression="customCheckAction" />
  <transition on="ok" to="sendTicketGrantingTicket" />
  <transition on="setNew" to="resetPasswordView" />
</action-state>

<end-state id="resetPasswordView" view="resetPasswordView" />
4

3 回答 3

1

我们公司也有类似的问题。成功登录后,用户必须被重定向到另一个页面。我们决定从动作状态 ID“realSubmit”添加另一个结果,这将表明流程的继续。我建议您首先让用户进行登录,以便他确认他是正确的用户。为此,您需要更改 AuthenticationViaFormAction 类中的提交方法。然后您需要销毁 TGT,并将用户重定向到重置密码的自定义流程操作。它看起来像这样:

    public final String submit(final RequestContext context, final Credentials credentials, final MessageContext messageContext) throws Exception {
    (...)
    //Login user, so we know that provided password is correct.
     WebUtils.putTicketGrantingTicketInRequestScope(context, this.centralAuthenticationService.createTicketGrantingTicket(credentials));
    // do custom logic, that check if it is first login attept:
    if( firstLoginAttempt(credentials) {
      //destroy TGT, so user won't stay logged-in after interrupting next step
      centralAuthenticationService.destroyTicketGrantingTicket(ticketGrantingTicketId);
      //redirect user to another action that will handle changing password
      return "changePassRequired"
    }

我不知道你如何检查密码是否是初始密码,所以我把它留给你。接下来在 login-webflow.xml 结果中添加转换:

    <action-state id="realSubmit">
    <evaluate expression="authenticationViaFormAction.submit(flowRequestContext,flowScope.credentials, messageContext)" />
    <transition on="warn" to="warn" />
    <transition on="success" to="sendTicketGrantingTicket" />
    <transition on="error" to="generateLoginTicket" />
    <transition on="accountDisabled" to="casAccountDisabledView" />
    <transition on="mustChangePassword" to="casMustChangePassView" />
    <transition on="accountLocked" to="casAccountLockedView" />
    <transition on="badHours" to="casBadHoursView" />
    <transition on="badWorkstation" to="casBadWorkstationView" />
    <transition on="passwordExpired" to="casExpiredPassView" />
    <transition on="changePassRequired" to="changePasswordPrepare" />
</action-state>

并添加自定义操作,您不妨使用与登录操作相同的表单,将其分为两种状态,例如:

<view-state id="changePasswordPrepare" view="resetPasswordView">
    <on-entry>
        <evaluate expression="resetPasswordAction.setupForm(flowRequestContext)" />
    </on-entry>
    <transition on="submit" bind="true" validate="true" to="resetPasswordRealAction">
    </transition>
</view-state>
<action-state id="resetPasswordRealAction">
    <evaluate expression="resetPasswordAction.submit(flowRequestContext)" />
    <transition on="success" to="sendTicketGrantingTicket" />
    <transition on="again" to="changePasswordPrepare" />
    <transition on="error" to="generateLoginTicket" />       
</action-state>

如果需要,resetPasswordAction.setupForm 将准备带有重置密码表单的自定义视图 resetPasswordAction.submit 将从您的表单中获取变量,并更改密码。类的片段可能看起来像这样:

public class resetPasswordAction extends FormAction {
  public final String submit(final RequestContext context) throws Exception {
    final Credentials previousCredentials = context.getFlowScope().get("loginCredentials");
    final String oldPass = context.getConversationScope().get("pass");
    final String newPassword = context.getConversationScope().get("password");
    final String newPasswordagain = context.getConversationScope().get("passwordagain");
    if (!newPassword.equals(newPasswordagain) {
      return "again";
    }
    if ( everythingIsOk(previousCredentials.getUsername(),oldPass,newPassword)) {
      Credentials credentials = new UsernamePasswordCredentials();
      credentials.setUsername(previousCredentials.getUsername());
      credentials.setPassword(newPassword);
      WebUtils.putTicketGrantingTicketInRequestScope(context, this.casService.createTicketGrantingTicket(credentials));
      return "success";
    }
    return "error";
  }
  public Event setupForm(RequestContext context) throws Exception {
    final Locale locale = LocaleContextHolder.getLocale();
    context.getFlashScope().put("language",locale);
    context.getFlashScope().put("service",context.getRequestScope().get("service");
  }
}

您可能会对CAM - CAS 帐户管理模块感兴趣,尽管它似乎处于早期的理论阶段。

您可能对cas 4感兴趣,现在处于 beta 版本,因为它似乎包含更多弹性身份验证逻辑。一,您可以在登录密码重置后使用。

于 2013-12-27T22:38:58.693 回答
0

您可以在成功登录时添加一个 JS 脚本,从您的特定 URL 重定向一个

document.location.href =“http://www....”

您可以个性化您的处理 JS。

于 2013-11-29T16:35:54.530 回答
0

我已经运行了一个 CAS 服务器,看来您应该通过 Spring WebFlow 进行自定义。按照我的胶水:

    <action-state id="validateForgotPassword">
    <on-entry>
        <set name="flashScope.pmTask" value="'forceChangePassword'"/>
    </on-entry>

    <evaluate expression="forgotPasswordTokenValidateAction" />
    <transition on="success" to="passwordManager"/>
    <transition on="error" to="badForgotPasswordTokenView" />
</action-state>
于 2013-11-21T12:11:54.977 回答