我一直在尝试在客户端代码中实现基于角色的授权,以限制特定用户的 View 。
我正在尝试使用以下代码来限制 View
@PreAuthorize("hasRole('ROLE_USER')")
public void create(Contact contact);
我还尝试添加以下代码
Authentication a = SecurityContextHolder.getContext().getAuthentication();
UserDetails currentUserDetails = (UserDetails) a.getPrincipal();
然后在module.gwt.xml文件中添加以下内容
<inherits name='org.springframework.security.core.Authentication' />
<inherits name='org.springframework.security.core.context.SecurityContextHolder' />
<inherits name='org.springframework.security.core.userdetails.UserDetails' />
但是编译时出现以下错误
[ERROR] Unable to find 'org/springframework/security/core/Authentication.gwt.xml' on your classpath; could be a typo, or maybe you forgot to include a classpath entry for source?
[java][ERROR] Line 22: Unexpected exception while processing element 'inherits'
请告诉我哪里出错了,我该怎么做才能完成这项工作。
最佳答案
简短的回答是:这是不可能的。
在客户端,仅 a specific subset of Java is emulated ,并且您使用的所有库都必须专门用于与 GWT 配合使用(包括拥有自己的 .gwt.xml)。
当您尝试在客户端使用 Spring 类时,GWT 将不认识它们并抛出错误,如果您尝试导入模块,它会查找相应的 .gwt.xml 文件(这显然不会不存在,因为 Spring 项目不适用于 GWT)。
那么,你能做什么?
保护所有 RPC 调用服务器端:如果用户可以加载 View ,则将无法加载数据。
为未经身份验证的用户保护您的 .html 页面(您必须为登录创建不同的页面)。这将阻止未登录的用户访问应用程序。您也可以看看here一些其他的想法,包括 servlet/jsp。
每次用户请求 View 时都会进行 RPC 调用,以检查他是否经过身份验证/是否拥有权限。在客户端缓存此信息可能是一个好主意,以避免减慢应用程序的速度(不要忘记在用户注销时清理缓存)。
关于java - 如何在GWT客户端使用Spring授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22224291/