SSH實現登陸攔截器

來源:互聯網
上載者:User

標籤:util   ext.get   get   void   相關   text   log   efault   struts   

/** * 登入驗證攔截器 * */@SuppressWarnings("serial")public class LoginInteceptor implements Interceptor {    @Override    public void destroy() {        // TODO Auto-generated method stub    }    @Override    public void init() {        // TODO Auto-generated method stub    }    /**每次訪問Action類之前,先執行intercept方法*/    @Override    public String intercept(ActionInvocation invocation) throws Exception {        //擷取當前訪問Action的URL        String actionName = invocation.getProxy().getActionName();        //如果當前訪問Action的URL是"loginAction_login"(根據自己寫的來定)表示此時還沒有Sesion,需要允許存取        if(!"loginAction_login".equals(actionName)){            //從Session中擷取目前使用者對象            Employee employee = SessionContext.get();            //如果Session不存在,跳轉到登入頁面            if(employee==null){                return "login";//這個login已經在struts.xml的全域結果檢視中配置了            }        }        //允許存取,訪問Action類中方法        return invocation.invoke();            }}

 

 

struts.xml中相關配置:

<!-- 攔截器配置 -->        <interceptors>            <!-- 定義了一個用於攔截器登入的攔截器 -->            <interceptor name="loginInterceptor" class="cn.itcast.ssh.utils.LoginInteceptor"></interceptor>            <!-- 定義一個攔截器棧 -->            <interceptor-stack name="systemStack">                <interceptor-ref name="defaultStack" />                <interceptor-ref name="loginInterceptor" />            </interceptor-stack>        </interceptors>              <!-- 定義系統預設攔截器 全域 -->        <default-interceptor-ref name="systemStack" />         <!-- 全域結果檢視 -->        <global-results>            <result name="login" type="redirect">                login.jsp            </result>        </global-results>

就這樣攔截器就實現了

SSH實現登陸攔截器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.