Ozgur Dogus Ozgur Dogus - 4 months ago 10
Java Question

How to programatically login to j_security_check

I have a JSP web application which uses

j_security_check
. Is it possible to login a specific user to
j_security_check
programatically via a JSP page if I know the userid and password? I tried to pass the variables as URL parameters this way...

response.sendRedirect(www.mydomain.com/j_security_check?j_username=someUserName&j_password=somePassword )


...but it doesnt work. Is there any way to do it?

Edit:
here is my login page which works fine right now. I trimmed some of the code for security reasons.

<form name="signon" method="post" action="/j_security_check">
<table width="100%" cellpadding="4" cellspacing="0">

<tr>
<td valign="top" colspan="4">
<h2><%= UI.tr(null, "login_details") %>
</h2>
</td>
</tr>

<tr>
<td valign="top" width="150px">
<%= UI.tr(null, "login_id") %>
</td>
<td valign="top" width="150px">
<%= UI.tr(null, "login_pass") %>
</td>
<td valign="top" width="150px">
<%= UI.tr(null, "login_token_or_captcha") %>
</td>
<td width="100%">&nbsp;</td>
</tr>


<tr>

<%
if (logins == null) {
%>
<td>
<input type="hidden" name="j_uri" value="/index.jsp">
<input type="text" id="username" name="j_username" size="16" style="width: 150px;" autocomplete="off" <%= username == null ? "" : "disabled value='" + username + "'" %> onblur="return checkCaptcha();">
</td>
<%
} else {
%>
<td>
<select name="j_username" style="width: 150px;">
<%
for (Login login : logins) {
%>
<option><%= login.getUsername() %>
</option>
<%
}
%>
</select>
</td>
<%
}
%>
<td><input type="password" name="j_password" size="16" style="width: 150px;"> </td>
<td><input type="text" id="mypw" name="mypw" size="16" autocomplete="off" style="width: 150px;"></td>
<td><input class="submit" type="submit" name="submit" value="<%= UI.tr(null, "login_submit") %>"></td>
</tr>




<tr>
<td valign="top" colspan="4">
<%-- <%
if("registry.nic.hn".equals(request.getServerName())) {
%>
<!-- GARTH - put whatever you want here for .HN -->
&nbsp;
<% } else { %> --%>
<h2><%= UI.tr(null, "login_news") %>
</h2>
<div><%= HTMLFormat.addBreaks(SiteConf.getSiteConf().getNews()) %>
</div>
<%-- <% } %> --%>
</td>
</tr>








Answer

The following code works for me in the JSP file:

String url = "j_security_check?j_username=" + username + "&j_password=" + password;
String redirectUrl = response.encodeRedirectURL(url);
response.sendRedirect(redirectUrl);
Comments