gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:59:27 26 4
gpt4 key购买 nike

我在使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com