2017-06-01 5 views
0

authority-stringを使用して特定のロールのインスタンスを取得するにはどうすればよいですか? 私はほとんどすべてを持っていますが、私は常に結果にnullを取得します。 ようこそ:Role.get( 'ROLE_ADMIN')はnullを返します。Grailsは、spring-scurity-coreでRole-classのインスタンスを取得できません

usercontrollerで定義された、指定されたユーザーのロールを追加または削除する必要のある操作があります。 ===

>> ROLES >>> params: [ROLE_ADMIN, ROLE_SALES, ROLE_SUPPLIER] 
>> ROLES >>> userRoles: [ROLE_ADMIN] 
%%%%% ROLE_ADMIN %%%%%#### Params: ROLE_ADMIN 
#### Role already used: ROLE_ADMIN 
#### Params: ROLE_SALES 
#### userRole to be Created1: ROLE_SALES 
2017-06-01 15:40:34.388 ERROR --- [nio-8080-exec-1] o.g.web.errors.GrailsExceptionResolver : ValidationException occurred when processing request: [POST] /user/userroles - parameters: 
version: 0 
id: 9 
_ROLE_ADMIN: 
ROLE_ADMIN: on 
_ROLE_USER: 
_ROLE_SALES: 
ROLE_SALES: on 
_ROLE_SUPPLIER: 
ROLE_SUPPLIER: on 
Validation Error(s) occurred during save(): 
- Field error in object 'com.torntrading.security.UserRole' on field 'role': rejected value [null]; codes [com.torntrading.security.UserRole.role.nullable.error.com.torntrading.security.UserRole.role,com.torntrading.security.UserRole.role.nullable.error.role,com.torntrading.security.UserRole.role.nullable.error.com.torntrading.security.Role,com.torntrading.security.UserRole.role.nullable.error,userRole.role.nullable.error.com.torntrading.security.UserRole.role,userRole.role.nullable.error.role,userRole.role.nullable.error.com.torntrading.security.Role,userRole.role.nullable.error,com.torntrading.security.UserRole.role.nullable.com.torntrading.security.UserRole.role,com.torntrading.security.UserRole.role.nullable.role,com.torntrading.security.UserRole.role.nullable.com.torntrading.security.Role,com.torntrading.security.UserRole.role.nullable,userRole.role.nullable.com.torntrading.security.UserRole.role,userRole.role.nullable.role,userRole.role.nullable.com.torntrading.security.Role,userRole.role.nullable,nullable.com.torntrading.security.UserRole.role,nullable.role,nullable.com.torntrading.security.Role,nullable]; arguments [role,class com.torntrading.security.UserRole]; default message [Property [{0}] of class [{1}] cannot be null] 
. Stacktrace follows: 

java.lang.reflect.InvocationTargetException: null 
    at org.grails.core.DefaultGrailsControllerClass$ReflectionInvoker.invoke(DefaultGrailsControllerClass.java:210) 
    at org.grails.core.DefaultGrailsControllerClass.invoke(DefaultGrailsControllerClass.java:187) 
    at org.grails.web.mapping.mvc.UrlMappingsInfoHandlerAdapter.handle(UrlMappingsInfoHandlerAdapter.groovy:90) 
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) 
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) 
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) 
    at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317) 
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127) 
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:115) 
    at grails.plugin.springsecurity.web.UpdateRequestContextHolderExceptionTranslationFilter.doFilter(UpdateRequestContextHolderExceptionTranslationFilter.groovy:64) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 
    at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.groovy:53) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 
    at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:158) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:169) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 
    at grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.groovy:62) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 
    at grails.plugin.springsecurity.web.SecurityRequestHolderFilter.doFilter(SecurityRequestHolderFilter.groovy:58) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214) 
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177) 
    at org.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:77) 
    at org.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:67) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: grails.validation.ValidationException: Validation Error(s) occurred during save(): 
- Field error in object 'com.torntrading.security.UserRole' on field 'role': rejected value [null]; codes [com.torntrading.security.UserRole.role.nullable.error.com.torntrading.security.UserRole.role,com.torntrading.security.UserRole.role.nullable.error.role,com.torntrading.security.UserRole.role.nullable.error.com.torntrading.security.Role,com.torntrading.security.UserRole.role.nullable.error,userRole.role.nullable.error.com.torntrading.security.UserRole.role,userRole.role.nullable.error.role,userRole.role.nullable.error.com.torntrading.security.Role,userRole.role.nullable.error,com.torntrading.security.UserRole.role.nullable.com.torntrading.security.UserRole.role,com.torntrading.security.UserRole.role.nullable.role,com.torntrading.security.UserRole.role.nullable.com.torntrading.security.Role,com.torntrading.security.UserRole.role.nullable,userRole.role.nullable.com.torntrading.security.UserRole.role,userRole.role.nullable.role,userRole.role.nullable.com.torntrading.security.Role,userRole.role.nullable,nullable.com.torntrading.security.UserRole.role,nullable.role,nullable.com.torntrading.security.Role,nullable]; arguments [role,class com.torntrading.security.UserRole]; default message [Property [{0}] of class [{1}] cannot be null] 

    at org.grails.orm.hibernate.AbstractHibernateGormInstanceApi.save(AbstractHibernateGormInstanceApi.groovy:131) 
    at org.grails.datastore.gorm.GormEntity$Trait$Helper.save(GormEntity.groovy:151) 
    at com.torntrading.security.UserController$$EQLIup0W.$tt__userroles(UserController.groovy:46) 
    at grails.transaction.GrailsTransactionTemplate$2.doInTransaction(GrailsTransactionTemplate.groovy:96) 
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133) 
    at grails.transaction.GrailsTransactionTemplate.execute(GrailsTransactionTemplate.groovy:93) 
    ... 37 common frames omitted 

:以下のコードは、testcodeの多くが含まれており、everyhingは、私は、コンソールが実行上でこれを示し

def userroles() { 
    def user = User.get(params.id) 
    List<String> userRoles = roleNamesFromUser(user) 
    List<String> uRoles = roleNamesFromParams() 
    println(">> ROLES >>> params: "+uRoles) 
    println(">> ROLES >>> userRoles: "+userRoles) 

    for (uRole in uRoles) { 
     println("#### Params: "+uRole) 
     if (uRole in userRoles) { 
      println("#### Role already used: "+uRole) 

     } else { 
      println("#### userRole to be Created1: "+uRole) 
      def role = Role.findByAuthority(uRole) 
//    println("#### userRole to be Created2: "+role.authority) 
//    UserRole.create user, role 
      def ur = new UserRole(user:user, role: role).save(flush:true, failOnError:true) 
      println("#### Role Created: "+ur.role.authority+" - username: "+ur.user.username) 
     } 
    } 
    for (role in userRoles) { 
     if (role in uRoles) { 
      println("#### Role already exist: "+role) 

     } else { 
      Role.findByAuthority(uRole).delete() 
      println("#### Role deleted: "+role) 
     } 
    } 
    userRoles = roleNamesFromUser(user) 
    for (role in userRoles) { 
     println("#### After update - Role: "+role) 
    } 


    UserRole.withSession { 
     it.flush() 
     it.clear() 
    } 

    redirect action:"index" 
} 
protected List<String> roleNamesFromParams() { 
params.keySet().findAll { it.contains('ROLE_') && params[it] == 'on' } as List 
} 
protected List<String> roleNamesFromUser(User user) { 
    def ur = user.getAuthorities().toList() 
    def List<String> ul = new ArrayList<String>() 
    def String auth 
    for(int i=0; i<ur.size; i++){ 
     auth = ur[i].authority 
     ul.add(auth) 
    } 
    return ul 
} 

役割クラスからインスタンスを取得できないことを除いて仕事を探します=================================

データベースには、これを含んでいます

id version authority 
1 0 ROLE_ADMIN 
2 0 ROLE_USER 
3 0 ROLE_SALES 
4 0 ROLE_SUPPLIER 
+0

データベースロールに実際にROLE_という接頭辞が付いていますか?私はLDAPグループを作成するときにROLE_をプレフィックスにしませんが、ユーザーのロールをチェックするときにはプレフィックスを追加する必要があります。 –

+0

はい、そうです。私の質問の一番下には、データベースからの印刷物があります。 – larand

答えて

0

を持っていますあなたはこの方法で試しました:Role.findByAuthority("ROLE_ADMIN")

+0

はい!それはそれをした!私はそれを試みましたが、権限の文字列に問題がありました。このシンプルなクラスを使うのがどれほど難しいか分かりませんでした。何らかの失敗したテストの後、私は最終的に、 'ROLE_ADMIN'を含む単純な文字列であると思ったことを発見しました。 '[UserRole authority:ROLE_ADMIN]'のようなものがあります。そして、私はさまざまなソリューションをテストし、 'findByAuthority'は動作しなかったことを思い出しました。さて、それは動作します、ありがとう、これは私が今までにあった最も不満な問題でした。 – larand

関連する問題