0

在我的项目中实现了spring security的实现,但是我在定义重定向时遇到了麻烦。当访问被锁定时,我需要将该用户重定向到特定的 URL。

当我放置标签“access-denied-handler”时,我希望他重定向到 bean 上定义的页面,但不要发生。

安全上下文.xml

<?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:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
                       http://www.springframework.org/schema/beans/spring-beans.xsd
                       http://www.springframework.org/schema/security
                       http://www.springframework.org/schema/security/spring-security-3.1.xsd">

<!-- Method Security -->
<security:global-method-security pre-post-annotations="enabled">
    <security:expression-handler ref="expressionHandler" />
</security:global-method-security>


<bean id="expressionHandler" class="org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler">
    <property name="permissionEvaluator" ref="permissionEvaluator"/>
</bean>

<bean id="permissionEvaluator" class="net.pontoall.hemisphere.security.HemispherePermissionEvaluator"/>

<!-- Publicos -->
<security:http pattern="/layouts/**" security="none" />
<security:http pattern="/messages/**" security="none" />
<security:http pattern="/test/**" security="none" />
<security:http pattern="/resources/**" security="none" />
<security:http pattern="/login/**" security="none" />
<security:http pattern="/install/**" security="none" />
<security:http pattern="/cobredireto/**" security="none" />
<security:http pattern="/hotsite/**" security="none" />
<security:http pattern="/captcha.jpg" security="none" />

<security:http auto-config="true" use-expressions="true">

    <security:access-denied-handler ref="HemisphereAccessDeniedHandler"/>

    <security:intercept-url pattern="/**" access="isAuthenticated()" />

    <security:form-login login-page="/login" default-target-url="/home" 
                         authentication-failure-url="/login?logout=true" 
                         authentication-success-handler-ref="authenticationSuccessHandler"
                         authentication-failure-handler-ref="authenticationFailureHandler"/>

    <security:logout logout-url="/j_spring_security_logout" invalidate-session="true" success-handler-ref="logoutHandler"/>
</security:http>

<!-- Authentication Manager -->
<security:authentication-manager alias="authenticationManager">
    <!-- Custom Authentication provider -->
    <security:authentication-provider ref="hemisphereAuthenticationProvider"/>
</security:authentication-manager>

<bean id="hemisphereAuthenticationProvider" class="net.pontoall.hemisphere.security.HemisphereAuthenticationProvider">
    <property name="userDetailsService" ref="userDetailService"/>
</bean>

<bean id="authenticationSuccessHandler" class="net.pontoall.hemisphere.security.HemisphereAuthenticationSuccessHandler">
    <property name="defaultTargetUrl" value="/home" />
    <property name="alwaysUseDefaultTargetUrl" value="no" />
</bean>

<bean id="authenticationFailureHandler" class="net.pontoall.hemisphere.security.HemisphereAuthenticationFailureHandler">
    <property name="defaultFailureUrl" value="/login" />
</bean>

<bean id="logoutHandler" class="net.pontoall.hemisphere.security.HemisphereLogoutHandler"/>

<bean id="HemisphereAccessDeniedHandler" class="net.pontoall.hemisphere.security.HemisphereAccessDeniedHandler">
    <property name="errorPage" value="/error/permissao"/>
</bean>

Bean Java - HemisphereAccessDeniedHandler.java:

public class HemisphereAccessDeniedHandler implements AccessDeniedHandler {

private String errorPage;

public String getErrorPage() {
    return errorPage;
}

public void setErrorPage(String errorPage) {

    if (errorPage == "") {
        errorPage = "/";
    }

    this.errorPage = errorPage;
}

@Override
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
    // Set the 403 status code.
    response.setStatus(HttpServletResponse.SC_FORBIDDEN);
    response.sendRedirect(errorPage);
}

}
4

2 回答 2

1

我发现发生了什么。我有一个 Spring MVC @ExceptionHandler,他正在捕获错误。

@ExceptionHandler
public ResponseEntity<String> handle(Exception exception, HttpServletRequest requestMain) {
    String erro = exception.getMessage();
    PusherRequest request = new PusherRequest("hemisphere-web", requestMain.getSession().getId());
    request.triggerPush(erro);

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);

    return new ResponseEntity<String>(exception.getMessage(), headers, HttpStatus.FORBIDDEN);
}
于 2012-10-31T17:32:10.733 回答
0

重定向有自己的 HTTP 状态码 (3xx),请参见http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html。该方法response.sendRedirect应自动将 HTTP 状态代码设置为 307(临时重定向,请参阅http://docs.oracle.com/javaee/5/api/javax/servlet/http/HttpServletResponse.html#sendRedirect)。

但是您在重定向之前将 HTTP 状态代码设置为 Forbidden (403)。

因此尝试删除response.setStatus(HttpServletResponse.SC_FORBIDDEN). 您还可以发布curl转储以查看原始服务器输出。

于 2012-10-31T17:03:06.647 回答