Angularjs 正在为angular js应用程序添加spring安全性
我试图在spring项目中从angular js配置登录视图。下面是我的spring安全xml设置Angularjs 正在为angular js应用程序添加spring安全性,angularjs,spring,spring-mvc,spring-security,Angularjs,Spring,Spring Mvc,Spring Security,我试图在spring项目中从angular js配置登录视图。下面是我的spring安全xml设置 <http auto-config="false" entry-point-ref="loginUrlAuthenticationEntryPoint"> <intercept-url pattern="/**" access="ROLE_ADMIN"/> <custom-filter position="FORM_LOGIN_FILTER
<http auto-config="false" entry-point-ref="loginUrlAuthenticationEntryPoint">
<intercept-url pattern="/**" access="ROLE_ADMIN"/>
<custom-filter position="FORM_LOGIN_FILTER" ref="theiaFormAuthenticationFilter"/>
<custom-filter ref="theiaRestDigestFilter" after="FORM_LOGIN_FILTER" />
<logout logout-success-url="/#/login" />
<session-management invalid-session-url="/#/login" />
<access-denied-handler error-page="/loginFailed"/>
</http>
它似乎无法识别/#/login。在这个应用程序中,我们没有索引页。它必须直接进入登录页面。是否仍要在配置文件中添加角度路由?我错过什么了吗
我现在正在学习本教程:您有本教程。。最好的教程,你将为此目的。它通过oauth2和拆分服务器一步一步地从简单的应用程序发展到最复杂的应用程序。非常推荐: 编辑: 因为你在使用angularjs,所以你不能做你正在做的事情。SpringSecurity将只保存资产,而不监听url。 这是一个单页应用程序,所以webflow上的所有控件都应该在客户端上 你应该使用这样的东西:
http
.httpBasic()
.and()
.authorizeRequests()
.antMatchers("/index.html", "/home.html", "/login.html").permitAll()
.anyRequest().authenticated();
angular.module('hello', [ 'ngRoute' ]) // ... omitted code
.controller('navigation',
function($rootScope, $scope, $http, $location) {
var authenticate = function(credentials, callback) {
var headers = credentials ? {authorization : "Basic "
+ btoa(credentials.username + ":" + credentials.password)
} : {};
$http.get('user', {headers : headers}).success(function(data) {
if (data.name) {
$rootScope.authenticated = true;
} else {
$rootScope.authenticated = false;
}
callback && callback();
}).error(function() {
$rootScope.authenticated = false;
callback && callback();
});
}
authenticate();
$scope.credentials = {};
$scope.login = function() {
authenticate($scope.credentials, function() {
if ($rootScope.authenticated) {
$location.path("/");
$scope.error = false;
} else {
$location.path("/login");
$scope.error = true;
}
});
};
});
所以,当客户端未经许可试图获取资源时,服务器不会向您提供资源。在客户机中,您需要执行路由逻辑
实际上是这样的:
http
.httpBasic()
.and()
.authorizeRequests()
.antMatchers("/index.html", "/home.html", "/login.html").permitAll()
.anyRequest().authenticated();
angular.module('hello', [ 'ngRoute' ]) // ... omitted code
.controller('navigation',
function($rootScope, $scope, $http, $location) {
var authenticate = function(credentials, callback) {
var headers = credentials ? {authorization : "Basic "
+ btoa(credentials.username + ":" + credentials.password)
} : {};
$http.get('user', {headers : headers}).success(function(data) {
if (data.name) {
$rootScope.authenticated = true;
} else {
$rootScope.authenticated = false;
}
callback && callback();
}).error(function() {
$rootScope.authenticated = false;
callback && callback();
});
}
authenticate();
$scope.credentials = {};
$scope.login = function() {
authenticate($scope.credentials, function() {
if ($rootScope.authenticated) {
$location.path("/");
$scope.error = false;
} else {
$location.path("/login");
$scope.error = true;
}
});
};
});
在服务器中,当您尝试发送get/user时,如果用户会话处于活动状态,服务器将返回会话用户。因此,在服务器中,您还需要:
@SpringBootApplication
@RestController
public class UiApplication {
@RequestMapping("/user")
public Principal user(Principal user) {
return user;
}
...
}
“theiaFormAuthenticationFilter”和“loginUrlAuthenticationEntryPoint”的定义是什么?您调用哪个url,并将重定向重定向到哪个url?(尝试排除受保护的登录URL,也考虑资源(JS、CSS、图像…))实际上我现在改变了代码来使用Spring安全配置类。它能够提供spring security附带的默认登录页面。但我无法添加angularjs路径进行登录。下面是安全代码:我已经看过这个教程。但是你能检查一下我的更新吗。角度的js路线似乎没有被识别!每当我尝试转到localhost/#/login查看我的编辑时,它都会转到404页。我拿走了重要的东西。。理解简单的事情。。因为angularjs不是真正不同的页面,而是一个页面,您不能说spring重定向到另一个页面。所有的逻辑路由逻辑都应该在客户端。您只能为将尝试访问受限区域的经过身份验证的用户保护服务器上的资源。我只是在AngularJS UI端更改了身份验证,就成功了。