登录控制器和grails 3.0.0M1中的视图

时间:2015-10-23 05:44:26

标签: spring grails

安装grails 3.0.5并安装spring security core 3.0.0 M1

我创建域用户角色并请求地图..

我尝试运行它..

然后我收到此错误..

    Line | Method
->>   62 | doFilter  in grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|     53 | doFilter  in grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter
|     62 | doFilter  in grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run       in java.lang.Thread

Caused by MissingPropertyException: No such property: SpringSecurityUtils for class: LoginController
->>  123 | propertyMissing in grails.artefact.gsp.TagLibraryInvoker$Trait$Helper
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|     51 | auth      in LoginController
|     62 | doFilter  in grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter
|     53 | doFilter  in grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter
|     62 | doFilter  in grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run       in java.lang.Thread

这就是我得到的......

{{3}}

我在哪里可以找到登录控制器和登录视图?

    import com.akiong.HttpSessionCollector
import com.ryo.security.User
import grails.converters.JSON
import org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.security.authentication.AccountExpiredException
import org.springframework.security.authentication.CredentialsExpiredException
import org.springframework.security.authentication.DisabledException
import org.springframework.security.authentication.LockedException
import org.springframework.security.web.WebAttributes
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter

import javax.servlet.http.HttpServletResponse

class LoginController {

    /**
     * Dependency injection for the authenticationTrustResolver.
     */
    def authenticationTrustResolver

    /**
     * Dependency injection for the springSecurityService.
     */
    def springSecurityService
    def cifService

    private static final Logger logger = LoggerFactory.getLogger(this)

    /**
     * Default action; redirects to 'defaultTargetUrl' if logged in, /login/auth otherwise.
     */
    def index = {
        if (springSecurityService.isLoggedIn()) {
            redirect uri: SpringSecurityUtils.securityConfig.successHandler.defaultTargetUrl
        }
        else {
            redirect action: 'auth', params: params
        }
    }

    /**
     * Show the login page.
     */
    def auth = {

        def config = SpringSecurityUtils.securityConfig

        if (springSecurityService.isLoggedIn()) {
            redirect uri: config.successHandler.defaultTargetUrl
            return
        }

        String view = 'auth'
        String postUrl = "${request.contextPath}${config.apf.filterProcessesUrl}"
        render view: view, model: [postUrl: postUrl,
                                   rememberMeParameter: config.rememberMe.parameter]
    }

    /**
     * The redirect action for Ajax requests.
     */
    def authAjax = {
        response.setHeader 'Location', SpringSecurityUtils.securityConfig.auth.ajaxLoginFormUrl
        response.sendError HttpServletResponse.SC_UNAUTHORIZED
    }

    /**
     * Show denied page.
     */
    def denied = {
        if (springSecurityService.isLoggedIn() &&
                authenticationTrustResolver.isRememberMe(SecurityContextHolder.context?.authentication)) {
            // have cookie but the page is guarded with IS_AUTHENTICATED_FULLY
            redirect action: 'full', params: params
        }
    }

    /**
     * Login page for users with a remember-me cookie but accessing a IS_AUTHENTICATED_FULLY page.
     */
    def full = {
        def config = SpringSecurityUtils.securityConfig
        render view: 'auth', params: params,
            model: [hasCookie: authenticationTrustResolver.isRememberMe(SecurityContextHolder.context?.authentication),
                    postUrl: "${request.contextPath}${config.apf.filterProcessesUrl}"]
    }

    /**
     * Callback after a failed login. Redirects to the auth page with a warning message.
     */
    def authfail = {
        println "failled akiong"
        def username = session[UsernamePasswordAuthenticationFilter.SPRING_SECURITY_LAST_USERNAME_KEY]
        String msg = ''
        def exception = session[WebAttributes.AUTHENTICATION_EXCEPTION]
        if (exception) {
            if (exception instanceof AccountExpiredException) {
                msg = g.message(code: "springSecurity.errors.login.expired")
            }
            else if (exception instanceof CredentialsExpiredException) {
                msg = g.message(code: "springSecurity.errors.login.passwordExpired")
            }
            else if (exception instanceof DisabledException) {
                msg = g.message(code: "springSecurity.errors.login.disabled")
            }
            else if (exception instanceof LockedException) {
                msg = g.message(code: "springSecurity.errors.login.locked")
            }
            else {
                msg = g.message(code: "springSecurity.errors.login.fail")
            }
        }

        if (springSecurityService.isAjax(request)) {
            render([error: msg] as JSON)
        }
        else {
            flash.message = msg
            redirect action: 'auth', params: params
        }
    }

    /**
     * The Ajax success redirect url.
     */
    def ajaxSuccess = {
        // i want edit this and add something in this ajaxSuccess
    }

    private void buildMenuList() {
        String appName = grailsApplication.metadata['app.name']
        StringBuffer contentSB = new StringBuffer();
        def roles = User.get(springSecurityService.getPrincipal().id).getAuthorities()*.getAuthority()
        if (roles.size() > 0) {
            def menus = com.akiong.security.RoleMenu.createCriteria().list{
                projections { distinct("menu") }
                role{ 'in'("authority", roles) }
            }

            def parentMenus = menus.findAll {it.parentMenu}*.parentMenu
            parentMenus.addAll(menus.findAll {it.parentMenu == null})
            parentMenus.unique()
            parentMenus = parentMenus.sort {it.sequence}
            def childMenu
            def root = grailsAttributes.getApplicationUri(request)

            parentMenus.each{ x->
                childMenu = menus.findAll {it.parentMenu && it.parentMenu.equals(x)}.sort {it.sequence}
                if(childMenu){
                    if(!x.url.equals("") && !x.url.equals("/")){
                        contentSB.append("<li class='dropdown'><a class='dropdown-tonggle' data-toggle='dropdown' id='mnImg"+x.id+"' onclick=\"window.location='"+root+x.url+"'\" href='").append(root).append(x.url).append("' class='listMChld'>").append(x.name)
                    }
                    else{
                        contentSB.append("<li class='dropdown' id='mnImg"+x.id+"'><a class='dropdown-tonggle' data-toggle='dropdown'>").append(x.name)
                    }
                    contentSB.append("<b class='caret'></b></a><ul class='dropdown-menu'>")
                    childMenu.each {
                        contentSB.append("<li><a onclick=\"window.location='"+root+it.url+"'\" href='").append(root).append(it.url).append("' class='listMChld'>")
                        contentSB.append(it.name).append("</a></li>")
                    }
                    contentSB.append("</ul></li>")
                }
                else{
                    contentSB.append("<li class='dropdown'><a class='dropdown-tonggle' data-toggle='dropdown' id='mnImg"+x.id+"' onclick=\"window.location='"+root+x.url+"'\" href='").append(root).append(x.url).append("' class='listMChld'>").append(x.name).append("</a></li>")
                }
            }

        }

        session.setAttribute("menuList", contentSB.toString())
    }

    /**
     * The Ajax denied redirect url.
     */
    def ajaxDenied = {
        render([error: 'access denied'] as JSON)
    }

    def concurrentSession = {

        def msg = "Your account is logged in from another browser or location."

        if (springSecurityService.isAjax(request)) {
            render([error: msg] as JSON)
        }
        else {
            flash.message = msg
            redirect action: 'auth', params: params
        }

    }
}

这是我最近使用grails 2.1.1的项目中的logincontroller

顺便问一下,如何编辑登录控制器?

2 个答案:

答案 0 :(得分:1)

如果你已经创建了自己的LoginController,请分享你的LoginController代码,它将有助于调试。理想情况下,您的LoginController应如下所示。

https://github.com/grails-plugins/grails-spring-security-core/blob/master/grails-app/controllers/grails/plugin/springsecurity/LoginController.groovy

答案 1 :(得分:1)

看起来问题出在SpringSecurityUtils的import语句

尝试以下导入语句

import grails.converters.JSON
import grails.plugin.springsecurity.SpringSecurityUtils
import org.springframework.security.authentication.AccountExpiredException
import org.springframework.security.authentication.CredentialsExpiredException
import org.springframework.security.authentication.DisabledException
import org.springframework.security.authentication.LockedException
import org.springframework.security.core.Authentication
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.security.web.WebAttributes

import javax.servlet.http.HttpServletResponse
相关问题