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

  • Home
  • SEARCH
  • 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 8013901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:45:59+00:00 2026-06-04T19:45:59+00:00

One controller in my app needs to be accessible by users authenticated against an

  • 0

One controller in my app needs to be accessible by users authenticated against an external database.

I have set up a custom user object,

class CustomUserDetails extends GrailsUser {

    final String externalId

    CustomUserDetails(String username, String password, boolean enabled,
        boolean accountNonExpired, boolean credentialsNonExpired,
        boolean accountNonLocked,
        Collection<GrantedAuthority> authorities,
        long id, String externalId) {

        super(username, password, enabled, accountNonExpired,
        credentialsNonExpired, accountNonLocked, authorities, id)

        this.externalId = externalId 
    }       
}

and a custom AuthenticationProvider

class CustomAuthenticationProvider implements AuthenticationProvider {
    def springSecurityService

    Authentication authenticate(Authentication customAuth) {

    /* Do stuff to validate the user's credentials here */

        def userDetails = new CustomUserDetails(customAuth.getPrincipal(), customAuth.getCredentials(), true, true, true, true, 
                [new GrantedAuthorityImpl('ROLE_SPECIAL_USER')], 9999999, "externalDatabaseIdString")

        def token = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.authorities)

        return token
    }

    boolean supports(Class authentication) {
        return true
    }
}

I’ve made entries in Config.groovy to add this to the springsecurity.providerNames list, and added the following to conf/spring/resources.groovy

beans = {
customAuthenticationProvider(info.proadvisors.auth.CustomAuthenticationProvider){ bean ->   bean.autowire = "byName" }

userDetailsService(org.codehaus.groovy.grails.plugins.springsecurity.GormUserDetailsService){ bean -> bean.autowire = "byName" }
}

Here’s the problem – in my controller, springSecurityService is being injected but springSecurityService.getCurrentUser() is null, and returns a null pointer exception when I try to access the externalId property that should be on the authenticated user object.

If, in my CustomAuthenticationProvider, instead of creating an instance of CustomUserDetails I use GormUserDetailsService to give me a GrailsUser object and use that to build the token, the controller works properly and getCurrentUser() works.

Any ideas on why this isn’t working?

  • 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-06-04T19:46:02+00:00Added an answer on June 4, 2026 at 7:46 pm

    springSecurityService.getPrincipal() gives me what I’m looking for.

    Not sure why getCurrentUser() doesn’t work while getPrincpal() does, but it is what it is.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET MVC web app and in one of the controller actions
I have a simple Rails 3 app: no models, one view and one controller
I'm trying to pass an object from one controller action to another. The object
I have an integer variable ( time ) in one view controller whose value
I have the CakePHP stack in /var/www/site from one view under controller A I
There is a purchasing controller in our rails 3.1.0 app. A purchasing needs 4
I have a Winforms app which runs under a standard user account (i.e. user
I have an MVC 2 app that has a System.Timers.Timer object starting up during
I have an ASP MVC app that uses it's own custom authentication mechanism. However
I have 2 Controllers within one app as I am trying out these different

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.