【发布时间】:2014-05-16 17:10:24
【问题描述】:
我正在使用 Spring MVC + Spring Security 开发一个 Web 应用程序,并且我有以下 URL:
/*this URL should be accessible by any User, i.e. users should be able to see other users' profiles*/
/users/someUserId/profile
/* all the following URLs should be accessed only by the current authenticated user */
/users/someUserId/profile/edit
/users/someUserId/picture/edit
/users/someUserId/notifications
/users/someUserId/friends
而且我需要按照前面的描述保护它们。
我的配置方法如下:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/resources/**").permitAll()
.regexMatchers("/users/[\\w]+/profile").authenticated()
.antMatchers("/users/[\\w]/**").access("principal.name == regexGroupMatch")
.anyRequest().authenticated()
.and()
.jee().mappableRoles("Admin", "User");
}
我想知道是否有可能实现这样的目标:
.antMatchers("/heroes/[\\w]/**").access("principal.name == regexGroupMatch")
通过这样做,我只希望用户 user1 能够访问这些 URL:
/users/user1/profile/edit
/users/user1/picture/edit
/users/user1/notifications
因此,user2 一定不能访问前面提到的 URL,但必须能够访问: /users/user1/profile/编辑 /users/user1/图片/编辑 /users/user1/notifications
还有:
/users/user1/profile
/users/user2/profile
/users/user3/profile
etc...
是否可以使用 Spring Security 来实现?
【问题讨论】:
-
你读过文档吗?
-
是的@chrylis,我已阅读文档here。我相信我可以使用
.antMatcher(url)来实现这一点,但我不知道我应该如何使用哪种方法才能只允许当前用户看到他自己的个人资料。 -
顺便说一句,你读过this topic吗?
标签: java spring url spring-mvc spring-security