繁体   English   中英

spring-security&jersey:没有自动重定向到登录站点

[英]spring-security & jersey: no automatic redirection to the login site

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

当我使用@Secured或@PreAuthorize Annotations注释我的泽西资源时,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后,一切都按预期工作。

可能是由于登录页面本身被Spring安全过滤了吗? 一种检查方法是尝试添加类似的行

<intercept-url pattern="/login.jsp*" filters="none"/>

将以下文件管理器添加到web.xml

<filter>
        <filter-name>Acegi Filter Chain Proxy</filter-name>
        <filter-class>org.springframework.security.util.FilterToBeanProxy</filter-class>
        <init-param>

            <param-name>targetClass</param-name>
            <param-value>org.springframework.security.util.FilterChainProxy</param-value>
        </init-param>
    </filter>

更新上下文文件:

<security:intercept-url pattern="/login*" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
<security:form-login login-page="/login.htm" 
                             login-processing-url="/yourprocessingurl.do" 
                             default-target-url="/index.htm" 
                             authentication-failure-url="/login.htm" />
<security:logout logout-url="/logout" logout-success-url="/logout.jsp" />
<security:anonymous key="anonymous-security" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM