identity.java

来自「java写的blog」· Java 代码 · 共 56 行

JAVA
56
字号
/*
 * Created on 2004-10-2
 * Author: Xuefeng, Copyright (C) 2004, Xuefeng.
 */
package org.crystalblog.web;

import javax.servlet.http.*;

import org.crystalblog.exception.*;

/**
 * Identity class uses in the session to identify the user.
 * 
 * @author Xuefeng
 */
public class Identity implements java.io.Serializable {

    public static final String IDENTITY = "s_identity";

    public static final Identity ANONYMOUS = new Identity((-1), null);

    private int accountId;
    private String username;

    public Identity(int accountId, String username) {
        this.accountId = accountId;
        this.username = username;
    }

    public boolean isAnonymous() { return username==null; }
    public boolean isLogin() { return username!=null; }

    public int getAccountId() { return accountId; }
    public String getUsername() { return username==null ? "guest" : username; }

    public boolean isAdmin() { return "admin".equals(username); }

    /**
     * Get the identity of the session.
     * 
     * @param request The http request.
     * @return The Identity of the login user.
     * @throws AuthorizationException If not login.
     */
    public static Identity getIdentity(HttpServletRequest request) {
        Object obj = request.getSession().getAttribute(IDENTITY);
        if(obj==null)
            throw new AuthorizationException("Authorization failed: Anonymous user.");
        Identity id = (Identity)obj;
        if(id.isAnonymous())
            throw new AuthorizationException("Authorization failed: Anonymous user.");
        return id;
    }

}

⌨️ 快捷键说明

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