Grails 3, Spring Security Rest username after login











up vote
0
down vote

favorite












I have a Grails 3 application with Spring Security Rest implemented, I can authenticate from my Angular2 app successfully, and with @Secured() applied to the controllers can control which areas of the application can be accessed. So given this, I know that the authentication and authorization is working.



I'm trying to retrieve the logged in user's username that was used to authenticate.



I've tried Authentication authentication = SecurityContextHolder.getContext().getAuthentication()
it's null.



I tried
Principal principal = request.getUserPrincipal()
it's also null.



Here's my application.groovy



    grails.plugin.springsecurity.providerNames = ['customADAuthProvider']
grails.plugin.springsecurity.successHandler.alwaysUseDefault = true
grails.plugin.springsecurity.userLookup.userDomainClassName = 'User'
grails.plugin.springsecurity.userLookup.authorityJoinClassName = 'UserRole'
grails.plugin.springsecurity.authority.className = 'Role'
grails.plugin.springsecurity.controllerAnnotations.staticRules = [
[pattern: '/', access: ['permitAll']],
[pattern: '/error', access: ['permitAll']],
[pattern: '/index', access: ['permitAll']],
[pattern: '/index.gsp', access: ['permitAll']],
[pattern: '/shutdown', access: ['permitAll']],
[pattern: '/assets/**', access: ['permitAll']],
[pattern: '/**/js/**', access: ['permitAll']],
[pattern: '/**/css/**', access: ['permitAll']],
[pattern: '/**/images/**', access: ['permitAll']],
[pattern: '/**/favicon.ico', access: ['permitAll']]
]

grails.plugin.springsecurity.filterChain.chainMap = [
[pattern: '/assets/**', filters: 'none'],
[pattern: '/**/js/**', filters: 'none'],
[pattern: '/**/css/**', filters: 'none'],
[pattern: '/**/images/**', filters: 'none'],
[pattern: '/**/favicon.ico', filters: 'none'],
[pattern: '/api/**', filters: 'JOINED_FILTERS,-anonymousAuthenticationFilter,-exceptionTranslationFilter,-authenticationProcessingFilter,-securityContextPersistenceFilter,-rememberMeAuthenticationFilter']
]

grails.plugin.springsecurity.rest.logout.endpointUrl = '/api/logout'


No matter what I try, I can never get the authentication object or principal object to retrieve the username or any user details.



I'm using spring-security-rest:2.0.0.RC1, and Grails 3.3.6










share|improve this question


























    up vote
    0
    down vote

    favorite












    I have a Grails 3 application with Spring Security Rest implemented, I can authenticate from my Angular2 app successfully, and with @Secured() applied to the controllers can control which areas of the application can be accessed. So given this, I know that the authentication and authorization is working.



    I'm trying to retrieve the logged in user's username that was used to authenticate.



    I've tried Authentication authentication = SecurityContextHolder.getContext().getAuthentication()
    it's null.



    I tried
    Principal principal = request.getUserPrincipal()
    it's also null.



    Here's my application.groovy



        grails.plugin.springsecurity.providerNames = ['customADAuthProvider']
    grails.plugin.springsecurity.successHandler.alwaysUseDefault = true
    grails.plugin.springsecurity.userLookup.userDomainClassName = 'User'
    grails.plugin.springsecurity.userLookup.authorityJoinClassName = 'UserRole'
    grails.plugin.springsecurity.authority.className = 'Role'
    grails.plugin.springsecurity.controllerAnnotations.staticRules = [
    [pattern: '/', access: ['permitAll']],
    [pattern: '/error', access: ['permitAll']],
    [pattern: '/index', access: ['permitAll']],
    [pattern: '/index.gsp', access: ['permitAll']],
    [pattern: '/shutdown', access: ['permitAll']],
    [pattern: '/assets/**', access: ['permitAll']],
    [pattern: '/**/js/**', access: ['permitAll']],
    [pattern: '/**/css/**', access: ['permitAll']],
    [pattern: '/**/images/**', access: ['permitAll']],
    [pattern: '/**/favicon.ico', access: ['permitAll']]
    ]

    grails.plugin.springsecurity.filterChain.chainMap = [
    [pattern: '/assets/**', filters: 'none'],
    [pattern: '/**/js/**', filters: 'none'],
    [pattern: '/**/css/**', filters: 'none'],
    [pattern: '/**/images/**', filters: 'none'],
    [pattern: '/**/favicon.ico', filters: 'none'],
    [pattern: '/api/**', filters: 'JOINED_FILTERS,-anonymousAuthenticationFilter,-exceptionTranslationFilter,-authenticationProcessingFilter,-securityContextPersistenceFilter,-rememberMeAuthenticationFilter']
    ]

    grails.plugin.springsecurity.rest.logout.endpointUrl = '/api/logout'


    No matter what I try, I can never get the authentication object or principal object to retrieve the username or any user details.



    I'm using spring-security-rest:2.0.0.RC1, and Grails 3.3.6










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a Grails 3 application with Spring Security Rest implemented, I can authenticate from my Angular2 app successfully, and with @Secured() applied to the controllers can control which areas of the application can be accessed. So given this, I know that the authentication and authorization is working.



      I'm trying to retrieve the logged in user's username that was used to authenticate.



      I've tried Authentication authentication = SecurityContextHolder.getContext().getAuthentication()
      it's null.



      I tried
      Principal principal = request.getUserPrincipal()
      it's also null.



      Here's my application.groovy



          grails.plugin.springsecurity.providerNames = ['customADAuthProvider']
      grails.plugin.springsecurity.successHandler.alwaysUseDefault = true
      grails.plugin.springsecurity.userLookup.userDomainClassName = 'User'
      grails.plugin.springsecurity.userLookup.authorityJoinClassName = 'UserRole'
      grails.plugin.springsecurity.authority.className = 'Role'
      grails.plugin.springsecurity.controllerAnnotations.staticRules = [
      [pattern: '/', access: ['permitAll']],
      [pattern: '/error', access: ['permitAll']],
      [pattern: '/index', access: ['permitAll']],
      [pattern: '/index.gsp', access: ['permitAll']],
      [pattern: '/shutdown', access: ['permitAll']],
      [pattern: '/assets/**', access: ['permitAll']],
      [pattern: '/**/js/**', access: ['permitAll']],
      [pattern: '/**/css/**', access: ['permitAll']],
      [pattern: '/**/images/**', access: ['permitAll']],
      [pattern: '/**/favicon.ico', access: ['permitAll']]
      ]

      grails.plugin.springsecurity.filterChain.chainMap = [
      [pattern: '/assets/**', filters: 'none'],
      [pattern: '/**/js/**', filters: 'none'],
      [pattern: '/**/css/**', filters: 'none'],
      [pattern: '/**/images/**', filters: 'none'],
      [pattern: '/**/favicon.ico', filters: 'none'],
      [pattern: '/api/**', filters: 'JOINED_FILTERS,-anonymousAuthenticationFilter,-exceptionTranslationFilter,-authenticationProcessingFilter,-securityContextPersistenceFilter,-rememberMeAuthenticationFilter']
      ]

      grails.plugin.springsecurity.rest.logout.endpointUrl = '/api/logout'


      No matter what I try, I can never get the authentication object or principal object to retrieve the username or any user details.



      I'm using spring-security-rest:2.0.0.RC1, and Grails 3.3.6










      share|improve this question













      I have a Grails 3 application with Spring Security Rest implemented, I can authenticate from my Angular2 app successfully, and with @Secured() applied to the controllers can control which areas of the application can be accessed. So given this, I know that the authentication and authorization is working.



      I'm trying to retrieve the logged in user's username that was used to authenticate.



      I've tried Authentication authentication = SecurityContextHolder.getContext().getAuthentication()
      it's null.



      I tried
      Principal principal = request.getUserPrincipal()
      it's also null.



      Here's my application.groovy



          grails.plugin.springsecurity.providerNames = ['customADAuthProvider']
      grails.plugin.springsecurity.successHandler.alwaysUseDefault = true
      grails.plugin.springsecurity.userLookup.userDomainClassName = 'User'
      grails.plugin.springsecurity.userLookup.authorityJoinClassName = 'UserRole'
      grails.plugin.springsecurity.authority.className = 'Role'
      grails.plugin.springsecurity.controllerAnnotations.staticRules = [
      [pattern: '/', access: ['permitAll']],
      [pattern: '/error', access: ['permitAll']],
      [pattern: '/index', access: ['permitAll']],
      [pattern: '/index.gsp', access: ['permitAll']],
      [pattern: '/shutdown', access: ['permitAll']],
      [pattern: '/assets/**', access: ['permitAll']],
      [pattern: '/**/js/**', access: ['permitAll']],
      [pattern: '/**/css/**', access: ['permitAll']],
      [pattern: '/**/images/**', access: ['permitAll']],
      [pattern: '/**/favicon.ico', access: ['permitAll']]
      ]

      grails.plugin.springsecurity.filterChain.chainMap = [
      [pattern: '/assets/**', filters: 'none'],
      [pattern: '/**/js/**', filters: 'none'],
      [pattern: '/**/css/**', filters: 'none'],
      [pattern: '/**/images/**', filters: 'none'],
      [pattern: '/**/favicon.ico', filters: 'none'],
      [pattern: '/api/**', filters: 'JOINED_FILTERS,-anonymousAuthenticationFilter,-exceptionTranslationFilter,-authenticationProcessingFilter,-securityContextPersistenceFilter,-rememberMeAuthenticationFilter']
      ]

      grails.plugin.springsecurity.rest.logout.endpointUrl = '/api/logout'


      No matter what I try, I can never get the authentication object or principal object to retrieve the username or any user details.



      I'm using spring-security-rest:2.0.0.RC1, and Grails 3.3.6







      grails spring-security spring-security-rest






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 at 21:33









      idonaldson

      1901128




      1901128
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          You can do something like this...



          class SomeController {

          def springSecurityService

          def someAction() {
          def user = springSecurityService.currentUser
          // ...
          }
          }


          Or this...



          class SomeController {

          def springSecurityService

          def someAction() {
          def principal = springSecurityService.principal
          String username = principal.username
          def authorities = principal.authorities // a Collection of GrantedAuthority
          boolean enabled = principal.enabled
          // ...
          }
          }


          Both of those are pasted directly from https://grails-plugins.github.io/grails-spring-security-core/3.2.x/index.html






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420767%2fgrails-3-spring-security-rest-username-after-login%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            You can do something like this...



            class SomeController {

            def springSecurityService

            def someAction() {
            def user = springSecurityService.currentUser
            // ...
            }
            }


            Or this...



            class SomeController {

            def springSecurityService

            def someAction() {
            def principal = springSecurityService.principal
            String username = principal.username
            def authorities = principal.authorities // a Collection of GrantedAuthority
            boolean enabled = principal.enabled
            // ...
            }
            }


            Both of those are pasted directly from https://grails-plugins.github.io/grails-spring-security-core/3.2.x/index.html






            share|improve this answer

























              up vote
              1
              down vote













              You can do something like this...



              class SomeController {

              def springSecurityService

              def someAction() {
              def user = springSecurityService.currentUser
              // ...
              }
              }


              Or this...



              class SomeController {

              def springSecurityService

              def someAction() {
              def principal = springSecurityService.principal
              String username = principal.username
              def authorities = principal.authorities // a Collection of GrantedAuthority
              boolean enabled = principal.enabled
              // ...
              }
              }


              Both of those are pasted directly from https://grails-plugins.github.io/grails-spring-security-core/3.2.x/index.html






              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                You can do something like this...



                class SomeController {

                def springSecurityService

                def someAction() {
                def user = springSecurityService.currentUser
                // ...
                }
                }


                Or this...



                class SomeController {

                def springSecurityService

                def someAction() {
                def principal = springSecurityService.principal
                String username = principal.username
                def authorities = principal.authorities // a Collection of GrantedAuthority
                boolean enabled = principal.enabled
                // ...
                }
                }


                Both of those are pasted directly from https://grails-plugins.github.io/grails-spring-security-core/3.2.x/index.html






                share|improve this answer












                You can do something like this...



                class SomeController {

                def springSecurityService

                def someAction() {
                def user = springSecurityService.currentUser
                // ...
                }
                }


                Or this...



                class SomeController {

                def springSecurityService

                def someAction() {
                def principal = springSecurityService.principal
                String username = principal.username
                def authorities = principal.authorities // a Collection of GrantedAuthority
                boolean enabled = principal.enabled
                // ...
                }
                }


                Both of those are pasted directly from https://grails-plugins.github.io/grails-spring-security-core/3.2.x/index.html







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 at 23:03









                Jeff Scott Brown

                14.7k11731




                14.7k11731






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420767%2fgrails-3-spring-security-rest-username-after-login%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    What visual should I use to simply compare current year value vs last year in Power BI desktop

                    Alexandru Averescu

                    Trompette piccolo