【问题标题】:Session management using authentication in Struts 2在 Struts 2 中使用身份验证进行会话管理
【发布时间】:2015-07-15 07:10:35
【问题描述】:

我有一个应用程序,我可以在其中登录,它会将我带到下一页并显示一个表格。

我已经通过实现SessionAware 接口来处理会话。 当我使用浏览器上的刷新按钮或 F5 刷新页面时,它工作正常。但是,如果我在 url 上按 enter,它会将我带到登录页面,因为用户名和密码是 null

请在下面找到我的java代码和jsp代码以及struts.xml

JAVA 类:

public String Authentication()
{        
    //  return userInfo.getUserLoginId() + "_BUSINESS_SERVICES";
    if(j_username == null)
    {
        status="failure3";
        return status;
    }
    System.out.println("get username and get password" +j_username + j_password);


    // if no userName stored in the session,
    // check the entered userName and password
    if (j_username != null && j_password != null) {
         System.out.println("inside function");
        // add userName to the session
        sessionMap.put("j_username", j_username);
        status = otlAuthenticationController.loginAuthentication(j_username,j_password);
        if(status == "success")
        {
            this.otlUserList= otlAuthenticationController.obtainList();
            System.out.println("size is"+otlUserList.size());

        }

    }


    return status;
}

public String logout()
{
    if (sessionMap instanceof org.apache.struts2.dispatcher.SessionMap) {
        try {
            //((org.apache.struts2.dispatcher.SessionMap) sessionMap).invalidate();
            if (sessionMap.containsKey("userName")) {
            sessionMap.remove(j_username);
            System.out.println("session killed");
            status ="success";
            }
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }
    return "success";
}

JSP 页面:

<div class="label" style="margin-top:10px;"><s:text name="login.password" /></div><input id="j_password" name="j_password" type="password" placeholder="Password" size="31" autocomplete="off"/><br><br>
<div class="left" style="padding-left:150px; horizontal-align:right;text-align:center;"><input type="submit" name="login"  value=<s:text name="login"/> class="button normal normal1"  onClick="validate1()"/></div>&nbsp;
   <br> <br><s:if test="status=='failure'">
  <center><p style="color:RED" text-align :"center"><b>  Invalid username. Please enter a valid username</b></p></center>
</s:if>
<s:if test="status=='failure2'">
<center><p style="color:RED" text-align :"center"><b>  Invalid password. Please enter a valid password</b></p></center>
</s:if>
<s:if test="status=='failure3'">
  <center><p style="color:RED" text-align :"center"><b>  Login to the application to continue</b></p></center>
</s:if>
</div>

struts.xml:

<action name="logout" class ="com.opentext.planning.view.OTLAuthentication" method="logout">
       <result name="success">/WEB-INF/index.jsp</result>
       <result name="failure">/WEB-INF/error.jsp</result>
</action>       
<action name ="Otl_Homepage" 
    class="com.opentext.planning.view.OTLAuthentication" method="Authentication">
    <result name="success">/WEB-INF/Otl_Homepage.jsp</result>
   <result name="failure">/WEB-INF/index.jsp</result>
    <result name="failure2">/WEB-INF/index.jsp</result>
    <result name="failure3">/WEB-INF/index.jsp</result>
</action>

【问题讨论】:

  • 同一个jsp返回3个不同的失败结果有什么意义?为什么不使用 camelCase 作为变量名?为什么 JSP 代码的格式如此糟糕?为什么一个动作名称是小写的而另一个是大写的?为什么一种操作方法是小写的而另一种是大写的?为什么有时{ 是内联的,有时它是在一个新行中?伙计,这很重要……考虑改进它。顺便说一句,登录凭据应在自定义拦截器中检查,而不是在操作方法中检查,并且该拦截器应应用于除登录操作之外的每个操作...

标签: java jsp authentication parameters struts2


【解决方案1】:

所需的参数在动作上下文中,但如果你使用动作,由于某种原因没有从params拦截器获取参数,那么如果你的动作实现ParameterAware,你仍然可以获取参数。

public class OLTAuthentication implements ParameterAware {

private Map<String, String[]> parameters;

public void setParameters(Map<String, String[]> parameters){
  this.parameters = parameters;
} 

public String Authentication() {
    String j_username = parameters.get("j_username")[0];
    String j_password = parameters.get("j_password")[0];
    ...

【讨论】:

    猜你喜欢
    • 2017-03-15
    • 2015-05-22
    • 2013-06-19
    • 2019-12-28
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多