Spring 安全:如何在 applicationContext.xml 中获得授权用户
Spring Security: how to get an authorized user in applicationContext.xml
对不起我的英语。如何在 applicationContext.xml
中获得授权用户
Authentication
class:
public class Authentication {
public Account getAccount(){
return (Account) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
}
}
并且在文件 applicationContext.xml 中:
<bean id="Authentication" class="com.otv.util.Authentication">
</bean>
<bean id="CurrentAccount"
factory-bean="Authentication"
factory-method="getAccount"/>
但它不起作用:
Exception while loading the app : java.lang.IllegalStateException:
ContainerBase.addChild: start: org.apache.catalina.LifecycleException:
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'Principal' defined in ServletContext resource
[/WEB-INF/applicationContext.xml]: Instantiation of bean failed;
nested exception is
org.springframework.beans.factory.BeanDefinitionStoreException:
Factory method [public com.otv.model.entity.Account
com.otv.util.Authentication.getAccount()] threw exception; nested
exception is java.lang.NullPointerException]]
如何在 applicationContext.xml 中获得授权用户?
已更新
如果我按照所说的使用holmis83。我收到错误:
org.hibernate.TransientObjectException:object references an unsaved transient
instance - save the transient instance before flushing:
com.otv.model.entity.Account
在applicationContext.xml中:
<bean id="Authentication" class="com.otv.util.Authentication"/>
<bean id="CurrentAccount" factory-bean="Authentication" factory-method="getAccount" scope="request">
<aop:scoped-proxy/>
</bean>
<bean id="PostPaginatorDTO" class="com.otv.model.dto.paginator.PostPaginatorDTO" scope="request">
<property name="account" ref="CurrentAccount" />
</bean>
PostBean
class:
@ManagedProperty(value="#{PostPaginatorDTO}")
public PostPaginatorDTO paginatorDTO;
public List<Post> getEntityList() {
entityList=getDao().findByPostPaginatorDTO(getPaginatorDTO());
return entityList;
}
我猜您正在尝试创建一个范围不同于默认范围的 bean,即单例。使用 scope
属性。如果您想在单例 bean 中使用作用域 bean,最好也使用作用域代理。
<bean id="CurrentAccount" factory-bean="Authentication" factory-method="getAccount" scope="request">
<aop:scoped-proxy/>
</bean>
对不起我的英语。如何在 applicationContext.xml
中获得授权用户Authentication
class:
public class Authentication {
public Account getAccount(){
return (Account) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
}
}
并且在文件 applicationContext.xml 中:
<bean id="Authentication" class="com.otv.util.Authentication">
</bean>
<bean id="CurrentAccount"
factory-bean="Authentication"
factory-method="getAccount"/>
但它不起作用:
Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'Principal' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public com.otv.model.entity.Account com.otv.util.Authentication.getAccount()] threw exception; nested exception is java.lang.NullPointerException]]
如何在 applicationContext.xml 中获得授权用户?
已更新
如果我按照所说的使用holmis83。我收到错误:
org.hibernate.TransientObjectException:object references an unsaved transient instance - save the transient instance before flushing: com.otv.model.entity.Account
在applicationContext.xml中:
<bean id="Authentication" class="com.otv.util.Authentication"/>
<bean id="CurrentAccount" factory-bean="Authentication" factory-method="getAccount" scope="request">
<aop:scoped-proxy/>
</bean>
<bean id="PostPaginatorDTO" class="com.otv.model.dto.paginator.PostPaginatorDTO" scope="request">
<property name="account" ref="CurrentAccount" />
</bean>
PostBean
class:
@ManagedProperty(value="#{PostPaginatorDTO}")
public PostPaginatorDTO paginatorDTO;
public List<Post> getEntityList() {
entityList=getDao().findByPostPaginatorDTO(getPaginatorDTO());
return entityList;
}
我猜您正在尝试创建一个范围不同于默认范围的 bean,即单例。使用 scope
属性。如果您想在单例 bean 中使用作用域 bean,最好也使用作用域代理。
<bean id="CurrentAccount" factory-bean="Authentication" factory-method="getAccount" scope="request">
<aop:scoped-proxy/>
</bean>