Как перенаправить на страницу отказа в доступе с пружинной безопасностью - PullRequest
2 голосов
/ 12 февраля 2012

У меня есть приложение с JSF + Spring.Я использую Spring Security и работаю правильно. Однако, когда я пытаюсь добраться до защищенной страницы без аутентификации, вместо перенаправления на запрещенную страницу я просто показываю 403 Запрещенную страницу.Я не знаю, отсутствует ли что-либо в applicationContext или web.xml, вот мой код:

часть applicationContext:

<sec:http access-denied-page="/denied.xhtml"  auto-config="true" use-expressions="false" >
    <sec:form-login login-page="/login.xhtml" default-target-url="/"   authentication-failure-url="/denied.xhtml"
    login-processing-url="/static/j_spring_security_check"
    />
    <sec:intercept-url pattern="/PANEL/**"  access="ROLE_GENERALT"></sec:intercept-url>
    <sec:logout invalidate-session="true" logout-url="/index.xhtml"/>
    </sec:http>

<sec:global-method-security secured-annotations="enabled" jsr250-annotations="enabled"></sec:global-method-security>

и web.xml:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/appContext.xml
        </param-value>
    </context-param>
    <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>

</filter>

<filter-mapping>
  <filter-name>springSecurityFilterChain</filter-name>
  <url-pattern>/*</url-pattern>
  <dispatcher>FORWARD</dispatcher>
  <dispatcher>REQUEST</dispatcher>
</filter-mapping>

1 Ответ

12 голосов
/ 13 февраля 2012

Вам необходимо установить свойство страницы ошибки для accessDeniedHandler, который используется ExceptionTranslationFilter, когда возникает AccessDeniedException

см. Информацию ссылка

<bean id="exceptionTranslationFilter" class="org.springframework.security.web.access.ExceptionTranslationFilter">
  <property name="authenticationEntryPoint" ref="authenticationEntryPoint"/>
  <property name="accessDeniedHandler" ref="accessDeniedHandler"/>
</bean>

<bean id="accessDeniedHandler" class="org.springframework.security.web.access.AccessDeniedHandlerImpl">
  <property name="errorPage" value="/denied.xhtml"/>
</bean>

в качестве альтернативы, вы можете просто добавить это в ваш web.xml

<error-page>
  <error-code>403</error-code>
  <location>/pages/denied.xhtml</location>
</error-page>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...