我想將Apache Shiro封裝在Servlet環境中。我想創建MySecurityUtils並在靜態方法中使用Shiro SecurityUtils.getSubject。我的問題是這是否是在靜態方法中使用SecurityUtils.getSubject方法的正確方法。這會導致多線程servlet環境中的任何問題嗎?封裝Shiro主題
MySecurityUtils.java
import org.apache.shiro.subject.Subject;
import org.apache.shiro.SecurityUtils;
public class MySecurityUtils {
public static MyUser getUser() {
Subject currentUser = SecurityUtils.getSubject();
MyUser myUser = new MyUser(currentUser);
...
}
}
MyUser.java
public class MyUser {
// ... constructors
public boolean isPermitted(..) {subject.isPermitted(...)}
}
我想添加額外的應用程序特定檢查到MyUser對象。 MyUser將是不可變的,Subject方法的方法將通過delagate方法訪問。我認爲在那種情況下,它會沒事的或? – 2013-03-12 15:10:12
由於您每次都從您的MySecurityUtils類返回一個新實例,因此請注意,如果您需要每次請求獲取用戶多次。您將在不同的'MyUser'實例上運行。 – 2013-03-12 15:26:52