Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6795583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:21:51+00:00 2026-05-26T18:21:51+00:00

Upon creating new users in my system, I am sending them a temporary password

  • 0

Upon creating new users in my system, I am sending them a temporary password via email and setting an property of changePasswordNextLogin=true. When they come to log in for the first time, I would like to intercept the flow upon a successful login, check for this this value, and if it is true, redirect them to a change password action. Once the password change has been completed, ideally I would like to send them to their intended destination.

I have been pouring through the default settings and am not seeing – or more likely not interpreting properly – any way to make that happen. It seems that almost every time that I try to cobble some solution together in Grails, I find that someone has already made a much more elegant approach to do the same thing. Is there any functionality built in that would allow this?

If not, I would really appreciate any suggestions on the best approach to make it so.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:21:51+00:00Added an answer on May 26, 2026 at 6:21 pm

    There is some support for this directly with Spring Security and the grails plugin, but you also have to do some work yourself 🙂

    The domain class that was created when you installed grails-spring-security plugin (and ran the S2Quickstart script) has a property on it named ‘passwordExpired’. Set this to true when you create your new user domain instance.

    Once that user logs in for the first time, the Spring Security core libs will throw an exception which you can catch in your login controller’s authfail closure, re-directing them to the change password form (that you need to supply yourself).

    Here’s an example from one of my apps, a skeleton version of this closure should already be included in your login controller:

    /**
     * Callback after a failed login.
     */
    def authfail = {
    
        def msg = ''
    
        def username = 
           session[UsernamePasswordAuthenticationFilter.SPRING_SECURITY_LAST_USERNAME_KEY]
    
        def exception = session[WebAttributes.AUTHENTICATION_EXCEPTION]
    
        if (exception) {
            if (exception instanceof CredentialsExpiredException) {
                msg = g.message(code: "springSecurity.errors.login.passwordExpired")
                if (!springSecurityService.isAjax(request))
                    redirect (action:'changePassword') // <-- see below
            }
            // other failure checks omitted
        }
    
        if (springSecurityService.isAjax(request)) {
            render([error: msg] as JSON)
        }
        else {
            flash.message = msg
            redirect controller: 'login', action:'auth', params: params
        }
    }
    
    /**
     * render the change pwd form
     */
    def changePassword = {
        [username: session[UsernamePasswordAuthenticationFilter.SPRING_SECURITY_LAST_USERNAME_KEY] ?: springSecurityService.authentication.name]
    }
    

    From your ‘changePasssword’ view, submit the form back to another controller closure (I call mine ‘updatePassword’ that checks whatever constraints you want for passwords and either saves the updated password on the domain object or not..

    def updatePassword = {
        String username = session[UsernamePasswordAuthenticationFilter.SPRING_SECURITY_LAST_USERNAME_KEY] ?: springSecurityService.authentication.name
        if (!username) {
            flash.message = 'Sorry, an error has occurred'
            redirect controller: 'login', action:'auth'
            return
        }
        String password = params.password
        String newPassword = params.password_new
        String newPassword2 = params.password_new_2
        if (!password || !newPassword || !newPassword2 || newPassword != newPassword2) {
            flash.message = 'Please enter your current password and a new password'
            render view: 'changePassword', model: [username: username]
            return
        }
        SecUser user = SecUser.findByUsername(username)
        if (!passwordEncoder.isPasswordValid(user.password, password, null /*salt*/)) {
            flash.message = 'Current password is incorrect'
            render view: 'changePassword', model: [username: username]
            return
        }
        if (passwordEncoder.isPasswordValid(user.password, newPassword, null /*salt*/)) {
            flash.message = 'Please choose a different password from your current one'
            render view: 'changePassword', model: [username: username]
            return
        }
        if (!newPassword.matches(PASSWORD_REGEX)) {
            flash.message = 'Password does not meet minimum requirements'
            render view: 'changePassword', model: [username: username]
            return            
        }
    
        // success if we reach here!
        user.password = springSecurityService.encodePassword(newPassword)
        user.passwordExpired = false
        user.save() 
    
        flash.message = 'Password changed successfully' + (springSecurityService.loggedIn ? '' : ', you can now login')
        redirect uri: '/'
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a form, which will send out the details via email upon
I've been tasked with creating a One Time Password (OTP) system which will eventually
Upon creating a new ASP.NET MVC 3 Razor site, there are 9 Account\*.cshtml files
I'm creating a feedback system kind of like eBay. (Once you buy the item
I am creating a gridview that will be populated based upon a linq statement,
I'm developing a piece in VB.NET. Inside my primary form, I'm creating a new
I'm new to MFC and creating basically a preferences tool. This of course means
Scenario: I'm creating a new window (new tab) and writing html markup to it
I have a simple Model that is the default RegisterModel and upon creating a
I'm new to JQuery and I'm creating a site where one can click on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.