And*_*020 2 spring spring-mvc spring-security
我有一个使用 Java Spring MVC 构建的 Web 应用程序,并使用 Spring Security 进行登录。登录/注销效果很好。有两个用户角色“ROLE_ADMIN”和“ROLE_USER”。
我想确保我的方法userService.createUser(username)只能由具有“ROLE_ADMIN”角色的用户访问。
我添加了@PreAuthorize这样的注释...
public class UserServiceImpl implements UserService {
@PreAuthorize("hasRole('ROLE_ADMIN')")
public Integer createUser(String username) throws Exception {
/* .... */
}
/* ... */
}
Run Code Online (Sandbox Code Playgroud)
...但这并不能阻止仅具有“ROLE_USER”的登录用户创建用户。
我用谷歌搜索并阅读了注释@EnableGlobalMethodSecurity,但我不知道它去了哪里。
我是否用它注释我的 UserService 类?或者,WebController?或两者?
请帮忙,因为我真的需要一些建议!
解决方案,感谢@RG提供的链接......
我@EnableGlobalMethodSecurity(prePostEnabled=true)像这样添加到我的配置类中......
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class AppConfig {
/* ... */
}
Run Code Online (Sandbox Code Playgroud)
然后我@PreAuthorize像这样添加到界面中的方法......
public interface UserService {
@PreAuthorize("hasRole('ROLE_ADMIN')")
public Integer createUser(String username) throws Exception;
}
Run Code Online (Sandbox Code Playgroud)
作品!谢谢!
| 归档时间: |
|
| 查看次数: |
2873 次 |
| 最近记录: |