在Struts 2中使用的身份验证会话管理 [英] Session management using authentication in Struts 2

查看:391
本文介绍了在Struts 2中使用的身份验证会话管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的地方登录一个应用程序,它带我到下一个页面,显示的表。

我已经通过实施 SessionAware 接口处理会话。
当我刷新页面在浏览器或F5刷新按钮,它工作正常。不过,如果我preSS的URL输入,它需要我到登录页面,因为用户名和密码

请在下面找到我的Java code和JSP code和的struts.xml

Java类:

公共字符串认证()
{
    //返回userInfo.getUserLoginId()+_BUSINESS_SERVICES;
    如果(为j_username == NULL)
    {
        状态=failure3;
        返回状态;
    }
    的System.out.println(获取用户名和密码获得+为j_username +为j_password);
    //如果存储在会话没有用户名,
    //检查输入的用户名和密码
    如果(为j_username = NULL&放大器;!&安培;!为j_password = NULL){
         的System.out.println(内部函数);
        //添加用户名会话
        sessionMap.put(为j_username,为j_username);
        状态= otlAuthenticationController.loginAuthentication(为j_username,为j_password);
        如果(状态==成功)
        {
            this.otlUserList = otlAuthenticationController.obtainList();
            的System.out.println(大小为+ otlUserList.size());        }    }
    返回状态;
}公共字符串注销()
{
    如果(sessionMap的instanceof org.apache.struts2.dispatcher.SessionMap){
        尝试{
            //((org.apache.struts2.dispatcher.SessionMap)sessionMap).invalidate();
            如果(sessionMap.containsKey(username的)){
            sessionMap.remove(为j_username);
            的System.out.println(会议杀);
            状态=成功;
            }
        }赶上(IllegalStateException异常五){
            e.printStackTrace();
        }
    }
    返回成功;
}

JSP页面:

< D​​IV CLASS =标签的风格=的margin-top:10px的;>< S:文本NAME =login.password/>< / DIV><输入ID =为j_passwordNAME =为j_passwordTYPE =密码占位符=密码大小=31自动完成=关闭/&GT ;< BR>< BR>
< D​​IV CLASS =左的风格=填充左:150像素;水平对齐:权利;文本对齐:中心;><输入类型=提交名称=登录值=< S :文本名称=登陆/>类=按钮正常正常1的onClick =validate1()/>< / DIV>&安培; NBSP;
   < BR> < BR>< S:如果测试=状态=='失败'>
  <中心及GT;< p =风格的颜色:红色的text-align:中心>< B>无效的用户名。请输入有效的用户名和LT; / B>< / P>< /中心及GT;
< / S:如果>
< S:如果测试=状态=='failure2'>
<中心及GT;< p =风格的颜色:红色的text-align:中心>< B>无效的密码。请输入有效的密码和LT; / B>< / P>< /中心及GT;
< / S:如果>
< S:如果测试=状态=='failure3'>
  <中心及GT;< p =风格的颜色:红色的text-align:中心>< B>登录到应用程序继续< / B>< / P>< /中心及GT;
< / S:如果>
< / DIV>

struts.xml中:

<作用NAME =注销级=com.opentext.planning.view.OTLAuthentication方法=注销>
       &LT;结果名称=成功&GT; /WEB-INF/index.jsp< /&结果GT;
       &LT;结果名称=失败&GT; /WEB-INF/error.jsp< /&结果GT;
&LT; /作用&gt;
&lt;作用NAME =Otl_Homepage
    类=com.opentext.planning.view.OTLAuthentication方法=验证&GT;
    &LT;结果名称=成功&GT; /WEB-INF/Otl_Homepage.jsp< /&结果GT;
   &LT;结果名称=失败&GT; /WEB-INF/index.jsp< /&结果GT;
    &LT;结果名称=failure2&GT; /WEB-INF/index.jsp< /&结果GT;
    &LT;结果名称=failure3&GT; /WEB-INF/index.jsp< /&结果GT;
&LT; /作用&gt;


解决方案

所需的参数是在动作方面,但如果您使用的是行动,而由于某种原因没有得到从 PARAMS 拦截器,那么你仍然可以得到的参数,如果你的动作实现<一个href=\"http://struts.apache.org/maven/struts2-core/apidocs/org/apache/struts2/interceptor/ParameterAware.html\"相对=nofollow> ParameterAware

 公共类OLTAuthentication实现ParameterAware {私人地图&LT;字符串,字符串[]&GT;参数;公共无效setParameters(地图&LT;字符串,字符串[]&GT;参数){
  this.parameters =参数;
}公共字符串认证(){
    串为j_username = parameters.get(为j_username)[0];
    串为j_password = parameters.get(为j_password)[0];
    ...

I have an application where i login and it takes me to the next page and displays a table.

I have handled session by implementing the SessionAware interface. When i refresh the page with the refresh button on the browser or F5, it works fine. However, if i press enter on the url, it takes me to the login page because the username and password is null.

Please find below my java code and jsp code and struts.xml

JAVA class:

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 page:

<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>

解决方案

The required parameters are in the action context, but if you are using the action, which for some reason doesn't get parameters from the params interceptor, then you still can get parameters if your action implements 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];
    ...

这篇关于在Struts 2中使用的身份验证会话管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆