Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从数据库中获取电子邮件和密码?我能';t在Spring安全中验证用户_Java_Mysql_Spring_Security_Jpa - Fatal编程技术网

Java 如何从数据库中获取电子邮件和密码?我能';t在Spring安全中验证用户

Java 如何从数据库中获取电子邮件和密码?我能';t在Spring安全中验证用户,java,mysql,spring,security,jpa,Java,Mysql,Spring,Security,Jpa,堆栈跟踪 为什么当我尝试在AdminUser=(AdminUser)jpaUserDao.findUserByEmail(username)行中调用loadUserByUsername方法时,UserDetailsServiceImplementation中出现java.lang.NullPointerException spring-security.xml persistence.xml com.softserveinc.ita.redplatform.common.entity 更新

堆栈跟踪 为什么当我尝试在AdminUser=(AdminUser)jpaUserDao.findUserByEmail(username)行中调用loadUserByUsername方法时,UserDetailsServiceImplementation中出现java.lang.NullPointerException

spring-security.xml


persistence.xml


com.softserveinc.ita.redplatform.common.entity
更新
真的
org.hibernate.dialogue.mysql5dialogue
JPAAdminUserDao通过电子邮件查找用户

@存储库
@交易的
公共类JPAAdminUserDao扩展了JPAGenericDao
实现AdminUserDao{
@持久上下文
私有EntityManager EntityManager FactoryBean;
公共最终管理员用户findUserByEmail(最终字符串电子邮件){
列表用户=新建ArrayList();
用户=(列表)entityManagerFactoryBean
.createQuery(“来自”+AdminUser.class.getName()
+“作为用户,其中user.email=:email”)
.setParameter(“email”,email).getResultList();
如果(users.size()>0){
返回用户。获取(0);
}否则{
返回null;
}
}
公共静态void main(字符串[]args){
JPAAdminUserDao=新的JPAAdminUserDao();
dao.findUserByEmail(“rom23”);
}
}
UserDetailsService实现

公共类UserDetailsService实现实现UserDetailsService{
私用管理员道jpaUserDao;
@凌驾
公共最终用户详细信息loadUserByUsername(最终字符串用户名)
抛出UsernameNotFoundException{
HashSet=newhashset();
AdminUser=(AdminUser)jpaUserDao.findUserByEmail(用户名);
if(AdminUser的用户实例){
添加(新字符串(“角色用户”);
添加(新字符串(“角色\管理员”);
添加(新字符串(“ROLE_REDADMIN”);
}否则{
添加(新字符串(“角色用户”);
}
列表权限=buildUserAuthority(设置);
返回buildUserForAuthentication(用户、权限);
}
私人用户buildUserForAuthentication(最终AdminUser用户,
最后名单(主管当局){
返回新用户(User.getEmail(),User.getPassword(),true,
真的,真的,真的,权威);
}
私有列表buildUserAuthority(最终版)
设置用户角色){
Set setAuths=new HashSet();
//建立用户权限
for(字符串userRole:userRoles){
添加(新的SimpleGrantedAuthority(userRole));
}
列表结果=新建
ArrayList(setAuths);
返回结果;
}
公共最终管理员dao getJpaUserDao(){
返回jpaUserDao;
}
公共最终作废setAdminUserDao(最终AdminUserDao newJpaUserDao){
this.jpaUserDao=newJpaUserDao;
}
}

问题在于这一行代码:

AdminUser user = (AdminUser) jpaUserDao.findUserByEmail(username);
只是
jpaUserDao
不被注入。 您必须在bean声明中设置
jpaUserDao
,如:

<beans:bean id="UserDetailsServiceImplementation"
        class="com.softserveinc.ita.redplatform.business.service.UserDetailsServiceImplementation">
  <property name="adminUserDao" ref="jpaUserDao" />
</beans:bean>


并确保
jpaUserDao
也被声明

在这里发布整个堆栈跟踪是不可能的。如果你希望别人给出一些答案,请发布一些相关代码。不要作为评论,编辑你的问题。@Roma编辑你的问题,包括总体目标、期望的行为、必要的代码和目标系统。现在我有另外一个问题是由以下原因引起的:com.softserveinc.ita.redplatform.persistence.dao.impl.JPAAdminUserDao.findUserByEmail(JPAAdminUserDao.java:36),com.softserveinc.ita.redplatform.business.service.UserDetailsServiceImplementation.loadUserByUsername(UserDetailsServiceImplementation.java:41)位于org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:114)…41更多'
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"     xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">

<context:component-scan base-     package="com.softserveinc.ita.redplatform.persistence.dao.impl" />

<bean id="entityManagerFactoryBean"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

    <property name="dataSource" ref="dataSource" />

    <property name="persistenceUnitName" value="JPAUnit" />

    <property name="packagesToScan">
        <list>
            <value>com.softserveinc.ita.redplatform.common.entity</value>
        </list>
    </property>

    <property name="jpaVendorAdapter">
        <bean      class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>

    <property name="jpaProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop     key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
        </props>
    </property>
</bean>

<bean id="dataSource"     class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="${jdbc.driverClassName}" />
  <property name="url" value="${jdbc.url}" />
  <property name="username" value="${jdbc.username}" />
  <property name="password" value="${jdbc.password}" />
</bean>

<bean id="transactionManager"      class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactoryBean" />
</bean>

<tx:annotation-driven />

</beans>
@Repository
@Transactional
public class JPAAdminUserDao extends JPAGenericDao<AdminUser, Long> 
   implements AdminUserDao {
@PersistenceContext
private EntityManager entityManagerFactoryBean;

public final AdminUser findUserByEmail(final String email) {
    List<AdminUser> users = new ArrayList<AdminUser>();
    users = (List<AdminUser>) entityManagerFactoryBean
            .createQuery("from " + AdminUser.class.getName() 
                    + " as user where user.email=:email")
            .setParameter("email", email).getResultList();
    if (users.size() > 0) {
        return users.get(0);
    } else {
        return null;
    }
}

public static void main(String[] args) {
    JPAAdminUserDao dao = new JPAAdminUserDao();
    dao.findUserByEmail("rom23");
}
}
public class UserDetailsServiceImplementation implements UserDetailsService     {

private AdminUserDao jpaUserDao;

 @Override
public final UserDetails loadUserByUsername(final String username) 
        throws UsernameNotFoundException {

    HashSet<String> set = new HashSet<String>();
    AdminUser user = (AdminUser) jpaUserDao.findUserByEmail(username);
    if (user instanceof AdminUser) {
        set.add(new String("ROLE_USER"));
        set.add(new String("ROLE_ADMIN"));
        set.add(new String("ROLE_REDADMIN"));
    } else {
        set.add(new String("ROLE_USER"));
    }
    List<GrantedAuthority> authorities = buildUserAuthority(set);
    return buildUserForAuthentication(user, authorities);

}

private User buildUserForAuthentication(final AdminUser user, 
        final List<GrantedAuthority> authorities) {
    return new User(user.getEmail(), user.getPassword(), true, 
            true, true, true, authorities);
}


private List<GrantedAuthority> buildUserAuthority(final 
        Set<String> userRoles) {

    Set<GrantedAuthority> setAuths = new HashSet<GrantedAuthority>();
    // Build user's authorities
    for (String userRole : userRoles) {
        setAuths.add(new SimpleGrantedAuthority(userRole));
    }
    List<GrantedAuthority> result = new 
            ArrayList<GrantedAuthority>(setAuths);

    return result;
}

public final AdminUserDao getJpaUserDao() {
    return jpaUserDao;
}

public final void setAdminUserDao(final AdminUserDao newJpaUserDao) {
    this.jpaUserDao = newJpaUserDao;
}
}
AdminUser user = (AdminUser) jpaUserDao.findUserByEmail(username);
<beans:bean id="UserDetailsServiceImplementation"
        class="com.softserveinc.ita.redplatform.business.service.UserDetailsServiceImplementation">
  <property name="adminUserDao" ref="jpaUserDao" />
</beans:bean>