为什么PicketLink会使用令牌进行基本浏览器身份验证?

时间:2015-03-10 12:47:35

标签: angularjs picketlink

我有一个使用PicketLin(无状态)k + WildFly + Angular的令牌的简单登录系统。

问题是:当我输错密码时,服务器会向我发送标题:

WWW-Authenticate:Basic realm="PicketLink Default Realm"

这会导致浏览器打开基本身份验证窗口。我需要使用表单,而不是默认的浏览验证窗口;我尝试在angular中创建一个拦截器来尝试停止,但是浏览器在任何事情之前拦截并解释标题。

这是PicketLink的配置:

SecurityConfigurationBuilder builder = event.getBuilder();
builder.identity().stateless().http()
        .forPath("/api/*").authenticateWith().token();

这是Realm和证书(用于令牌生成)代码的代码: (省略了不相关的方法)

...
private void createDefaultPartition(PartitionManager partitionManager) {

    Realm partition = partitionManager.getPartition(Realm.class, Realm.DEFAULT_REALM);

    if (partition == null) {
        try {
            partition = new Realm(Realm.DEFAULT_REALM);
            partition.setAttribute(new Attribute<byte[]>("PublicKey", getPublicKey()));
            partition.setAttribute(new Attribute<byte[]>("PrivateKey",getPrivateKey()));
            partitionManager.add(partition);
        } catch (Exception e) {
            //log...
        }
    }
}


public void createAdminAccount(PartitionManager partitionManager) {
    IdentityManager identityManager = partitionManager.createIdentityManager();
    User user = new User("symon");
    user.setEmail("symoncc@gmail.com");
    user.setFirstName("Symon");
    user.setLastName("Lopes");
    identityManager.add(user);
    identityManager.updateCredential(user, new Password("123456"));
}

登录代码:

...
<body ng-app="app" >
    <div ng-controller="LoginController">

        <label>Usuário</label><br />
        <input type="text" id="usuario" ng-model="usuario.nome" /> <br />

        <label>Senha</label><br />
        <input type="text" id="senha" ng-model="usuario.senha" /> <br />

        <input type="button" value="logar" ng-click="logar()" />

        <div ng-show="loginResult">
            {{loginResult}}
        </div>
    </div>
</body>
...

app js code:

  (function(){

var app = angular.module('app',['ngRoute']);
var session = {};

app.config(['$routeProvider',function($routeProvider) {
    $routeProvider
        .when('/protected', {templateUrl:'partials/protected.html', controller: 'ProtectedController' })
}]);


app.controller('LoginController', ['$scope','$http',function($scope,$http) {
    $scope.usuario =  {nome:'symon', senha:'123456'};

    $scope.logar = function(){
        //$http.defaults.headers.common.Authorization = 'Basic ' + btoa($scope.usuario.nome + ':' + $scope.usuario.senha);
        $http.post('api/private/authc', {},{headers: { 'Authorization' : 'Basic ' + btoa($scope.usuario.nome + ':' + $scope.usuario.senha) }})
            .success(function(auth){
                $scope.loginResult = 'Autenticação efetuada com sucesso';
                session.token =auth.authctoken
                $http.get('api/pessoas',{headers: { 'Authorization' : 'Token ' +  session.token }}).success(
                        function(dados){
                            console.log(dados);
                        });             
            })
            .error(function(data){
                $scope.loginResult = 'Falha ao tentar fazer login: ' + data;                    
            });
    };      
}]);

})();

1 个答案:

答案 0 :(得分:1)

要开始工作,您应该在登录资源中添加另一个标题,如下所示:&#34; X-Requested-With&#34;:&#34; XMLHttpRequest&#34; 。 修改角度security.js文件,如下所示:

.factory('LoginResource', ['$resource', function($resource) {  
   return function(newUser) {  
   return $resource('rest/private/:dest', {}, {  
   login: {method: 'POST', params: {dest:"authc"}, headers:{"Authorization": "Basic " + btoa(newUser.userId + ":" + newUser.password), "X-Requested-With": "XMLHttpRequest"  
   } }  
  });  
}}]);

我希望这会有所帮助。 ;)

相关问题