ajax

2014-05-21 11 views
5

मैं कस्टम लॉगिन में वसंत सुरक्षा + JSF + primefaces
का उपयोग कर, मैं एक आदेश बटन
लेकिन आदेश बटन कोई काम ...
और जब पी में डाल: CommandButton ------ ----> AJAX = "false" ---------> काम है!
और जब च इस्तेमाल किया: JSF (कोर) --------> कोई काम नहीं
में ajax और मैं वसंत security.xml फ़ाइल में उपयोग करें:
ajax


test.xhtml - -> JSF (कोर) --------> कोई काम नहीं

<?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" xmlns:f="http://java.sun.com/jsf/core" 
      xml:lang="en" lang="en"> 
    <head> 
     <title>faces-request</title>   
    </head> 
    <body> 
    <h:form> 
     <center> 
      <h:outputText id="outtxt" value="#{authentiocationBean.ajaxTest}"/><br/> 
      <h:inputText id="intxt" value="#{authentiocationBean.ajaxTest}"/><br/> 
      <h:commandButton value="Submit"> 
       <f:ajax execute="intxt" render="outtxt"/> 
      </h:commandButton>   
     </center> 
    </h:form> 

    </body> 
    </html> 


Spring_security.xml:

<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.0.xsd 
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security-3.2.xsd"> 


    <http auto-config="true" use-expressions="true"> 
     <intercept-url pattern="/Admin/*" access="hasRole('ROLE_ADMIN')"/> 
     <intercept-url pattern="/user/*" access="hasAnyRole('ROLE_USER,ROLE_ADMIN')"/>   
     <form-login login-page="/login.xhtml" 
        authentication-failure-url="/Fail.xhtml?error"/> 
     <logout logout-url="/j_spring_security_logout" delete-cookies="JSESSIONID,SPRING_SECURITY_REMEMBER_ME_COOKIE" 
       invalidate-session="true" 
       logout-success-url="/login.xhtml"/> 
     <session-management session-authentication-error-url="/401.xhtml" session-fixation-protection="migrateSession"> 
      <concurrency-control max-sessions="1" expired-url="/login.xhtml"/> 
     </session-management> 

     <remember-me key="myAppKey"/> 
     <access-denied-handler error-page="/AccDe.xhtml"/> 
     <headers> 
      <xss-protection/> 
      <frame-options/> 
      <cache-control/> 
      <content-type-options/> 
     </headers> 
     <csrf/> 

    </http> 

    <authentication-manager alias="authenticationManager"> 
     <authentication-provider> 
      <jdbc-user-service data-source-ref="myDataSource" 
           users-by-username-query="select username, password, active from users where username=?" 
           authorities-by-username-query="select us.username, ur.authority from users us, user_roles ur 
        where us.user_id = ur.user_id and us.username =? " 
        /> 
      <password-encoder ref="passwordEncoder" hash="sha-256"/> 

     </authentication-provider> 
    </authentication-manager> 


    <bean id="passwordEncoder" 
      xmlns="http://www.springframework.org/schema/beans" 
      class="org.springframework.security.authentication.encoding.ShaPasswordEncoder"> 
     <!--org.springframework.security.crypto.password.PasswordEncoder  for salt!--> 
     <constructor-arg value="256"/> 
    </bean> 



</beans:beans> 


LoginPage:

<?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" 
     xmlns:p="http://primefaces.org/ui"> 
<h:head> 

    <title>faces-request</title>  
</h:head> 
<h:body> 
    <h:form prependId="false" id="formLogin"> 
     <center> 
      <p:panelGrid style="border-width: 0px;" columns="2"> 
       UserName: 
       <p:inputText required="true" id="j_username"/> 
       Password: 
       <p:password required="true" id="j_password"/> 
      </p:panelGrid>    
      <p:commandButton type="submit" id="login" action="#{authentiocationBean.doLogin()}" value="Login"/> 
      <p:outputLabel for="_spring_security_remember_me" value="Remember me: "/> 
      <p:selectBooleanCheckbox id="_spring_security_remember_me"/> 
      <br/> 
     </center> 
    </h:form> 
</h:body> 
</html> 

AuthentiocationBean वर्ग

import org.springframework.security.core.Authentication; 
import org.springframework.security.core.context.SecurityContextHolder; 

import java.io.IOException; 
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.RequestScoped; 
import javax.faces.context.ExternalContext; 
import javax.faces.context.FacesContext; 
import javax.servlet.RequestDispatcher; 
import javax.servlet.ServletException; 
import javax.servlet.ServletRequest; 
import javax.servlet.ServletResponse; 
import javax.servlet.http.HttpServletRequest; 


/** 
* @author Admin 
*/ 
@RequestScoped 
@ManagedBean 
public class AuthentiocationBean { 
    public String ajaxTest = "Test"; 
    boolean isLogged = false; 
    public String role = "ROLE_ADMIN"; 
    Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 
    String name = auth.getName(); 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 


    public String doLogin() throws IOException, ServletException { 
     isLogged = true; 
     FacesContext facesContext = FacesContext.getCurrentInstance(); 
     ExternalContext context = facesContext.getExternalContext(); 
     RequestDispatcher dispatcher = ((ServletRequest) context.getRequest()).getRequestDispatcher("/j_spring_security_check"); 
     dispatcher.forward((ServletRequest) context.getRequest(), (ServletResponse) context.getResponse()); 
     FacesContext.getCurrentInstance().responseComplete(); 
     // It's OK to return null here because Faces is just going to exit. 
     return null; 
    } 

    public void doLogout() { 
//  FacesContext.getCurrentInstance().getExternalContext().invalidateSession(); 
//  return "/logout.xhtml"; 
//  return null; 
    } 

    public boolean isLogged() { 
     return isLogged; 
    } 

    public void setLogged(boolean logged) { 
     isLogged = logged; 
    } 

    public String getRole() { 
     return role; 
    } 

    public void setRole(String role) { 
     this.role = role; 
    } 

    public String getAjaxTest() { 
     return ajaxTest; 
    } 

    public void setAjaxTest(String ajaxTest) { 
     this.ajaxTest = ajaxTest; 
    } 


} 

धन्यवाद

उत्तर

2

यह सिर्फ एक लॉग है पेज में, तो AJAX के साथ परेशान क्यों?

मैंने जेएसएफ के साथ वसंत 3.1.4 एलडीएपी समर्थन एकीकृत किया है। हालांकि मैंने शुरुआत में कस्टम प्रमाणीकरण बीन लिखा था, लेकिन अब मैं इसका उपयोग नहीं करता हूं। मैं विशेषज्ञ नहीं हूं, और मुझे यकीन है कि मैंने जो कुछ भी लागू किया है उससे अलग तरीका है।


(1) सरल प्रवेश पृष्ठ (अंश):

<h:inputText id="j_username"/> <h:inputText type="password" id="j_password" value=""/> <h:commandButton name="submit" type="submit" value="Log In" /> <input type="reset" value="Reset" />

(2,1) web.xml में, मैं एक संदर्भ के परम घोषित सुरक्षा विन्यास फाइल नाम के लिए (नीचे चरण 3 में उल्लिखित)।
मैं भी वसंत सुरक्षा फिल्टर श्रृंखला, जिसे आप यहां के बारे में पढ़ सकते हैं की घोषणा: http://docs.spring.io/spring-security/site/docs/3.1.4.RELEASE/reference/security-filter-chain.html#filter-chains-with-ns

(2,2) चेहरे-config में मैं घोषणा: <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>

(2,3) faces- में कॉन्फ़िगरेशन, मैं सत्र स्कोप के साथ एक कस्टम UserSession बीन घोषित करता हूं। (वेब में घोषित

@PostConstruct 
public void loadAuthorities() { 
    Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 
    . . . 
    . . . 
} 

(3) UserSession सेम लैंडिंग पृष्ठ (menu.xhtml) जो मेरी सुरक्षा कॉन्फ़िग फ़ाइल में घोषित किया जाता है के भीतर से में जाना जाता है:
UserSession सेम इस पद्धति है। चरण 2 में एक्सएमएल।1 इसके बाद के संस्करण):

<security:form-login default-target-url="/menu.xhtml" always-use-default-target="true"  authentication-failure-url="/denied.xhtml" />     
<security:logout invalidate-session="true" delete-cookies="JSESSIONID" logout-url="/j_spring_security_logout" /> 

(4) उपयोगकर्ता तो menu.xhtml पर पुनः निर्देशित प्रमाणित है Menu.xhtml UserSession सेम लोड करने के लिए कारण बनता है।

उपयोगकर्ता सत्र बीन सुरक्षा कॉन्टेक्स्ट से प्राधिकरणों की एक सूची खींचता है।

public boolean isRole(String role) { 
    return authorities.contains((String) role); 
} 

public boolean roleContains(String s); 
public boolean roleEndsWith(String s); 
. . . 
. . . 
:

UserSession सेम एक उपयोगकर्ता पृष्ठों और संसाधनों देखने पर अधिकार है कि क्या जांच करने के लिए सरल रैपर प्रदान करता है