java - 使用 Spring Security XML 配置的 HTTP Basic 不使用 HttpBasicConfigurer

标签 java spring-security

关于 HTTP 基本配置,XML 配置和 Java 似乎在 Spring Security 中执行的任务不同。

当使用以下 Java 配置时:

protected void configure(HttpSecurity http) throws Exception {
  http
    .httpBasic()
  .and()
    .authorizeRequests()
      .anyRequest().authenticated();
}

当请求 HTTP header X-Requested-WithXMLHttpRequest 时,使用 HttpBasicConfigurer 来使用不同的 EntryPoint。

使用配置时

<s:http use-expressions="true" create-session="ifRequired">
     <s:intercept-url pattern='/**' access='isAuthenticated()' />
     <s:http-basic />
<s:http />

未使用HTTPBassicConfigurer

有人知道如何使用 XML 配置添加它吗?

最佳答案

根据本文中的人员提供的评论,最终的解决方案是不可能将 HTTPBasicConfigurer 与 XML 配置一起使用。但是还有其他方法可以执行与 HTTPBasicConfigurer 中现在实现的几乎相同的操作。我最终使用的解决方案主要是基于Lea提供的备注:

<s:http use-expressions="true" create-session="ifRequired" >
    <s:intercept-url pattern='/**' access='isAuthenticated()' />
    <s:http-basic entry-point-ref="entryPoint" /> 
</s:http>

<bean id="entryPoint"
      class="org.springframework.security.web.authentication
                                .DelegatingAuthenticationEntryPoint">
    <constructor-arg>
        <map>
            <entry key="hasHeader('X-Requested-With','XMLHttpRequest')" 
                   value-ref="ajaxEntyPoint" />
        </map>
    </constructor-arg>
    <property name="defaultEntryPoint" ref="defaultEntryPoint"/>        
</bean>

<bean id="ajaxEntyPoint" 
      class="org.springframework.security.web.authentication.HttpStatusEntryPoint">
    <constructor-arg name="httpStatus" 
                     value="#{T(org.springframework.http.HttpStatus).UNAUTHORIZED}"/>
</bean>

<bean id="defaultEntryPoint"
      class="org.springframework.security.web.authentication.www
                                             .BasicAuthenticationEntryPoint">
    <property name="realmName" value="My webservices"/>
</bean>

关于java - 使用 Spring Security XML 配置的 HTTP Basic 不使用 HttpBasicConfigurer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30304680/

相关文章:

java - 避免 java.net.SocketException : Unexpected end of file from server? 的最佳 sleep 时间是多少

java - @Secured 和@PreAuthorize 注解在哪些方法中起作用?

testing - CAS 安全 Web 服务的集成测试

java - 错误 HTTP 状态 403 - 在请求参数 'null' 或 header '_csrf' 上发现无效的 CSRF token 'X-CSRF-TOKEN'

java - spring boot 是否提供标准的登录和注册支持

java - 有关使用@PreAuthorize 时抛出 Access Denied 异常的方法的信息

java - 线程连接(毫秒)不会在 Java 中杀死线程

java - JDialog modal=true 与 ModalityType.APPLICATION_MODAL

java - Spring上下文引用java

java - JVM 在 Java 对象相等性 (==) 中检查什么?