【问题标题】:Extending Spring Security UI plugin in Grails Application在 Grails 应用程序中扩展 Spring Security UI 插件
【发布时间】:2015-03-27 19:16:36
【问题描述】:

我一直在寻找正确扩展 spring security ui 插件的方法。 我想做的事情显然很简单:我有 3 个由脚本创建的域类:

  • 用户
  • 角色
  • 用户角色

我想扩展 User 域类,以添加名字、姓氏、国家、出生日期、性别和可见性功能,我想出了这个想法:

我创建了另外 2 个域类,RegisterUser 和 UserInfo。

代码如下:

class User {

   transient springSecurityService

   String username
   String password
   long phone
   String email
   boolean enabled = true
   boolean accountExpired
   boolean accountLocked
   boolean passwordExpired

   static hasMany = [openIds: OpenID]

   static transients = ['springSecurityService']

   static constraints = {
      username blank: false, unique: true
      password blank: false
      phone unique:true , nullable:false, blank: false, length:10, range: 3200000000..3940000000
      email email:true, nullable:false, blank:false
   }

   static mapping = {
      phone column :'idUser'
      email column : 'email'
      username column : 'username'
      password column: '`password`'
   }

   Set<Role> getAuthorities() {
      UserRole.findAllByUser(this).collect { it.role }
   }

   def beforeInsert() {
      encodePassword()
   }

   def beforeUpdate() {
      if (isDirty('password')) {
         encodePassword()
      }
   }

   protected void encodePassword() {
      password = springSecurityService?.passwordEncoder ? springSecurityService.encodePassword(password) : password
   }
}

class UserInfo implements Comparable {

   String firstname
   String lastname
   String country
   Date date_of_birth
   String gender

   static belongsTo = RegisteredUser

   static mapping = {
      firstname column : 'firstname'
      lastname column : 'surname'
      date_of_birth column : 'date_of_birth'
      country column : 'country'
      gender column : 'gender'
   }

   static constraints = {
      firstname blank:false, matches:"[a-zA-Z]+"
      lastname blank:false, matches:"[a-zA-Z]+"
      country blank:true, nullable:true
      date_of_birth nullable:true, unique:false
      gender blank:true, nullable:true, inList:["Male", "Female"]
   }

   @Override
   public int compareTo(Object o) {
      // Compare two user basing on their phone numbers and username
      RegisteredUser u = (RegisteredUser) o
      if(u.info.phone == this.phone){
         if(u.info.email == this.email){
            return 0;
         }
         return -1;
      }
      return 1;
   }
}

class RegisteredUser {

   User user
   UserInfo info
   boolean visible

   //Not related to spring security
   static hasOne = UserInfo
   static hasMany = [friendships : Friendship, blocked: Block, trips : Trip]

   static constraints = {

   }

   /**
    * Programmatic exercise...? Do it in a more elegant (groovy) and less java way
    * @return
    */
   static List<RegisteredUser> getContactList_Java(){
      def friendshipList = 
         Friendship.getFriendships(this)
      def contactList = new ArrayList<RegisteredUser>()
      for(Friendship f : friendshipList){
         if(f.user1.equals(this)){
            friendshipList.add(f.user2)
         }else{
            friendshipList.add(f.user1)
         }
      }
   }
   /**
    * Programmatic exercise?... Done! ;D
    * @return
    */
   static List<RegisteredUser> getContactList(){
      Friendship.getFriendships(){((Friendship)it).otherUser(this)}.collect()
   }

   /**
    * 
    * method user_exists
    * Parameters:
    * @param user
    * @return
    * method type:
    * Dynamically chosen
    */
   static user_exists(User u){
      if(User.where{
         username == u.username}.count() > 0){
         true
      }
      false
   }

   /**
    * 
    * method findByPhone
    * Parameters:
    * @param number
    * @return
    * method type:
    * Dynamically chosen
    */
   def findByPhone(int number){
      User.findByPhone(number)
   }

   /**
    * 
    * method findBySomeInfo
    * Parameters:
    * @param infos
    * @return
    * method type:
    * Dynamically chosen
    */
   def findByMail(String mail){
      User.findByEmail(mail)
   }

   /**
    * 
    * method findByName
    * Parameters:
    * @param firstname
    * @return
    * method type:
    * Dynamically chosen
    */
   def findByName(String firstname){
      UserInfo.findByFirstname(firstname)
   }

   /**
    * 
    * method findSentRequests
    * Parameters:
    * @return
    * method type:
    * Dynamically chosen
    */
   def findSentRequests(){
      Request.findBySender(this)
   }

   /**
    * 
    * method findReceivedRequest
    * Parameters:
    * @return
    * method type:
    * Dynamically chosen
    */
   def findReceivedRequest(){
      Request.findByReceiver(this)
   }

   /**
    * 
    * Method findSentActiveRequest
    * Parameters:
    * @return 
    * Method type:
    * Dynamically defined (Default)
    */
   def findSentActiveRequest(){
      Request.findBySenderAndStatus(this, util.Status.ACTIVE)
   }
}

如果我通过引导程序实例化注册用户,则应用程序根本看不到用户。

我看不到我明显犯的错误。

【问题讨论】:

    标签: grails spring-security


    【解决方案1】:

    我建议你删除 RegisteredUser 类。我认为一个好的解决方案可能是让一个用户类和一个配置文件类处于一对一的关系中:

    class User{
    
    transient springSecurityService
    
       String username
       String password
       long phone
       String email
       boolean enabled = true
       boolean accountExpired
       boolean accountLocked
       boolean passwordExpired
    
       ...
    
       static hasOne = [profile: UserProfile]
    
       ...
    }
    
    
    class UserProfile {
    
       User user
       String firstname
       String lastname
       String country
       Date date_of_birth
       String gender
    
       ...
    
    }
    

    【讨论】:

      猜你喜欢
      • 2011-10-08
      • 2014-02-27
      • 2016-05-24
      • 2011-09-25
      • 2015-06-18
      • 2013-01-08
      • 1970-01-01
      • 2012-10-01
      • 2016-01-30
      相关资源
      最近更新 更多