Search code examples
angularjsgrailsspring-securityangular-http-interceptors

401 unauthorized error while call to login with spring security rest plugin in grails app


I have a grails app with mongodb as datasource with spring security rest plugin. Using angularjs, I am trying to login a user with $http.post call, but getting 401 unauthorized error on browser console.

Here is my grail configuration for spring security rest plugin:

grails.plugin.springsecurity.userLookup.userDomainClassName = 'projmgmt.Person'
grails.plugin.springsecurity.userLookup.authorityJoinClassName =  'projmgmt.security.PersonRole'
grails.plugin.springsecurity.authority.className = 'projmgmt.security.Role'
grails.plugin.springsecurity.securityConfigType = 'InterceptUrlMap'
grails.plugin.springsecurity.interceptUrlMap = [
    '/':                    ['permitAll'],
    '/assets/**':           ['permitAll'],
    '/partials/**':         ['permitAll'],
    '/api/**':              ['permitAll'],
    '/auth/api/**':         ['permitAll'],
    '/person/**':           ['permitAll'],
    '/**':                  ['isFullyAuthenticated()']
]

grails.plugin.springsecurity.rememberMe.persistent = false
grails.plugin.springsecurity.rest.login.useJsonCredentials = true
grails.plugin.springsecurity.rest.login.endpointUrl =   '/auth/api/login'
grails.plugin.springsecurity.rest.logout.endpointUrl =  '/auth/api/logout'
grails.plugin.springsecurity.rest.token.storage.useGorm = true
grails.plugin.springsecurity.rest.token.storage.gorm.tokenDomainClassName = 'projmgmt.security.AuthenticationToken'
grails.plugin.springsecurity.rest.token.storage.gorm.tokenValuePropertyName = 'token'
grails.plugin.springsecurity.rest.token.storage.gorm.usernamePropertyName = 'username'

grails.plugin.springsecurity.filterChain.chainMap = [
    '/auth/api/**': 'JOINED_FILTERS,-exceptionTranslationFilter,-authenticationProcessingFilter,-securityContextPersistenceFilter',  // Stateless chain
    '/api/**': 'JOINED_FILTERS,-exceptionTranslationFilter,-authenticationProcessingFilter,-securityContextPersistenceFilter',  // Stateless chain
    '/**': 'JOINED_FILTERS,-restTokenValidationFilter,-restExceptionTranslationFilter'                                          // Traditional chain
] 

And my call to login via angularjs $http service:

$http.post('auth/api/login', { username: $scope.authData.username, password:     $scope.authData.password }, getAuthenticateHttpConfig).
            success(function (data) {

                $rootScope.isAuthenticated = true;
                $rootScope.currentUser = data.username;
                setLocalToken(data.token);
                authService.loginConfirmed({}, function (config) {
                    if (!config.headers["X-Auth-Token"]) {
                        console.log('X-Auth-Token not on original request; adding it');
                        config.headers["X-Auth-Token"] = getLocalToken();
                    }
                    return config;
                });
            }).
            error(function (data) {
                console.log('login error: ' + data);
                $rootScope.$broadcast('event:auth-loginFailed', data);
            });

Error:

 POST http://localhost:8080/projmgmt_server/auth/api/login 401 (Unauthorized) 

Any suggestions?


Solution

  • Solved....

    After enabling the debug mode.. i came to know that the spring security was gettng a disabled person.. so after looking into my person class I found i was missing these properties:

    boolean enabled = true
    boolean accountExpired
    boolean accountLocked
    boolean passwordExpired
    

    So after adding them, the issue got solved.