Spring 安全 4 JSF 托管 bean 预授权注释
Spring Security 4 JSF Managed bean preauthorize annotation
我希望有人可以帮助我将 @PreAuthorize 注释与托管 bean 集成。在过去的几个小时里,我一直在努力让它发挥作用,但显然我缺少了一些东西。我在网络上的研究表明,global-method-security 元素可以与 spring 或 aspectj 一起使用。因为我不想将我的托管 bean 声明为 spring beans,所以我选择使用 aspectj。出于某种原因,虽然 PreAuthorize 注释被完全忽略。我没有收到任何错误,一切都编译并运行良好,但没有对托管 bean 进行安全检查。显然 aspectj 需要某种编织???也许我正在接近这种错误的方式并且有一种更简单的方法..不确定。将 Tomcat 7 与 JSF 2.2 和 Spring Security 4 一起使用。我在 class 上有注释,但这可能是我的问题。我假设将它放在 class 上将使用其默认构造方法。
有人可以指点一下吗? (下面的配置)
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>
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 创建 bean 而不是 Spring 时,@PreAuthorize
在那里毫无意义。即使在使用 Spring 创建 bean 时,它的使用也是针对方法的(即使您可以注释 class 以处理其所有方法):
@PreAuthorize("hasRole('ROLE_USER')")
public void create(Contact contact);
此处Spring检查当前登录的用户是否具有用户角色,以便他能够创建联系人。
你的问题似乎是想限制对整个视图的访问,所以为什么不像你在 security.xml 声明中那样做呢?
<intercept-url pattern="/products/product_management.xhtml" access="hasRole('USER')" />
这样 Spring 安全性检查其 Web 过滤器中的权限,这更好。
另请参阅:
我希望有人可以帮助我将 @PreAuthorize 注释与托管 bean 集成。在过去的几个小时里,我一直在努力让它发挥作用,但显然我缺少了一些东西。我在网络上的研究表明,global-method-security 元素可以与 spring 或 aspectj 一起使用。因为我不想将我的托管 bean 声明为 spring beans,所以我选择使用 aspectj。出于某种原因,虽然 PreAuthorize 注释被完全忽略。我没有收到任何错误,一切都编译并运行良好,但没有对托管 bean 进行安全检查。显然 aspectj 需要某种编织???也许我正在接近这种错误的方式并且有一种更简单的方法..不确定。将 Tomcat 7 与 JSF 2.2 和 Spring Security 4 一起使用。我在 class 上有注释,但这可能是我的问题。我假设将它放在 class 上将使用其默认构造方法。
有人可以指点一下吗? (下面的配置)
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>
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 创建 bean 而不是 Spring 时,@PreAuthorize
在那里毫无意义。即使在使用 Spring 创建 bean 时,它的使用也是针对方法的(即使您可以注释 class 以处理其所有方法):
@PreAuthorize("hasRole('ROLE_USER')")
public void create(Contact contact);
此处Spring检查当前登录的用户是否具有用户角色,以便他能够创建联系人。
你的问题似乎是想限制对整个视图的访问,所以为什么不像你在 security.xml 声明中那样做呢?
<intercept-url pattern="/products/product_management.xhtml" access="hasRole('USER')" />
这样 Spring 安全性检查其 Web 过滤器中的权限,这更好。
另请参阅: