Java 如何使用Spring Security保护AngularJS部分页面
我正在开发一个Spring MVC web应用程序,我正在使用AngularJS,我想基于角色保护我的视图,例如,如果我以管理员角色登录,我可以查看删除用户部分页面,如果我以用户角色登录,我不能查看删除用户部分页面 我知道如何使用spring security实现这一点,但对于普通的web应用程序,而不是带有angularJS的restful单页应用程序,因为使用angularJS,您并不是真正从服务器请求视图,而是在ng视图标记中加载部分html页面 我还使用ngRoute来路由我的partials 这是我的ngRoute.jS文件Java 如何使用Spring Security保护AngularJS部分页面,java,angularjs,spring,spring-mvc,spring-security,Java,Angularjs,Spring,Spring Mvc,Spring Security,我正在开发一个Spring MVC web应用程序,我正在使用AngularJS,我想基于角色保护我的视图,例如,如果我以管理员角色登录,我可以查看删除用户部分页面,如果我以用户角色登录,我不能查看删除用户部分页面 我知道如何使用spring security实现这一点,但对于普通的web应用程序,而不是带有angularJS的restful单页应用程序,因为使用angularJS,您并不是真正从服务器请求视图,而是在ng视图标记中加载部分html页面 我还使用ngRoute来路由我的parti
angular.module('MyApp')
.config(['$routeProvider', 'USER_ROLES' ,function ($routeProvider, USER_ROLES) {
$routeProvider.
when('/', {
templateUrl: '/SpringMVCTemplateAnn/resources/angular/templates/dashboardTemplates/dashboardTemplate.html',
controller: 'DashBoardCtrl',
access: {
loginRequired: true,
authorizedRoles: [USER_ROLES.all]
}
}).
when('/login', {
templateUrl: '/SpringMVCTemplateAnn/resources/angular/templates/loginTemplate/login.html',
controller: 'LoginController',
access: {
loginRequired: false,
authorizedRoles: [USER_ROLES.all]
}
}).
when('/createUser', {
templateUrl: '/SpringMVCTemplateAnn/views/partials/userTemplates/createUser.html',
controller: 'UserCtrl'
}).
when('/deleteUser', {
templateUrl: '/SpringMVCTemplateAnn/views/partials/userTemplates/deleteUser.html',
controller: 'UserCtrl'
}).
}]);
这是我的spring安全配置,我正在使用java注释配置,我已经调整了配置以将其转换为适合rest应用程序的配置
这是我扩展WebSecurityConfigureAdapter的安全类
public class SegurityConfig extends WebSecurityConfigurerAdapter {
public SegurityConfig() {
super();
}
@Autowired
private UserDetailsService userDetailsService;
@Autowired
private RestUnauthorizedEntryPoint restAuthenticationEntryPoint;
@Autowired
private AccessDeniedHandler restAccessDeniedHandler;
@Autowired
private AuthenticationSuccessHandler restAuthenticationSuccessHandler;
@Autowired
private AuthenticationFailureHandler restAuthenticationFailureHandler;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService);
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/resources/**", "/index.jsp", "/login.jsp",
"/template/**", "/", "/error/**");
}
//
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.headers().disable()
.csrf().disable()
.authorizeRequests()
.antMatchers("/failure").permitAll()
.antMatchers("/v2/api-docs").hasAnyAuthority("admin")
.antMatchers("/users/**").hasAnyAuthority("admin")
.antMatchers("/views/partials/userTemplates/createUser.html").access("hasRole('create users')")
.anyRequest().authenticated()
.and()
.exceptionHandling()
.authenticationEntryPoint(restAuthenticationEntryPoint)
.accessDeniedHandler(restAccessDeniedHandler)
.and()
.formLogin()
.loginProcessingUrl("/login")
.successHandler(restAuthenticationSuccessHandler)
.failureHandler(restAuthenticationFailureHandler)
.usernameParameter("username")
.passwordParameter("password")
.permitAll()
.and()
.logout()
.logoutUrl("/logout")
.logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler())
.deleteCookies("JSESSIONID")
.permitAll()
.and();
}
}
我也在为angularjs使用http身份验证拦截器插件
这是MyApp模块
angular.module('MyApp', ['ngRoute', 'ui.bootstrap', 'smart-table', 'http-auth-interceptor']);
这是我的js文件,它阻止用户在未经身份验证的情况下浏览网站
angular.module('MyApp')
.run(function ($rootScope, $location, $http, AuthSharedService, Session,
USER_ROLES, $q, $timeout) {
$rootScope.$on('$routeChangeStart', function (event, next) {
if (next.originalPath === "/login" && $rootScope.authenticated) {
event.preventDefault();
console.log('registrese');
} else if (next.access && next.access.loginRequired && !$rootScope.authenticated) {
event.preventDefault();
$rootScope.$broadcast("event:auth-loginRequired", {});
} else if (next.access && !AuthSharedService.isAuthorized(next.access.authorizedRoles)) {
event.preventDefault();
$rootScope.$broadcast("event:auth-forbidden", {});
}
});
// Call when the the client is confirmed
$rootScope.$on('event:auth-loginConfirmed', function (event, data) {
console.log('login confirmed start ' + data);
$rootScope.loadingAccount = false;
var nextLocation = ($rootScope.requestedUrl ? $rootScope.requestedUrl : "/home");
var delay = ($location.path() === "/loading" ? 1500 : 0);
$timeout(function () {
Session.create(data);
$rootScope.account = Session;
$rootScope.authenticated = true;
$location.path(nextLocation).replace();
}, delay);
});
// Call when the 401 response is returned by the server
$rootScope.$on('event:auth-loginRequired', function (event, data) {
if ($rootScope.loadingAccount && data.status !== 401) {
$rootScope.requestedUrl = $location.path()
$location.path('/loading');
} else {
Session.invalidate();
$rootScope.authenticated = false;
$rootScope.loadingAccount = false;
$location.path('/login');
}
});
// Call when the 403 response is returned by the server
$rootScope.$on('event:auth-forbidden', function (rejection) {
$rootScope.$evalAsync(function () {
$location.path('/error/403').replace();
});
});
});
这是我的用户角色常量js文件
angular.module('MyApp')
.constant('USER_ROLES', {
all: '*',
admin: 'admin',
user: 'user'
});
如果我使用具有正常用户角色的用户登录,我想保护我的部分页面,我不想看到删除或创建用户部分页面
我尝试将我的部分内容移出resources文件夹,并将其放入我的secuirty类.antMatchers(“/views/partials/userTemplates/createUser.html”).access(“hasRole('create users')”)
,但如果它阻止了部分内容,即使我使用具有该角色的用户登录,我仍然会收到403错误,我希望发生这种情况,但如果我与一个没有该角色的用户登录,但当我与一个有该角色的用户登录时,为什么会发生这种情况,就像它不认识到我有该角色一样
有没有一种方法可以基于角色保护这些部分,比如在普通web应用中,因为该配置在我工作的普通web应用中工作,但在单个页面rest应用中似乎不工作
如果可以从服务器端保护这些部分,我知道我可以使用@PreAuthorize(“hasRole('create users')”)
在服务器端保护我的Rest方法,但是有没有一种方法可以像.antMatchers(“/my partials/**”)那样放置
或安全配置类中的类似内容来保护分区
我的文件夹结构
SpringMVCProject
--Web-Pages
--WEB-INF
--resources
--angular
--controllers
userController.js
--services
userService.js
--partials
--userPartials
deleteuser.html
app.js
permissionConstants.js
routes.js
--css
--views
--partials (I experimented putting this the folder but I 403 error)
--userpartials
deleteuser.html
index.html
--Source Packages
--controllers
--userControllers
UserController.java
--secuirty
SecuirtyConfig.java