java - Spring 安全和 Jersey : no automatic redirection to the login site

标签 java spring-security jersey

我在使用 spring-security 3.0.3 和 jersey 1.2 时遇到了以下问题:

当我使用@Secured 或@PreAuthorize 注释注释我的Jersey 资源时,spring-security 不会像我希望的那样将用户代理自动重定向到登录页面。只有 AuthenticationCredentialsNotFoundException 被抛出并且 HTTP 500 被返回给用户代理而不是重定向到 spring-security 的表单登录页面。 有谁知道为什么会出现这个问题?

资源:

@Path("/event")
@Component
public class EventResource extends AbstractBaseResource {

     @Resource(name = "eventService")
     private EventService eventService;

     public void setEventService(EventService eventService) {
         this.eventService = eventService;
     }

 @GET
 @Path("/view/{id}")
 @Produces(MediaType.TEXT_HTML)
 @PreAuthorize("hasRole('ROLE_USER')")
 public Viewable viewEvent(@PathParam("id") long id) throws UnsupportedEncodingException, URISyntaxException{
            ...
        }
}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_3_0.xsd"
    version="3.0">
    <filter>
        <filter-name>jersey</filter-name>
        <filter-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</filter-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
            <param-value>/(static|images|js|css|(WEB-INF/views))/.*</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
            <param-value>/WEB-INF/views/</param-value>
        </init-param>
    </filter>

    <filter>
  <filter-name>springSecurityFilterChain</filter-name>
  <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
 </filter>


    <filter-mapping>
        <filter-name>jersey</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter-mapping>
  <filter-name>springSecurityFilterChain</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

    [...]
</web-app>

上下文:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:security="http://www.springframework.org/schema/security" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">

    <context:annotation-config />
    <context:component-scan base-package="..." />
 <context:mbean-export/>

    <security:http auto-config="true" >
     <security:form-login/>
     <security:logout/>
    </security:http>

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

 <security:global-method-security pre-post-annotations="enabled" />

    [...]
</beans>

谢谢!

最佳答案

这是 spring-security 3.0.3 的问题。 切换到 spring-security 3.0.5 后一切正常。

关于java - Spring 安全和 Jersey : no automatic redirection to the login site,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4398810/

相关文章:

java - 选择正确的调用方法

java - 我可以在 Java 中使用反射重新定义方法/构造函数吗?

spring - 无状态前端grails服务器?

java - 在 Spring 项目中将 IP 列入白名单

Spring 启动 : How to specify the PasswordEncoder?

java - RestAssured - 将列表作为 QueryParam 传递

java - 如何用下划线替换斜线

java - 如何为一个 Java 类创建两个接口(interface),一个是只读的,一个是可读写的?

java - 我怎样才能完全覆盖 Jersey 使用的默认 ServiceLocator?

java - 如何从谷歌驱动器直接下载链接获取文件扩展名?