|
| 1 | +import grails.converters.JSON |
| 2 | + |
| 3 | +import javax.servlet.http.HttpServletResponse |
| 4 | + |
| 5 | +import org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils |
| 6 | + |
| 7 | +import org.springframework.security.authentication.AccountExpiredException |
| 8 | +import org.springframework.security.authentication.CredentialsExpiredException |
| 9 | +import org.springframework.security.authentication.DisabledException |
| 10 | +import org.springframework.security.authentication.LockedException |
| 11 | +import org.springframework.security.core.context.SecurityContextHolder as SCH |
| 12 | +import org.springframework.security.web.WebAttributes |
| 13 | +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter |
| 14 | + |
| 15 | +class LoginController { |
| 16 | + |
| 17 | + /** |
| 18 | + * Dependency injection for the authenticationTrustResolver. |
| 19 | + */ |
| 20 | + def authenticationTrustResolver |
| 21 | + |
| 22 | + /** |
| 23 | + * Dependency injection for the springSecurityService. |
| 24 | + */ |
| 25 | + def springSecurityService |
| 26 | + |
| 27 | + /** |
| 28 | + * Default action; redirects to 'defaultTargetUrl' if logged in, /login/auth otherwise. |
| 29 | + */ |
| 30 | + def index = { |
| 31 | + if (springSecurityService.isLoggedIn()) { |
| 32 | + redirect uri: SpringSecurityUtils.securityConfig.successHandler.defaultTargetUrl |
| 33 | + } |
| 34 | + else { |
| 35 | + redirect action: 'auth', params: params |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Show the login page. |
| 41 | + */ |
| 42 | + def auth = { |
| 43 | + |
| 44 | + def config = SpringSecurityUtils.securityConfig |
| 45 | + |
| 46 | + if (springSecurityService.isLoggedIn()) { |
| 47 | + redirect uri: config.successHandler.defaultTargetUrl |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + String view = 'auth' |
| 52 | + String postUrl = "${request.contextPath}${config.apf.filterProcessesUrl}" |
| 53 | + render view: view, model: [postUrl: postUrl, |
| 54 | + rememberMeParameter: config.rememberMe.parameter] |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * The redirect action for Ajax requests. |
| 59 | + */ |
| 60 | + def authAjax = { |
| 61 | + response.setHeader 'Location', SpringSecurityUtils.securityConfig.auth.ajaxLoginFormUrl |
| 62 | + response.sendError HttpServletResponse.SC_UNAUTHORIZED |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Show denied page. |
| 67 | + */ |
| 68 | + def denied = { |
| 69 | + if (springSecurityService.isLoggedIn() && |
| 70 | + authenticationTrustResolver.isRememberMe(SCH.context?.authentication)) { |
| 71 | + // have cookie but the page is guarded with IS_AUTHENTICATED_FULLY |
| 72 | + redirect action: 'full', params: params |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Login page for users with a remember-me cookie but accessing a IS_AUTHENTICATED_FULLY page. |
| 78 | + */ |
| 79 | + def full = { |
| 80 | + def config = SpringSecurityUtils.securityConfig |
| 81 | + render view: 'auth', params: params, |
| 82 | + model: [hasCookie: authenticationTrustResolver.isRememberMe(SCH.context?.authentication), |
| 83 | + postUrl: "${request.contextPath}${config.apf.filterProcessesUrl}"] |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Callback after a failed login. Redirects to the auth page with a warning message. |
| 88 | + */ |
| 89 | + def authfail = { |
| 90 | + |
| 91 | + def username = session[UsernamePasswordAuthenticationFilter.SPRING_SECURITY_LAST_USERNAME_KEY] |
| 92 | + String msg = '' |
| 93 | + def exception = session[WebAttributes.AUTHENTICATION_EXCEPTION] |
| 94 | + if (exception) { |
| 95 | + if (exception instanceof AccountExpiredException) { |
| 96 | + msg = g.message(code: "springSecurity.errors.login.expired") |
| 97 | + } |
| 98 | + else if (exception instanceof CredentialsExpiredException) { |
| 99 | + msg = g.message(code: "springSecurity.errors.login.passwordExpired") |
| 100 | + } |
| 101 | + else if (exception instanceof DisabledException) { |
| 102 | + msg = g.message(code: "springSecurity.errors.login.disabled") |
| 103 | + } |
| 104 | + else if (exception instanceof LockedException) { |
| 105 | + msg = g.message(code: "springSecurity.errors.login.locked") |
| 106 | + } |
| 107 | + else { |
| 108 | + msg = g.message(code: "springSecurity.errors.login.fail") |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + if (springSecurityService.isAjax(request)) { |
| 113 | + render([error: msg] as JSON) |
| 114 | + } |
| 115 | + else { |
| 116 | + flash.message = msg |
| 117 | + redirect action: 'auth', params: params |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * The Ajax success redirect url. |
| 123 | + */ |
| 124 | + def ajaxSuccess = { |
| 125 | + render([success: true, username: springSecurityService.authentication.name] as JSON) |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * The Ajax denied redirect url. |
| 130 | + */ |
| 131 | + def ajaxDenied = { |
| 132 | + render([error: 'access denied'] as JSON) |
| 133 | + } |
| 134 | +} |
0 commit comments