【问题标题】:Spring Security 4 JSF Managed bean preauthorize annotationSpring Security 4 JSF托管bean预授权注释
【发布时间】:2016-10-24 22:46:49
【问题描述】:

我希望有人可以帮助我将 @PreAuthorize 注释与托管 bean 集成。在过去的几个小时里,我一直试图让它工作,但显然我缺少一些东西。我在网络上的研究表明 global-method-security 元素可以与 spring 或 aspectj 一起使用。因为我不想将我的托管 bean 声明为 spring bean,所以我选择使用 aspectj。出于某种原因,尽管 PreAuthorize 注释被完全忽略。我没有收到任何错误,一切都编译并运行良好,但托管 bean 没有安全检查。显然aspectj需要某种编织???也许我正在接近这种错误的方式,并且有一种更简单的方法......不确定。将 Tomcat 7 与 JSF 2.2 和 Spring Security 4 一起使用。我在类上有注释,但这可能是我的问题。我假设把它放在类上会使用它的默认构造方法。

有人可以建议吗? (以下配置)

安全性.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>

Maven 构建

<plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.8</version>
            <configuration>
                <showWeaveInfo>true</showWeaveInfo>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>org.springframework.security</groupId>
                        <artifactId>spring-security-aspects</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjrt</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
            </dependencies>
        </plugin>

托管 Bean

@ManagedBean
@ViewScoped
@PreAuthorize("hasAnyRole('Admin')")
public class ProductManagement

【问题讨论】:

    标签: jsf spring-security authorization


    【解决方案1】:

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

    @PreAuthorize("hasRole('ROLE_USER')")
    public void create(Contact contact);
    

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

    您的问题似乎要限制的是对整个视图的访问,那么为什么不采用与您在 security.xml 声明中相同的方式呢?

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

    这样 Spring security 在其 Web Filter 中检查权限,效果更好。

    另见:

    【讨论】:

    • 我希望它在类级别声明时会阻止构造函数调用。我尝试了一种特定的方法,它仍然不起作用。没关系,因为我想我会像你说的那样以错误的方式解决这个问题。我将坚持使用开箱即用的拦截 URL,然后创建我自己的小安全实用程序类来对某些方法进行检查。我认为这会给我更精细的控制。这样我就可以根据角色将权限放入我的数据库中。与 Spring Security 的斗争越少越好。
    • @user2677597 我稍微改变了我的答案以更好地解释这个问题。正如我所说,没有机会在 JSF bean 中使用 spring 注释。您仍然可以将其转换为 Spring bean(参见 spring 文档,与其他框架集成的部分)。如果您采用这种方式,则必须实现自定义范围以模拟 JSF 视图范围,但您已经在 Internet 上完成了一些实现。
    • @user2677597 你解决了吗?你做了什么来获得托管 bean 方法的注释级别?
    猜你喜欢
    • 2016-11-12
    • 1970-01-01
    • 2013-01-23
    • 2014-07-01
    • 2018-05-24
    • 1970-01-01
    • 2012-02-22
    • 2012-01-27
    • 1970-01-01
    相关资源
    最近更新 更多