如何使用authority-string获取特定角色的实例? 我几乎所有的东西,但我总是在结果中得到null。 例如:Role.get('ROLE_ADMIN')返回null。
在usercontroller中定义一个应该为指定用户添加/删除角色的操作。下面的代码包含很多测试代码,并且每次访问看起来都有效,只是我无法从Role-class中检索实例
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
}
控制台在运行中显示:
>> 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
====================================
数据库包含:
id version authority
1 0 ROLE_ADMIN
2 0 ROLE_USER
3 0 ROLE_SALES
4 0 ROLE_SUPPLIER
答案 0 :(得分:0)
您是否尝试过这种方式:Role.findByAuthority("ROLE_ADMIN")
?