SpringSecurity4JSF托管bean预授权注释

SpringSecurity4JSF托管bean预授权注释,jsf,spring-security,authorization,Jsf,Spring Security,Authorization,我希望有人能帮助我将@PreAuthorize注释与托管bean集成在一起。在过去的几个小时里,我一直在努力让它发挥作用,但显然我缺少了一些东西。我在web上的研究表明,全局方法安全元素可以与spring或aspectj一起使用。因为我不想将我的托管bean声明为Springbean,所以我选择使用aspectj。由于某种原因,尽管预授权注释被完全忽略。我没有收到任何错误,所有的编译和运行都很好,但是没有对托管bean进行安全检查。显然aspectj需要某种编织???也许我走错了路,还有一条更容

我希望有人能帮助我将@PreAuthorize注释与托管bean集成在一起。在过去的几个小时里,我一直在努力让它发挥作用,但显然我缺少了一些东西。我在web上的研究表明,全局方法安全元素可以与spring或aspectj一起使用。因为我不想将我的托管bean声明为Springbean,所以我选择使用aspectj。由于某种原因,尽管预授权注释被完全忽略。我没有收到任何错误,所有的编译和运行都很好,但是没有对托管bean进行安全检查。显然aspectj需要某种编织???也许我走错了路,还有一条更容易的路……不确定。将Tomcat7与JSF2.2和SpringSecurity4结合使用。我在类上有注释,但这可能是我的问题。我假设将它放在类上将使用其默认构造函数方法

有人能给点建议吗?(配置如下)

security.xml

<b:beans xmlns="http://www.springframework.org/schema/security"
     xmlns:b="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">

<http use-expressions="true">
    <headers>
        <frame-options policy="SAMEORIGIN" />
    </headers>
    <intercept-url pattern="/admin/**" access="hasRole('Admin')" />
    <intercept-url pattern="/cms/**" access="hasAnyRole('Admin','CMS_Admin')" />
    <form-login login-page='/login' default-target-url="/" />
    <logout invalidate-session="true" logout-success-url="/" />
    <csrf disabled="true"/>
</http>

<b:bean name="bcryptEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>
<b:bean name="iberisUserDetailsService" class="com.bizznetworxonline.iberis.core.web.controllers.admin.security.IberisUserDetailsService"/>

<authentication-manager>
    <authentication-provider user-service-ref='iberisUserDetailsService'>
        <password-encoder ref="bcryptEncoder"/>
    </authentication-provider>
</authentication-manager>

<global-method-security mode="aspectj" pre-post-annotations="enabled" proxy-target-class="true">
</global-method-security>

</b:beans>

当您指示JSF创建bean而不是Spring时,
@PreAuthorize
在这里没有什么意义。即使在使用Spring创建bean时,它的使用也是针对方法的(即使您可以注释一个类以处理其所有方法):

这里Spring检查当前登录的用户是否具有用户角色,以便他能够创建联系人

在您的问题中,似乎要限制对整个视图的访问,那么为什么不按照security.xml声明中的方式进行呢

<intercept-url pattern="/products/product_management.xhtml" access="hasRole('USER')" />

通过这种方式,Spring security在其Web筛选器中检查权限,这甚至更好

另见:


我希望它在类级别声明时会阻止构造函数调用。我用一种特殊的方法试过,但仍然不起作用。不过没关系,因为我想我会像你说的那样走错方向。我将坚持使用开箱即用的截取url,然后创建自己的小安全util类来对某些方法执行检查。我想这会给我更精细的控制。这样,我可以根据角色在数据库中设置权限。与spring security的冲突越少越好。@user2677597为了更好地解释这个问题,我稍微修改了我的答案。正如我所说,在JSFbean中不可能使用spring注释。您仍然可以在Springbean中使用它(参见SpringDocs,它是与其他框架集成的一部分)。如果这样做,您必须实现自定义范围以模拟JSF视图范围,但您已经在Internet上完成了一些实现。@user2677597您解决了吗?您是如何在托管bean方法中获得注释级别的?
@ManagedBean
@ViewScoped
@PreAuthorize("hasAnyRole('Admin')")
public class ProductManagement
@PreAuthorize("hasRole('ROLE_USER')")
public void create(Contact contact);
<intercept-url pattern="/products/product_management.xhtml" access="hasRole('USER')" />