匿名用户无权访问Portlet

来源:互联网 发布:烟花算法 matlab 编辑:程序博客网 时间:2024/04/28 10:00
 转自:廖均勇的共享空间
很多朋友,包括我自己,都碰到过这个问题,匿名用户无权访问Portlet。问题主要出现在自己新建的public社区里面,Liferay缺省的社区/web/guest没有这个问题。
18.1 问题现象
按照以下步骤,就可以重现这个问题:
1、 新建一个public社区,并新建一个页面。如/web/cms/home
2、 在里面添加portlet。为了测试,可以添加以下两个:Hello World和 Sample Struts Portlet.其中,Hello World是Liferay自带的portlet,Sample Struts Portlet是一个用WAR包部署的portet。
3、 登录用户访问/web/cms/home,不会有问题,Portlet都能正常显示。
4、 匿名用户访问/web/cms/home,就会出现问题。Portlet的内容部分,提示"You do not have the roles required to access this portlet."。
5、 如果添加Liferay自带的部分其他portlet,如Message Board,就不会出现这个问题。
曾经试图分析相关代码,太过复杂,没有继续。等后续有时间的时候再说。根据我的初步分析,这是因为没有为portlet定义缺省权限导致的。
18.2 解决方法
这里只谈如何解决这个问题。解决这个问题,分三种情况:Liferay自带、Ext扩展和独立WAR。三种情况,有三种不同的解决方案。
三种方案类似,都是从resource-actions这个角度去解决问题。以Liferay4.2.2为例,其他版本应该没有太大区别。
18.2.1 Liferay自带
这种情况下,因为相关配置文件都在portal-ejb.jar文件中,因此我们需要对这个文件进行修改。以 Hello World这个Portlet为例。
先在本地某目录(如c:/temp)中创建一个文件,others.xml,其内容为:
<?xml version="1.0"?>
<resource-action-mapping>
 <portlet-resource>
  <portlet-name>47</portlet-name>
  <supports>
   <action-key>CONFIGURATION</action-key>
   <action-key>VIEW</action-key>
  </supports>
  <community-defaults>
   <action-key>VIEW</action-key>
  </community-defaults>
  <guest-defaults>
   <action-key>VIEW</action-key>
  </guest-defaults>
  <guest-unsupported>
   <action-key>CONFIGURATION</action-key>
  </guest-unsupported>
 </portlet-resource>
</resource-action-mapping>
然后从portal-ejb.jar/resource-actions 中将文件default.xml解压到c:/temp目录,并在最后添加一行  <resource file="resource-actions/others.xml" />,整个文件内容变成
<?xml version="1.0"?>
<resource-action-mapping>
 <resource file="resource-actions/portal.xml" />
 <resource file="resource-actions/blogs.xml" />
 <resource file="resource-actions/bookmarks.xml" />
 <resource file="resource-actions/calendar.xml" />
 <resource file="resource-actions/cms.xml" />
 <resource file="resource-actions/communities.xml" />
 <resource file="resource-actions/documentlibrary.xml" />
 <resource file="resource-actions/imagegallery.xml" />
 <resource file="resource-actions/journal.xml" />
 <resource file="resource-actions/messageboards.xml" />
 <resource file="resource-actions/polls.xml" />
 <resource file="resource-actions/shopping.xml" />
 <resource file="resource-actions/wiki.xml" />
 <resource file="resource-actions/workflow.xml" />
 <resource file="resource-actions/others.xml" />
</resource-action-mapping>
这两个文件修改好之后,重新放到portal-ejb.jar包中的resource-actions目录下。
18.2.2 EXT扩展
Ext开发环境中,主要是对我们自己开发的Portlet需要进行类似的定义。具体方法如下,以一个叫MyPortlet的Portlet为例。
1、 修改文件 /ext/ext-ejb/classes/portal-ext.properties,添加一行
resource.actions.configs=resource-actions/default.xml,resource-actions/default-ext.xml
2、 创建目录/ext/ext-ejb/classes/resource-actions
3、 创建文件/ext/ext-ejb/classes/resource-actions/default-ext.xml,其内容为
<?xml version="1.0"?>
<resource-action-mapping>
 <resource file="resource-actions/myportlet.xml" />
</resource-action-mapping>
4、 创建文件/ext/ext-ejb/classes/resource-actions/ myportlet.xml,其内容为
<?xml version="1.0"?>
<resource-action-mapping>
 <portlet-resource>
  <portlet-name>MyPortlet</portlet-name>
  <supports>
   <action-key>CONFIGURATION</action-key>
   <action-key>VIEW</action-key>
  </supports>
  <community-defaults>
   <action-key>VIEW</action-key>
  </community-defaults>
  <guest-defaults>
   <action-key>VIEW</action-key>
  </guest-defaults>
  <guest-unsupported>
   <action-key>CONFIGURATION</action-key>
  </guest-unsupported>
 </portlet-resource>
</resource-action-mapping>
5、 然后重新部署ext环境即可。Ant deploy
18.2.3 独立WAR
独立War的情况比较复杂一点,但也基本类似。我们用Sample Struts Portlet为例。按照以下步骤进行:
1、 修改web.xml,添加下列内容
 <context-param>
  <param-name>portlet_properties</param-name>
  <param-value>portal-sample.properties</param-value>
 </context-param>
注意其正确的位置,参照company_id。
2、 创建文件sample-struts-portlet-4.2.2/WEB-INF/classes/portal-sample.properties,其内容为
resource.actions.configs=resource-actions/default-sample.xml
3、 创建sample-struts-portlet-4.2.2/WEB-INF/classes/ resource-actions/default-sample.xml文件,其内容为
<?xml version="1.0"?>
<resource-action-mapping>
 <resource file="resource-actions/sample.xml" />
</resource-action-mapping>
4、 创建文件sample-struts-portlet-4.2.2/WEB-INF/classes/ resource-actions/sample.xml,其内容为
<?xml version="1.0"?>
<resource-action-mapping>
 <portlet-resource>
  <portlet-name>sample_struts_portlet</portlet-name>
  <supports>
   <action-key>CONFIGURATION</action-key>
   <action-key>VIEW</action-key>
  </supports>
  <community-defaults>
   <action-key>VIEW</action-key>
  </community-defaults>
  <guest-defaults>
   <action-key>VIEW</action-key>
  </guest-defaults>
  <guest-unsupported>
   <action-key>CONFIGURATION</action-key>
  </guest-unsupported>
 </portlet-resource>
</resource-action-mapping>
18.3 注意事项
至此,三种情况下,portlet无权访问的问题应该都可以解决。这三种情况,我都已经测试通过,都是可行的。
不过需要注意的一点是:需要把原来页面中的Portlet删掉,重新添加一次。这是因为重新添加的时候,liferay来读取相应的权限文件,为新添加的这个portlet进行授权。
原创粉丝点击