Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Spring Security的同一应用程序中的两个领域?_Java_Spring_Security_Spring Security - Fatal编程技术网

Java 使用Spring Security的同一应用程序中的两个领域?

Java 使用Spring Security的同一应用程序中的两个领域?,java,spring,security,spring-security,Java,Spring,Security,Spring Security,我们正在构建一个web应用程序,可供经过身份验证的用户和匿名用户使用。如果您决定不注册/登录,您只有一组有限的功能。用户身份验证通过具有Spring安全性的OpenID完成。那很好 但是,该应用程序还附带了一个部署在//admin的管理UI。我们是否可以使用Spring安全性拥有两个独立的领域(例如,/admin/**)的基本身份验证?这需要如何配置?我想不出一个简单的方法来拥有两个领域(我自己也没有尝试过): 您可以定义两个过滤器,其中每个过滤器都具有不同的spring配置,并具有自己的环境。

我们正在构建一个web应用程序,可供经过身份验证的用户和匿名用户使用。如果您决定不注册/登录,您只有一组有限的功能。用户身份验证通过具有Spring安全性的OpenID完成。那很好


但是,该应用程序还附带了一个部署在
//admin
的管理UI。我们是否可以使用Spring安全性拥有两个独立的领域(例如,
/admin/**
)的基本身份验证?这需要如何配置?

我想不出一个简单的方法来拥有两个领域(我自己也没有尝试过):

您可以定义两个过滤器,其中每个过滤器都具有不同的spring配置,并具有自己的环境。全局内容进入应用程序配置,即过滤器配置中特定的领域

如果只是针对不同的身份验证方法,您可以选择由哪个身份验证方法决定调用哪个筛选器。

可能的解决方案:

  • /admin
    添加URL拦截器需要“角色\管理员”
  • 配置
    org.springframework.security.web.authentication.www.BasicAuthenticationFilter
    的实例以拦截
    /admin
    URL,并在提供适当凭据的情况下将用户验证为角色\管理员
示例配置:

<security:intercept-url pattern="/admin" access="ROLE_ADMIN"/>

<bean id="basicAuthenticationEntryPoint" 
      class="org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint">
    <property name="realmName" 
              value="WS realm"/>
</bean>

<bean id="basicAuthenticationProcessingFilter"
      class="org.springframework.security.web.authentication.www.BasicAuthenticationFilter">
    <property name="authenticationManager" 
              ref="authenticationManager"/>
    <property name="authenticationEntryPoint" 
              ref="basicAuthenticationEntryPoint"/>    
</bean>

此外,您需要调整过滤器,使其仅应用于
/admin
URL-通过在
doFilter
方法中对其进行硬编码,或者通过提供适当的包装bean。

Spring Security在3.1版中添加了对该场景的支持,该版本目前作为候选版本提供。它是由实现的,语法的详细信息见3.1附带的手册

然而,它的使用非常简单。基本上,您只需在Spring安全配置中定义多个
http
元素,每个领域一个。我们是这样使用它的:

<!-- Configure realm for system administration users -->
<security:http pattern="/admin/**" create-session="stateless">
    <security:intercept-url pattern='/**' access='ROLE_ADMIN' requires-channel="https" />
    <security:http-basic/>  
</security:http>


<!-- Configure realm for standard users -->
<security:http auto-config="true" access-denied-page="/error/noaccess" use-expressions="true" create-session="ifRequired">
    <security:form-login login-page="/login"
            ...
            ...
</security:http>


我一直在挖掘,试图弄清楚如何做到这一点,谢谢你这么清楚地指出了这一点!Spring security 3.1.0已于2011年12月7日正式发布,我正是这样做的,如果我登录到管理部分,我也会登录到用户部分,但未经授权(当然)。我认为这是因为SpringSecurity在会话中使用一个密钥来持有授权。有没有一种方法可以将spring安全性配置为您可以使用不同的授权实体同时登录到admin和user部分的场景?看起来access属性的语法是错误的,应该是:access=“hasRole('ROLE_name')”
<!-- Configure realm for system administration users -->
<security:http pattern="/admin/**" create-session="stateless">
    <security:intercept-url pattern='/**' access='ROLE_ADMIN' requires-channel="https" />
    <security:http-basic/>  
</security:http>


<!-- Configure realm for standard users -->
<security:http auto-config="true" access-denied-page="/error/noaccess" use-expressions="true" create-session="ifRequired">
    <security:form-login login-page="/login"
            ...
            ...
</security:http>