我有一个Spring安全和ajax请求的primefaces命令按钮的问题。在我的注册页面中单击命令按钮并失败响应后,下一次单击,错误302发生并返回登录页面!我使用的是primefaces 8.0和spring security 5。
这是我的安全配置:
<http auto-config="true" use-expressions="true">
<access-denied-handler error-page="/accessDenied.xhtml"/>
<form-login login-page="/login.xhtml"
login-processing-url="/login"
password-parameter="password"
username-parameter="username"
authentication-failure-url="/login.xhtml?error=true"/>
<logout invalidate-session="true"
logout-success-url="/"
logout-url="/logout"/>
<intercept-url pattern="/login.xhtml" access="permitAll"/>
<!-- RESOURCES -->
<intercept-url pattern="/skins/**" access="permitAll"/>
<intercept-url pattern="/css/**" access="permitAll"/>
<intercept-url pattern="/adf/**" access="permitAll"/>
<intercept-url pattern="/images/**" access="permitAll"/>
<intercept-url pattern="/js/**" access="permitAll"/>
<intercept-url pattern="/javax.faces.resource/**" access="permitAll"/>
<intercept-url pattern="/signup.xhtml" access="permitAll"/>
<intercept-url pattern="/**" access="isAuthenticated()"/>
<csrf/>
</http>
my sign .xhtml page:
& lt; h:身体类="blue-grey-theme"在
<h:form id="form" prependId="false">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
<p:growl id="smsGrowl" life="7000" globalOnly="true"/>
<p:tabView id="applicantTab" widgetVar="tabView" effect="fade"
activeIndex="#{applicantSignUpBean.applicantSignUpModel.tabViewActiveIndex}"
style="margin-bottom:10px; text-align: right; width: 98%; min-width: 400px">
<p:tab id="PersonalTab" title="#{bundle.personal_applicant}">
<h:panelGrid id="personalGrid" columns="4" cellpadding="5" style="width: 99%;"
styleClass="ui-grid">
<app:outputLabel rendered="true" value="#{bundle.firstName}" required="true" for="fName"/>
<app:input id="fName" required="true" requiredMessage="#{bundle.firstname_required}"
value="#{mySignUpBean.mySignUpModel.firstName}"
style="width:250px"/>
<app:outputLabel rendered="true" value="#{bundle.lastName}" required="true" for="lName"/>
<app:input id="lName"
value="#{mySignUpBean.mySignUpModel.lastName}"
required="true" requiredMessage="#{bundle.lastname_required}"
style="width:250px"/>
<!-- ...... -->
</h:panelGrid>
<br/>
<p:outputPanel style="text-align: center; ">
<p:commandButton id="btnSavePersonal"
actionListener="#{applicantSignUpBean.signUpPersonal}"
icon="fa fa-save" value="#{bundle.registration}"
process=":form:applicantTab:PersonalGrid @this"
update=":form:applicantTab:PersonalGrid"
style="margin: 20px; width: 150px;" styleClass="button_save">
</p:commandButton>
<p:tooltip for="btnSavePersonal" value="#{bundle.save}"/>
</p:outputPanel>
</p:tab>
</p:tabView>
</h:form>
我试了好几次都没有成功。
谢谢你的帮助
Spring Security通过要求随机生成的令牌作为HTTP参数来防止CSRF攻击,但这会破坏JSF commandButtons。但是,由于JSF 2.2已经包含了针对CSRF攻击的显式保护,您可以安全地禁用Spring Security CSRF保护。
从Spring Security 4.0开始,CSRF保护默认启用XML配置。如果要禁用CSRF保护,请使用相应的XML配置可以在下面看到。
<http> <!-- ... --> <csrf disabled="true"/> </http>
更多信息请参见:Spring Security Configure CSRF Protection
编辑
你不能注册,登录或注销使用AJAX请求,所以你将不得不使用ajax=false
在你的btnSavePersonal
命令按钮,但这将打破验证器,消息和这个方法applicantSignUpBean.signUpPersonal
将不会被调用,你将不得不移动你的业务逻辑到Spring Beans
。
检查这个:PrimeFaces Spring Security Example