在Grails应用程序中扩展Spring Security UI插件

在Grails应用程序中扩展Spring Security UI插件,grails,spring-security,Grails,Spring Security,我一直在努力寻找正确扩展SpringSecurityUI插件的方法。 我想做的很简单:我有3个由脚本创建的域类: 使用者 角色 用户角色 我想扩展User domain类,以便添加名字、姓氏、国家、出生日期、性别和可见性特性,我提出了以下想法: 我创建了另外两个域类RegisterUser和UserInfo 守则如下: class User { transient springSecurityService String username String password

我一直在努力寻找正确扩展SpringSecurityUI插件的方法。 我想做的很简单:我有3个由脚本创建的域类:

  • 使用者
  • 角色
  • 用户角色
我想扩展User domain类,以便添加名字、姓氏、国家、出生日期、性别和可见性特性,我提出了以下想法:

我创建了另外两个域类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)
   }
}
类用户{
临时安全服务
字符串用户名
字符串密码
长电话
字符串电子邮件
布尔启用=真
布尔帐户已过期
布尔帐户锁定
布尔密码过期
static hasMany=[OpenID:OpenID]
静态瞬态=['springSecurityService']
静态约束={
用户名空白:false,唯一:true
密码空白:false
电话唯一:true,可空:false,空白:false,长度:10,范围:32000000000..3940000000
电子邮件:真,可空:假,空白:假
}
静态映射={
电话栏:'idUser'
电子邮件专栏:“电子邮件”
用户名列:“用户名”
密码列:“`password`”
}
设置getAuthorities(){
UserRole.findallbyser(this.collect{it.role}
}
def beforeInsert(){
encodePassword()
}
def beforeUpdate(){
if(isDirty(‘密码’)){
encodePassword()
}
}
受保护的无效密码(){
密码=springSecurityService?.passwordEncoder?springSecurityService.encodePassword(密码):密码
}
}
类UserInfo实现了可比较的{
字符串名
字符串姓氏
弦国
出生日期
字符串性别
static belongsTo=RegisteredUser
静态映射={
firstname列:“firstname”
lastname列:“姓氏”
出生日期栏:“出生日期”
国家专栏:“国家”
性别专栏:“性别”
}
静态约束={
firstname空白:false,匹配:“[a-zA-Z]+”
lastname空白:false,匹配:“[a-zA-Z]+”
国家/地区空白:true,可为空:true
出生日期可为空:真,唯一:假
性别空白:true,可空:true,inList:[“男性”,“女性”]
}
@凌驾
公共整数比较对象(对象o){
//根据两个用户的电话号码和用户名进行比较
RegisteredUser u=(RegisteredUser)o
如果(u.info.phone==此电话){
如果(u.info.email==此电子邮件){
返回0;
}
返回-1;
}
返回1;
}
}
类注册器{
用户用户
用户信息
布尔可见
//与spring安全性无关
静态hasOne=UserInfo
static hasMany=[友谊:友谊,阻止:阻止,旅行:旅行]
静态约束={
}
/**
*编程练习…?以更优雅(groovy)和更少java的方式进行
*@返回
*/
静态列表getContactList_Java(){
def友谊列表=
友谊。获取友谊(本)
def contactList=new ArrayList()
用于(友谊f:友谊列表){
如果(f.user1.equals(this)){
friendshipList.add(f.user2)
}否则{
friendshipList.add(f.user1)
}
}
}
/**
*程序性练习?…完成!;D
*@返回
*/
静态列表getContactList(){
Friendly.getFriendlations(){((Friendly)it).otherUser(this)}.collect()
}
/**
* 
*方法用户不存在
*参数:
*@param用户
*@返回
*方法类型:
*动态选择
*/
静态用户_存在(用户u){
if(User.where{
username==u.username}.count()>0){
真的
}
假的
}
/**
* 
*方法findByPhone
*参数:
*@param编号
*@返回
*方法类型:
*动态选择
*/
def findByPhone(整数){
User.findByPhone(数字)
}
/**
* 
*方法findBySomeInfo
*参数:
*@param infos
*@返回
*方法类型:
*动态选择
*/
def findByMail(字符串邮件){
User.findByEmail(邮件)
}
/**
* 
*方法findByName
*参数:
*@param firstname
*@返回
*方法类型:
*动态选择
*/
def findByName(字符串名){
UserInfo.findByFirstname(firstname)
}
/**
* 
*方法查找中心
*参数:
*@返回
*方法类型:
*动态选择
*/
def FindCenter请求(){
Request.findBySender(此)
}
/**
* 
*方法findReceivedRequest
*参数:
*@返回
*方法类型:
*动态选择
*/
def findReceivedRequest(){
Request.findByReceiver(此)
}
/**
* 
*方法findSentActiveRequest
*参数:
*@返回
*方法类型:
*动态定义(默认)
*/
def findSentActiveRequest(){
Request.findBySenderAndStatus(这是util.Status.ACTIVE)
}
}
如果我通过引导实例化一个注册用户,那么应用程序根本看不到用户


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

我建议您删除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

   ...

}