Grails安全插件自定义重定向
我是Groovy和Grails的新手。我已经使用Spring安全插件开发了一个应用程序,使用数据库请求映射。我想根据角色自定义重定向到主页 如果用户是ROLE_ADMIN,他将被重定向到views adminUser/Homepage.gsp中的主页 如果用户是ROLE_用户,他将被重定向到views user/Homepage.gsp中的主页Grails安全插件自定义重定向,grails,spring-security,Grails,Spring Security,我是Groovy和Grails的新手。我已经使用Spring安全插件开发了一个应用程序,使用数据库请求映射。我想根据角色自定义重定向到主页 如果用户是ROLE_ADMIN,他将被重定向到views adminUser/Homepage.gsp中的主页 如果用户是ROLE_用户,他将被重定向到views user/Homepage.gsp中的主页 我无法根据用户登录获得任何自定义身份验证重定向。我就是这样做的。我已经根据你的需要修改过了。如果有帮助,请告诉我 在auth()方法下的springse
我无法根据用户登录获得任何自定义身份验证重定向。我就是这样做的。我已经根据你的需要修改过了。如果有帮助,请告诉我 在auth()方法下的springsecurities LoginController内部执行类似操作(它将在单击登录之前获取用户所在的页面): 现在在src/groovy中创建一个auth成功处理程序:
package packageName
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
public class MyAuthSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler
{
@Override
protected String determineTargetUrl(HttpServletRequest request, HttpServletResponse response)
{
def returnUrl = request.getSession().getAttribute('returnUrl')
// Get current users role using springSecurityService
// You can inject springSecurityService into this class
// http://stackoverflow.com/questions/6467167/how-to-get-current-user-role-with-spring-security-plugin
if (role == 'ROLE_ADMIN')
{
returnUrl = '/adminUser/Homepage.gsp'
}
else if (role == 'ROLE_USER')
{
returnUrl = '/User/Homepage.gsp'
}
else
{
returnUrl = 'redirect somewhere'
}
request.getSession().removeAttribute('returnUrl')
return returnUrl
}
}
现在在conf/spring/resources.groovy下创建一个bean,如下所示:
import grails.plugin.springsecurity.SpringSecurityUtils
// Place your Spring DSL code here
beans = {
authenticationSuccessHandler(packageName.MyAuthSuccessHandler) {
def conf = SpringSecurityUtils.securityConfig
requestCache = ref('requestCache')
defaultTargetUrl = conf.successHandler.defaultTargetUrl
alwaysUseDefaultTargetUrl = conf.successHandler.alwaysUseDefault
targetUrlParameter = conf.successHandler.targetUrlParameter
useReferer = conf.successHandler.useReferer
redirectStrategy = ref('redirectStrategy')
}
}
那你就可以走了。让我知道它是否有效。在使用方法之前,必须先注入springSecurityService。另外,getAuthories()应该返回一个列表,这样您就必须循环遍历它(这是因为人们可以有多个角色)
假设在BuildConfig中有这样一行代码:
compile ":spring-security-core:2.0-RC4"
在您的引导程序中有如下代码:
def roleAdmin = new Role(authority:LSSRole.ROLE_ADMIN.toString()).save(failOnError: true)
def roleFirm = new Role(authority:LSSRole.ROLE_FIRM.toString()).save(failOnError: true)
def roleLaw = new Role(authority:LSSRole.ROLE_LAWYER.toString()).save(failOnError: true)
def roleFin = new Role(authority:LSSRole.ROLE_FINANCE.toString()).save(failOnError: true)
使用此代码创建的示例管理员用户:
UserRole.create admin, roleAdmin, true
配置中的一些代码如下所示:
'/dbconsole/**': [LSSRole.ROLE_ADMIN.toString()],
'/secure/**': [LSSRole.ROLE_ADMIN.toString()],
'/payment/**': [LSSRole.ROLE_FIRM.toString()],
'/filing/**': [LSSRole.ROLE_FIRM.toString()],
'/finance/**': [LSSRole.ROLE_FINANCE.toString()],
'/lawyer/**': [LSSRole.ROLE_LAWYER.toString()],
其中LSSRole是一个枚举,一些代码如下:
"/" {
controller = "dispatch"
action = "index"
}
在成功登录后将用户转移到的UrlMappings中,您可以构建一个类似这样的调度器,根据用户的角色将用户分派到不同的登录页:
class DispatchController {
def index() {
def controller = 'login'
def action = 'auth'
if (SpringSecurityUtils.ifAllGranted(LSSRole.ROLE_ADMIN.toString())) {
controller = 'secure'
action = 'index'
} else if (SpringSecurityUtils.ifAllGranted(LSSRole.ROLE_FINANCE.toString())) {
controller = 'finance'
action = 'index'
} else if (SpringSecurityUtils.ifAllGranted(LSSRole.ROLE_FIRM.toString())) {
controller = 'filing'
action = 'summary'
} else if (SpringSecurityUtils.ifAllGranted(LSSRole.ROLE_LAWYER.toString())) {
controller = 'lawyer'
action = 'index'
} else {
flash.message = 'Where do you think you\'re going? Nno no no'
SecurityContextHolder.clearContext()
}
redirect controller:controller, action:action
}
希望这有帮助
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import grails.plugin.springsecurity.SpringSecurityUtils
public class MyAuthSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler
{
@Override
protected String determineTargetUrl(HttpServletRequest request, HttpServletResponse response)
{
def returnUrl = request.getSession().getAttribute('returnUrl')
def roles = SpringSecurityUtils.getPrincipalAuthorities()
for (String role in roles)
{
if (role.equals("ROLE_ADMIN")) {
returnUrl = '/AdminUser/index.gsp'
}
else if (role.equals("ROLE_USER")) {
returnUrl = '/User/index.gsp'
}
else {
returnUrl = '/'
}
}
request.getSession().removeAttribute('returnUrl')
return returnUrl
}
}
这是我的工作代码。。。。
我使用SpringSecurityUtils来获取当前用户角色并将其重定向到所需页面,而不是注入依赖项。。。。。。
谢谢大家的支持
@sean3838感谢您的帮助…..注入springSecurityService会给出一个空指针异常,下面是代码和错误消息请注意,在DispatcherController中使用这种方法,不需要显式注入springSecurityService的依赖项,它按原样工作。非常感谢你的回答这是一个很好的重定向方法…但是我的场景有点不同,你的逻辑不能在我的应用程序中组合…非常感谢你的帮助bt问题仍然是一样的,它再次返回null。。。。。springSecurityService未初始化,正在获取nullpointer异常。。。。。下面是我的错误代码:-[error MESSAGE]MESSAGE:无法在null对象上调用方法getPrincipal()
class DispatchController {
def index() {
def controller = 'login'
def action = 'auth'
if (SpringSecurityUtils.ifAllGranted(LSSRole.ROLE_ADMIN.toString())) {
controller = 'secure'
action = 'index'
} else if (SpringSecurityUtils.ifAllGranted(LSSRole.ROLE_FINANCE.toString())) {
controller = 'finance'
action = 'index'
} else if (SpringSecurityUtils.ifAllGranted(LSSRole.ROLE_FIRM.toString())) {
controller = 'filing'
action = 'summary'
} else if (SpringSecurityUtils.ifAllGranted(LSSRole.ROLE_LAWYER.toString())) {
controller = 'lawyer'
action = 'index'
} else {
flash.message = 'Where do you think you\'re going? Nno no no'
SecurityContextHolder.clearContext()
}
redirect controller:controller, action:action
}
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import grails.plugin.springsecurity.SpringSecurityUtils
public class MyAuthSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler
{
@Override
protected String determineTargetUrl(HttpServletRequest request, HttpServletResponse response)
{
def returnUrl = request.getSession().getAttribute('returnUrl')
def roles = SpringSecurityUtils.getPrincipalAuthorities()
for (String role in roles)
{
if (role.equals("ROLE_ADMIN")) {
returnUrl = '/AdminUser/index.gsp'
}
else if (role.equals("ROLE_USER")) {
returnUrl = '/User/index.gsp'
}
else {
returnUrl = '/'
}
}
request.getSession().removeAttribute('returnUrl')
return returnUrl
}
}