jsf spring安全访问拒绝身份验证登录

jsf spring安全访问拒绝身份验证登录,jsf,authentication,login,spring-security,access-denied,Jsf,Authentication,Login,Spring Security,Access Denied,我在jsf2 web应用程序中使用SpringSecurity3 当未经身份验证的用户尝试访问受保护的资源时,Spring Security会重定向到登录页面 在这种情况下,如何在登录页面中向用户显示一条消息,说明不允许他/她访问资源 当Spring Security在ExceptionTranslationFilter中抛出AccessDeniedException,然后重定向到登录页面时,我尝试在登录页面中使用preRenderView侦听器,该侦听器检查“WebAttributes.ACC

我在jsf2 web应用程序中使用SpringSecurity3

当未经身份验证的用户尝试访问受保护的资源时,Spring Security会重定向到登录页面

在这种情况下,如何在登录页面中向用户显示一条消息,说明不允许他/她访问资源


当Spring Security在ExceptionTranslationFilter中抛出AccessDeniedException,然后重定向到登录页面时,我尝试在登录页面中使用preRenderView侦听器,该侦听器检查“WebAttributes.ACCESS\u DENIED\u 403”,但它为空。

看起来像这里提到的重复问题

如上所述,创建一个实例AccessDeniedHandlerImpl并将其属性设置为自定义错误页。从该页面使用简单的javascript转发到实际登录页面,如

 <script language="javascript" type="text/javascript">
 window.setTimeout('window.location="http://localhost:8080/WebApp/login.xhtml"; ',5000);
 </script>

window.setTimeout('window.location='http://localhost:8080/WebApp/login.xhtml"; ',5000);

或者只提供登录页面的按钮或链接。

将其添加到web.xml

    <error-page>
       <error-code>403</error-code>
       <location>/pages/errorAccessDenied.xhtml</location>
    </error-page>

403
/pages/errorAccessDenied.xhtml
然后用消息创建一个errorAccessDenied.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml"
       xmlns:h="http://java.sun.com/jsf/html">

<h:head>
</h:head>
<h:body>
     <h:outputText value="You do not have access to the requested resource" />
</h:body>
</html>

现在我明白了,实际上,如果用户尚未通过身份验证,则会引发AuthenticationException,ExceptionTranslationFilter将启动authenticationEntryPoint。所以只需像这样添加错误请求参数

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

<bean id="authenticationEntryPoint"
 class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
 <property name="loginFormUrl" value="/login.xhtml?error=true"/>
</bean>

and in your login.xhtml use the param like this

<h:outputText rendered="#{param['error']}" value="Please authenticate first!" />

在login.xhtml中使用如下参数

我的情况并非如此。我的情况是,用户还没有通过身份验证,这就是为什么他被发送到登录页面。当用户通过身份验证时,发送的不是拒绝访问页面,而是他没有被允许的角色。如果用户尚未通过身份验证,spring security将始终将用户发送到登录页面,在进行身份验证后,如果用户没有访问请求的资源的权限,则会显示拒绝访问页面。我假设您不希望web应用程序服务器发送默认的拒绝访问页面。为此,请参阅下文,我知道未经身份验证的用户将被重定向到登录页面。我想要的是在登录页面中向用户显示一个mesage。这就是我在问题中提出的问题,如果您有指向其他资源的标题链接,用户可以轻松导航到允许他访问的其他页面。Spring安全标记库用于呈现组件,具体取决于经过身份验证的用户是否具有特定的/s角色,也就是说,它处理授权。但我的案例与授权无关,而是身份验证。更重要的是,在我的应用程序中,我没有用户角色。我只想在未经身份验证的用户被重定向到登录页面时显示一条消息,因为他试图访问需要身份验证的页面。谢谢@Ravi。最后我发现这是正确的解决方案。它起作用了。我将?error=true附加到loginFormUrl,然后在登录页面的preRenderView侦听器中检查它,以便向用户显示会议。