我正在使用Spring MVC 3.2和Spring Security 3.1构建我的Web应用程序。
对于身份验证,我实施UserDetailsService
并使用DaoAuthenticationProvider
。
一切正常!
但是我对userDetailsServiceImpl
bean有问题:
当dbUser
为null
时,NullPointerException
(在行dbUser.getUsername()
处)不会被投放到控制台中。
package net.dntuan.training.mvc.security.authentication;
import java.util.*;
import net.dntuan.training.mvc.dao.UserDao;
import net.dntuan.training.mvc.security.Role;
import org.slf4j.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.*;
@Service("userDetailsServiceImpl")
@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
public class UserDetailsServiceImpl implements UserDetailsService {
private static final Logger logger = LoggerFactory.getLogger(UserDetailsServiceImpl.class);
@Autowired
private UserDao userDao;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
net.dntuan.training.mvc.domain.User dbUser = this.userDao.getUserByUsername(username);
logger.debug("dbUser is null? " + (dbUser == null));
List<GrantedAuthority> roles = new ArrayList<GrantedAuthority>();
roles.add(Role.USER);
User user = new User(dbUser.getUsername(), dbUser.getPassword(), dbUser.getEnable(), true, true, true, roles);
logger.debug("does not reach here");
return user;
}
}
控制台显示如下
[DEBUG] [org.springframework.orm.hibernate4.HibernateTransactionManager] - Creating new transaction with name [net.dntuan.training.mvc.security.authentication.UserDetailsServiceImpl.loadUserByUsername]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly; '' (AbstractPlatformTransactionManager.java:366)
[DEBUG] [org.springframework.orm.hibernate4.HibernateTransactionManager] - Opened new Session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=[] updates=[] deletions=[] collectionCreations=[] collectionRemovals=[] collectionUpdates=[] unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])] for Hibernate transaction (HibernateTransactionManager.java:416)
[DEBUG] [org.springframework.orm.hibernate4.HibernateTransactionManager] - Preparing JDBC Connection of Hibernate Session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=[] updates=[] deletions=[] collectionCreations=[] collectionRemovals=[] collectionUpdates=[] unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])] (HibernateTransactionManager.java:426)
[DEBUG] [org.springframework.orm.hibernate4.HibernateTransactionManager] - Exposing Hibernate transaction as JDBC transaction [org.postgresql.jdbc4.Jdbc4Connection@2826e5d8] (HibernateTransactionManager.java:487)
Hibernate: select this_.id as id1_1_0_, this_.enable as enable2_1_0_, this_.fullname as fullname3_1_0_, this_.password as password4_1_0_, this_.username as username5_1_0_ from public.user this_ where this_.username=?
[DEBUG] [net.dntuan.training.mvc.security.authentication.UserDetailsServiceImpl] - dbUser is null? true (UserDetailsServiceImpl.java:27)
[TRACE] [org.springframework.orm.hibernate4.HibernateTransactionManager] - Triggering beforeCompletion synchronization (AbstractPlatformTransactionManager.java:936)
[DEBUG] [org.springframework.orm.hibernate4.HibernateTransactionManager] - Initiating transaction rollback (AbstractPlatformTransactionManager.java:844)
[DEBUG] [org.springframework.orm.hibernate4.HibernateTransactionManager] - Rolling back Hibernate transaction on Session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=[] updates=[] deletions=[] collectionCreations=[] collectionRemovals=[] collectionUpdates=[] unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])] (HibernateTransactionManager.java:570)
[TRACE] [org.springframework.orm.hibernate4.HibernateTransactionManager] - Triggering afterCompletion synchronization (AbstractPlatformTransactionManager.java:965)
[DEBUG] [org.springframework.orm.hibernate4.HibernateTransactionManager] - Closing Hibernate Session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=[] updates=[] deletions=[] collectionCreations=[] collectionRemovals=[] collectionUpdates=[] unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])] after transaction (HibernateTransactionManager.java:632)
在使用Eclipse调试之后,我认为默认情况下Spring似乎正在捕获异常 我是Spring的新手,所以我的问题是:为什么Spring会默认捕获它? Spring是如何做到的?
答案 0 :(得分:1)
仅仅因为你没有明确提到它,它并不意味着没有抛出NPE。它很可能被抛出,导致数据库事务回滚,因为日志消息清楚地表明了这一点。
<强>更新强>
Spring的事务管理基础架构捕获异常(事实上任何Throwable
)here,并且这样做是为了能够实现事务配置描述的行为(= {{1}的属性你的方法的注释)。默认情况下,任何@Transactional
触发回滚(请参阅docs on @Transactional
settings),因为这些被视为无法恢复的错误情况。如果在RuntimeException
包上启用跟踪级别日志记录,您将能够看到日志中提到的方法引发的NullPointerException
。