📄 identity.java
字号:
/*
* 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -