⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 identity.java.svn-base

📁 一些公用的Java函数
💻 SVN-BASE
字号:
/* * Identity.java *  * Created on 2007-10-30, 10:48:13 *  * To change this template, choose Tools | Templates * and open the template in the editor. */package com.s7turn.jaas;import java.security.Principal;import javax.el.ELContext;import javax.faces.context.ExternalContext;import javax.faces.context.FacesContext;import javax.servlet.ServletRequest;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * * @author Long */public abstract class Identity {    ///facecontext    public static Identity getCurrentIdentity()    {        FacesContext fctx = FacesContext.getCurrentInstance();        if( fctx != null )        {            ELContext elCtx = fctx.getELContext();            if( elCtx != null )            {                return (Identity) elCtx.getELResolver().getValue( elCtx, null, "identity" );            }        }        return null;    }        public static Identity getCurrentIdentity( ServletRequest request )    {        Identity identity = null;        if( request instanceof HttpServletRequest )        {            HttpServletRequest hsr = (HttpServletRequest) request;            identity = (Identity) hsr.getSession().getAttribute("identity");            if( identity == null )            {                ////load identity from cookie. we will get the token from cookie and parse it as well.                Cookie[] cookies = hsr.getCookies();                String token = null;                if( cookies != null && cookies.length > 0 )                {                    for( Cookie ck : cookies )                    {                        if( "s7turn_token".equals( ck.getName() ) )                        {                            token = ck.getValue();                            break;                        }                    }                }                                 if( token == null || token.trim().length() == 0 )                {                    ///So we will check the url from the action, that will contain the token.                    token = hsr.getParameter("s7turn_token");                }                                if( token != null && token.trim().length() > 0 )                {                    SecurityProvider provider = SecurityHelper.getInstance().getSecurityProvider();                    identity = provider.createIdentity();                     if( identity.parseLogin(token) )                    {                        ///identity.commit(age);                        ////Add the identity to session                        hsr.getSession().setAttribute("identity", identity );                    }                    ///then parse the token and login it.                }            }        }        return identity;    }        public boolean isUserInRole( String regex )    {        return SecurityHelper.getInstance().getSecurityProvider().isUserInRole(this, regex);    }        ////this method parse the token as perform login.    protected boolean parseLogin( String loginToken )    {        return false;    }        public boolean isLoggedIn()    {        return getMember() != null;    }        public String getUserName()    {        return isLoggedIn() ? getMember().getName() : "guest";    }        public abstract Principal getMember();        /**     * this method check the login user has the op permission of the resource     *      * @param res     * @param op     * @return     */    public abstract boolean hasPermission( String res, String op );        ////This method is for JSF method, that will be called after the chain ended.    public String login()    {        ///So, we can get the JSF Context.        /// and call commit method save some information into cookie.        commit( 24 * 60 * 60 * 1000 );        return "loggedIn";    }        public String logout()    {        commit( -1 );        return "loggedOut";    }        ////below token method will be used to save the security identity    ////between system and client.    public String getTokenName()    {        return "s7turn_token"; ///    }        public String getToken()    {        ////the token will combie the user's identity information and         return "";    }        ///commit this login section.    protected void commit( int age )    {        FacesContext fctx = FacesContext.getCurrentInstance();        if( fctx != null )        {            ExternalContext ectx = fctx.getExternalContext();            if( ectx != null )            {                Object rsp =  ectx.getResponse();                if( rsp instanceof HttpServletResponse )                {                    HttpServletResponse response = (HttpServletResponse) rsp;                    Cookie cookie = new Cookie( getTokenName(), this.getToken() );                    //cookie.setDomain( "" );                    //URL url = new URL( buf.toString() );                    //cookie.setPath( ectx.getRequestContextPath() );                    cookie.setSecure( ((HttpServletRequest) ectx.getRequest()).isSecure() );                    cookie.setMaxAge( age );                    response.addCookie( cookie );                }            }        }    }        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -