使用grails app中的spring security rest插件调用登录时发生401未经授权的错误

时间:2014-06-15 09:10:09

标签: angularjs grails spring-security angular-http-interceptors

我有一个带有mongodb的grails应用程序作为带有spring security rest插件的数据源。使用angularjs,我试图用$ http.post调用登录用户,但在浏览器控制台上获得401未经授权的错误。

以下是spring security rest插件的grail配置:

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
] 

我打电话通过angularjs $ http服务:

$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);
            });

错误:

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

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

解决了......

启用调试模式后..我开始知道弹簧安全性是一个残疾人..所以在查看我的人类后,我发现我错过了这些属性:

boolean enabled = true
boolean accountExpired
boolean accountLocked
boolean passwordExpired

所以在添加它们之后,问题就解决了。

答案 1 :(得分:0)

我的情况,我修改了Role类。由于该类的String authority属性只能拥有一组已定义的权限,因此我将其更改为Enum列,即

class Role implements Serializable {

    private static final long serialVersionUID = 1

    String id
    Authority authority
    Date dateCreated
    Date lastUpdated

    static mapping = {
        authority enumType: 'identity'
    }
}

我花了整整一个工作日来通过将权限属性返回到String类型来捕获并修复此问题。 :(

不要在这些课程中乱七八糟。