Java 在用户服务ref中使用jdbc用户服务

Java 在用户服务ref中使用jdbc用户服务,java,session,authentication,spring-security,Java,Session,Authentication,Spring Security,我使用的是spring安全性,对于基于数据库的身份验证,我使用了以下配置,并且工作正常 <authentication-manager alias="authenticationManager"> <authentication-provider> <jdbc-user-service data-source-ref="dataSource" users-by-username-query="SELECT ...... U.email_ad

我使用的是spring安全性,对于基于数据库的身份验证,我使用了以下配置,并且工作正常

<authentication-manager alias="authenticationManager">
  <authentication-provider>
     <jdbc-user-service data-source-ref="dataSource"
      users-by-username-query="SELECT ...... U.email_address=?"
      authorities-by-username-query="SELECT ... U.email_address=?">
  </authentication-provider>
</authentication-manager>
,我试过了,现在有问题了

XML说我不能同时使用用户服务ref和jdbc用户服务。有没有一种方法可以解决这个问题,或者如果我必须使用user service ref标签来验证用户,我必须做什么?还有什么方法可以添加额外的信息,比如说整个用户都反对会话


非常感谢您的帮助。

经过数小时的搜索和实验,我终于可以这样做了

  • 创建一个新的服务,比如MyUserService,它将实现
    org.springframework.security.core.userdetails.userdetails服务
    ,并具有注释
    @service
    。UserDetailsService只有一个方法
    loadUserByUserName
    。此方法的实现将在MyUserService中进行。看起来是这样的

    public class MyUser implements UserDetails {
        private static final long serialVersionUID = 1L;
    /*All the variables their getter setters that you wish to store in session. And    
       implementation of all the methods of UserDetails go here.*/
    
     }
    
    @服务 公共类MyUserService实现UserDetailsService{

    @Override
    public UserDetails loadUserByUsername(String arg0)
            throws UsernameNotFoundException {
    
        MyUser user=new MyUser();
        /*get details of user and authorities from database whose username is   
                arg0 and place them in user instance */
        return user;
    }
    
    }
    
  • MyUser也是一个新类,它实现了
    org.springframework.security.core.userdetails.userdetails
    ,它的所有方法都在MyUser类中实现

    public class MyUser implements UserDetails {
        private static final long serialVersionUID = 1L;
    /*All the variables their getter setters that you wish to store in session. And    
       implementation of all the methods of UserDetails go here.*/
    
     }
    
  • 定义这样一个bean
  • 其中,
    org.aurora.timeexpense.service.MyUserService
    是我定义的实现UserDetailsService的服务的路径

    4.Spring安全配置将如下所示


    你可以走了。

    经过几个小时的搜索和实验,我终于可以这样做了

  • 创建一个新的服务,比如MyUserService,它将实现
    org.springframework.security.core.userdetails.UserDetailsService
    ,并具有注释
    @service
    。UserDetailsService只有一个方法
    loadUserByUserName
    。此方法的实现将在MyUserService中。它将如下所示

    public class MyUser implements UserDetails {
        private static final long serialVersionUID = 1L;
    /*All the variables their getter setters that you wish to store in session. And    
       implementation of all the methods of UserDetails go here.*/
    
     }
    
    @服务 公共类MyUserService实现UserDetailsService{

    @Override
    public UserDetails loadUserByUsername(String arg0)
            throws UsernameNotFoundException {
    
        MyUser user=new MyUser();
        /*get details of user and authorities from database whose username is   
                arg0 and place them in user instance */
        return user;
    }
    
    }
    
  • MyUser也是一个新类,它实现了
    org.springframework.security.core.userdetails.userdetails
    ,它的所有方法都在MyUser类中实现

    public class MyUser implements UserDetails {
        private static final long serialVersionUID = 1L;
    /*All the variables their getter setters that you wish to store in session. And    
       implementation of all the methods of UserDetails go here.*/
    
     }
    
  • 定义这样一个bean
  • 其中,
    org.aurora.timeexpense.service.MyUserService
    是我定义的实现UserDetailsService的服务的路径

    4.Spring安全配置将如下所示


    你可以开始了。

    我已经删除了jdb用户服务标签,现在只在身份验证提供程序中使用用户服务参考标签,身份验证工作正常。现在我想返回自定义用户详细信息,而不是我的customUserDeatilsService中的org.springframework.security.core.userdetails.user,但我不知道怎么做我已经删除了jdb用户服务标签,现在只在身份验证提供程序中使用用户服务引用标签,身份验证工作正常。现在我想返回的是自定义用户详细信息,而不是customUserDeatilsSer中的org.springframework.security.core.userdetails.user但是我不知道怎么做,如果有什么我能做的,请帮助我。