Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.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 针对web服务和用户的Spring安全性_Java_Spring_Security_Spring Mvc_Spring Security - Fatal编程技术网

Java 针对web服务和用户的Spring安全性

Java 针对web服务和用户的Spring安全性,java,spring,security,spring-mvc,spring-security,Java,Spring,Security,Spring Mvc,Spring Security,我们有一个web应用程序,我们希望使用spring security以两种不同的方式保护它: 1) 使用登录表单进行身份验证并有权访问某些服务的用户 2) 使用摘要身份验证(用户+密码在请求头中传递)保护的其他服务-由其他Web应用程序使用,因此没有登录表单 这些都是独立工作的,但我们无法让它们在同一个web应用程序中工作。 当我们尝试使用两个XML运行webapp时,会出现以下错误: A universal match pattern ('/**') is defined before ot

我们有一个web应用程序,我们希望使用spring security以两种不同的方式保护它:

1) 使用登录表单进行身份验证并有权访问某些服务的用户

2) 使用摘要身份验证(用户+密码在请求头中传递)保护的其他服务-由其他Web应用程序使用,因此没有登录表单

这些都是独立工作的,但我们无法让它们在同一个web应用程序中工作。 当我们尝试使用两个XML运行webapp时,会出现以下错误:

A universal match pattern ('/**') is defined  before other patterns in the filter chain, causing them to be ignored. Please check the ordering in your <security:http> namespace or FilterChainProxy bean configuration
在筛选器链中的其他模式之前定义了通用匹配模式('/**'),导致忽略它们。请检查命名空间或FilterChainProxy bean配置中的顺序
用于用户的security.xml文件:

<security:http use-expressions="true">
    <security:intercept-url pattern="/user/login"
        access="permitAll" />
    ...
    <security:intercept-url pattern="/**"
        access="isAuthenticated()" />

    <security:form-login
        authentication-success-handler-ref="userAuthenticationSuccessHandler" />

    <security:logout logout-url="/user/logout"
        logout-success-url="/demo/user/logoutSuccess" />
</security:http>

<bean id="bCryptPasswordEncoder"
    class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" />

<security:authentication-manager alias="authenticationManager">
    <security:authentication-provider
        ref="authenticationProvider">
    </security:authentication-provider>
</security:authentication-manager>

...
web服务的rest-security.xml文件:

<security:http create-session="stateless"
    entry-point-ref="digestEntryPoint">
    <security:intercept-url pattern="/provider/**"
        access="ROLE_WEBAPP" />

    <security:http-basic />
    <security:custom-filter ref="digestFilter"
        after="BASIC_AUTH_FILTER" />
</security:http>

<bean id="digestFilter"
    class="org.springframework.security.web.authentication.www.DigestAuthenticationFilter">
    <property name="userDetailsService" ref="webappDetailsServiceImpl" />
    <property name="authenticationEntryPoint" ref="digestEntryPoint" />
</bean>

<bean id="digestEntryPoint"
    class="org.springframework.security.web.authentication.www.DigestAuthenticationEntryPoint">
    <property name="realmName" value="Contacts Realm via Digest Authentication" />
    <property name="key" value="acegi" />
</bean>

<security:authentication-manager>
    <security:authentication-provider
        ref="restAuthenticationProvider">
    </security:authentication-provider>
</security:authentication-manager>


有没有人有过这样的经历?

< P>这可能不是你想要的,但是我会考虑把两个API分开。一个用于人,一个用于web服务

我们的人机界面就在根上下文之外:

http(s)://your.website.com/

然后,我们的web服务接口脱离api上下文:

http(s)://your.website.com/api/v1/

这使得使用spring处理两种不同类型的安全性变得很容易。

错误消息

通用匹配模式在过滤器链中的其他模式之前定义,导致忽略它们

它非常简洁

您应该检查web.xml中contextConfigLocation上下文参数的bean定义文件的顺序

<context-param>  
    <param-name>contextConfigLocation</param-name>  
    <param-value>security.xml,rest-security.xml</param-value>  
</context-param>

上下文配置位置
xml,rest-security.xml
应重现上述错误消息

<context-param>  
    <param-name>contextConfigLocation</param-name>  
    <param-value>rest-security.xml,security.xml</param-value>  
</context-param>

上下文配置位置
rest security.xml,security.xml

应该可以解决这个问题。

我在这里找到了解决方案:

这篇文章详细说明了我想做什么

技巧似乎是向rest http元素添加
pattern=“/provider/**”
。因此,正确的rest安全配置是:

<security:http create-session="stateless"
    entry-point-ref="digestEntryPoint" pattern="/provider/**"
    use-expressions="true">
    <security:intercept-url pattern="/provider/**"
        access="isAuthenticated()" />

    <security:http-basic />
    <security:custom-filter ref="digestFilter"
        after="BASIC_AUTH_FILTER" />
</security:http>


这正是我们正在尝试做的……但它不适用于当前的spring安全配置,我也找不到一个连贯的例子来说明如何做到这一点。您有关于这个主题的文档/教程吗?这两个安全XML不是在contextConfigLocation中定义的,只是在应用程序上下文(导入其他XML)中定义的。我尝试了您的解决方案,但仍然收到相同的错误消息。然而,这似乎是问题的根本原因。您能否将整个安全上下文保存在一个文件中,或者甚至将所有security:intercept url元素放在一个security:http元素中?