登录后的Spring安全重定向

问题描述 投票:6回答:2

成功登录后,它不会重定向到“index.php”。 Id重定向相同的页面,即“login.php”。我的spring-security.xml页面有问题吗?

顺便说一下,当我运行应用程序时,它会将我重定向到“login.php”,这很好。但它没有显示primefaces组件,但显示html组件。在我成功登录后,它重定向同一页面,但这次它显示的是Primefaces组件而不是html组件。

<beans:beans xmlns="http://www.springframework.org/schema/security"
         xmlns:beans="http://www.springframework.org/schema/beans" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">

<http auto-config="true" use-expressions="true">
    <intercept-url pattern="/pages/login.xhtml*" access="permitAll"/>
    <intercept-url pattern="/**" access="hasRole('admin')" />
    <form-login login-page='/pages/login.xhtml' default-target-url="/pages/index.xhtml"                    
                authentication-failure-url="/pages/login.xhtml"/>
    <logout logout-success-url="/pages/logout.xhtml" />

</http>
<!--Authentication Manager Details -->    
<authentication-manager alias="authenticationManager">
    <authentication-provider user-service-ref="customUserDetailsService">
<!--            <password-encoder hash="md5"/>-->
    </authentication-provider>
</authentication-manager>

我的web.xml

<welcome-file-list>
    <welcome-file>pages/index.xhtml</welcome-file>
</welcome-file-list>

我的登录页面

<p:outputPanel id="loginOutputPanelId" style="border: navy">
                        <p:panelGrid id="loginInformationPanel" columns="2">
                            <h:outputText value="Username: "/>
                            <p:inputText value="#{loginController.userName}"/>
                            <h:outputText value="Password: "/>
                            <p:inputText value="#{loginController.password}"/>
                        </p:panelGrid>
                        <p:commandButton value="Login" actionListener="#{loginController.login()}"/>
                    </p:outputPanel>

我的loginController.login()方法返回“index”字符串和我的faces.config;

<navigation-rule>
        <from-view-id>/pages/login.xhtml</from-view-id>
        <navigation-case>
            <from-outcome>index</from-outcome>
            <to-view-id>/pages/index.xhtml</to-view-id>
            <redirect />
        </navigation-case>
    </navigation-rule>

编辑:没有组件它运行没有任何问题。当我添加form-login时,它说“http://localhost:8080/myApplication/pages/login.xhtml的网页导致了太多的重定向”。

<http auto-config='true' use-expressions="true">
<intercept-url pattern="/**" access="hasRole('admin')" />
<logout logout-success-url="/pages/logout.xhtml" />
<form-login login-page="/pages/login.xhtml"
                login-processing-url="/j_spring_security_check"                                                       
                default-target-url="/pages/index.xhtml"                                                        
                authentication-failure-url="/pages/login.xhtml"/>
</http>

我的登录页面

<p:outputPanel id="loginOutputPanelId" style="border: navy">
                        <p:panelGrid id="loginInformationPanel" columns="2">
                            <h:outputText value="Kullanıcı Adı: "/>
                            <p:inputText id="j_username" required="true" value="#{loginController.userName}"/>
                            <h:outputText value="Şifre: "/>
                            <p:inputText id="j_password" required="true" value="#{loginController.password}"/>
                        </p:panelGrid>
                        <p:commandButton id="login" type="submit" ajax="false" value="Login" actionListener="#{loginController.login()}"/>
                    </p:outputPanel>

我新的loginController.login()方法;

ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();

        RequestDispatcher dispatcher = ((ServletRequest) context.getRequest())
         .getRequestDispatcher("/j_spring_security_check");

        dispatcher.forward((ServletRequest) context.getRequest(),
         (ServletResponse) context.getResponse());

        FacesContext.getCurrentInstance().responseComplete();
spring spring-security
2个回答
16
投票

要强制spring-security转到/pages/index.xhtml,你可以使用属性always-use-default-target

<form-login login-page='/pages/login.xhtml' 
            default-target-url="/pages/index.xhtml"
            always-use-default-target="true"                    
            authentication-failure-url="/pages/login.xhtml"/>

否则,当用户调用安全资源时,应通过spring security自动显示登录页面,登录完成后,继续访问最初请求的安全资源。

在您的情况下,一些混乱似乎来自于您希望spring安全性来处理登录,并且您尝试使用jsf actionListener和导航规则自己处理它。

把“<form-login [...]”放在配置中基本上告诉spring激活一个过滤器(UsernamePasswordAuthenticationFilter),它将听取对/j_spring_security_check的请求。如果您希望spring处理登录,默认情况下,您的表单登录必须请求此URL,并传递两个参数:j_usernamej_password

这样,spring qazxsw poi将启动并尝试使用您在AuthenticationProvider中配置的UserDetailsS​​ervice来验证提供的凭据。

我认为你必须删除你的jsf控制器登录并使用spring-security来处理身份验证。

希望这可以帮助。

PS:确保您的web.xml在所有其他servlet过滤器之前定义DelegatingFilterProxy:

UsernamePasswordAuthenticationFilter

0
投票

请检查<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> </filter-mapping> 中的Faces Servlet URL模式。

如果它包含web.xml像:

.jsf

那么你必须在spring-security.xml中更新你的代码,如:

<servlet-mapping>
   <servlet-name>Faces Servlet</servlet-name>
   <url-pattern>*.jsf</url-pattern>
</servlet-mapping>
© www.soinside.com 2019 - 2024. All rights reserved.