简体   繁体   中英

Intercepting j_security_check on Tomcat to modify username for passion to LDAP in Struts 2

I need to modify the username on the login form before it is passed on to LDAP for authentication. Application is struts-based. I need to remove some characters from the j_username before it is passed on to LDAP. Whats is the best way of doing it, javascript or writing a filter or interceptors in Struts? Can we make a login request to come to filter before it goes to j_security_check ?

Here are the details:

login.jsp :

<form name="loginForm" method="POST" action='<%= response.encodeURL("j_security_check") %>'>
<table>
<tr><td>User Name</td><td><input type="text" name="j_username" size="10" maxlength="30"></td></tr>
<tr><td>Password</td><td><input type="password" name="j_password" size="10" maxlength="30"></td></tr>
<tr><td>&nbsp;</td><td><input type="submit" value="Login" name="j_security_check"></td></td></tr>
</table>
</form>

web.xml :

<login-config>
        <auth-method>FORM</auth-method>
        <realm-name>Form-Based Authentication Area</realm-name>
        <form-login-config>
          <!--  form-login-page>/login.jsp</form-login-page -->
          <form-login-page>/WEB-INF/login.jsp</form-login-page>
          <form-error-page>/WEB-INF/login.jsp?result=false</form-error-page>
        </form-login-config>
    </login-config>

context.xml :

 <Realm className="org.apache.catalina.realm.JNDIRealm"
         allRolesMode="authOnly"
         connectionURL="ldap://localhost:9002"
         connectionName="CN..."
         connectionPassword=""
         userPattern="--"/>

The form action j_security_check is handled by the server before any filter invoked. So you can't intercept it in Struts 2.

If you need to modify some parameters passed with the request, you can set the form action to Struts action. Once Struts action is invoked you will get all parameters in the ActionContext . You can write a custom interceptor and add it to the action config.

The interceptor can return a result to redirect to the j_security_check with parameters that you already modified.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM