我有一个AngularJS程序,它使用通过GlassFish 4.1提供的RESTful服务。我对web.xml文件中定义的RESTful服务有身份验证要求。在开发过程中,每当网站尝试访问RESTful服务时,我一直依赖GlassFish来启动浏览器身份验证请求,这一切都很好。但是,现在,我想使用ui-router和登录控制器通过HTTP帖子将用户名和密码发送到GlassFish。 HTTP帖子不起作用,所以在此期间我刚刚关闭了GlassFish端的身份验证要求,并且假装成功登录GlassFish进行模拟登录,然后ui-router接受在用户登录"后完成剩下的工作。身份验证后的路由工作非常好,所以没有问题。我的问题归结为:如何在访问服务时需要身份验证的当前配置中将用户名和密码正确发送到GlassFish服务器?下面是web.xml和登录控制器。谢谢你的帮助。
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<security-constraint>
<web-resource-collection>
<web-resource-name>Enforce TLS</web-resource-name>
<description/>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<!-- require SSL -->
<description/>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<security-constraint>
<web-resource-collection>
<web-resource-name>Secure Pages</web-resource-name>
<description/>
<url-pattern>/tsn/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>tsnadmin</role-name>
<role-name>tester</role-name>
</auth-constraint>
<user-data-constraint>
<description/>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>tsnRealm</realm-name>
</login-config>
<security-role>
<role-name>tsnadmin</role-name>
</security-role>
<security-role>
<role-name>tester</role-name>
</security-role>
</web-app>
控制器:
'use strict';
angular.module('pisuiteClientExpApp')
.controller('LoginModalCtrl', function (
$scope,
$timeout,
$http,
userRoles,
auagByUserSvc) {
this.cancel = $scope.$dismiss;
/*this.submit = function () {
$scope.dataLoading = true;
$timeout(function () {
$scope.user;
if ($scope.username === 'rpurvis') {
$scope.user = {uname: 'rpurvis', role: userRoles.tester,
success: $scope.username === 'rpurvis' && $scope.password === 'password'};
} else if ($scope.username === userRoles.admin) {
$scope.user = {uname: userRoles.admin, role: userRoles.admin,
success: $scope.username === userRoles.admin && $scope.password === 'password'};
}
if (!$scope.user.success) {
$scope.user.message = 'Username or password is incorrect';
$scope.user = null;
$scope.$close($scope.user);
} else {
auagByUserSvc.get({user: $scope.user.uname}, function (auag_success) {
/*console.log("setting images to an array of length: " + data.length)
if (auag_success.length > 0) {
$scope.user.role = auag_success[0].idAuthGroup.idAuthGroup;
$scope.user.inspectorid = auag_success[0].idAuthUser.inspectorid;
}
$scope.$close($scope.user);
}, function (error) {
$scope.$close($scope.user);
});
}
}, 1000);*/
var req = {
method: 'POST',
url: 'https://localhost:9191/PISuiteService_Exp/',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function (obj) {
var str = [];
for (var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data: {username: $scope.username, password: $scope.password}
};
$http(req).then(function (success) {
$scope.user = {uname: $scope.username};
console.log("in post success");
auagByUserSvc.get({user: $scope.username}, function (auag_success) {
if (auag_success.length > 0) {
$scope.user.role = auag_success[0].idAuthGroup.idAuthGroup;
$scope.user.inspectorid = auag_success[0].idAuthUser.inspectorid;
}
$scope.$close($scope.user);
}, function (error) {
$scope.$close($scope.user);
});
}, function (error) {
console.log("in post error");
$scope.user.message = 'Username or password is incorrect';
$scope.user = null;
$scope.$close($scope.user);
}, function (progress) {
console.log("in post progress");
});
};
});
答案 0 :(得分:0)
要使用REST服务器进行身份验证,您需要在每个请求的标头中发送授权令牌。令牌的值以BASIC
开头"Authorization" = "Basic " + btoa(username + ":" + password)
以下是使用jQuery的示例:How to use Basic Auth with jQuery and AJAX?。
使用angular,您可以像这样创建您的请求(查看标题字段):
var req = {
method: 'POST',
url: 'https://localhost:9191/PISuiteService_Exp/',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + btoa($scope.username + ':' + $scope.password),
},
transformRequest: function (obj) {
var str = [];
for (var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
}
};
建议使用REST服务进行上述无状态身份验证,因为根据定义,REST服务应该是无状态的。
但是,您可能希望在会话开始时进行一次身份验证。这可能是您最初的原因,因为GlassFish在未经过身份验证时会自动触发登录表单,并保留会话其余部分的登录信息。
在这种情况下,您可以使用基于表单的身份验证,以便通过POST请求发送登录数据。有关详细信息,请查看Java EE tutorial,我将从那里发布表单示例:
<form method="POST" action="j_security_check">
<input type="text" name="j_username">
<input type="password" name="j_password">
</form>