Announcement Announcement Module
Collapse
No announcement yet.
spring security and jsf Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • spring security and jsf

    As we are getting below: exception :

    Unable to redirect the page after success/failure after login page?


    Please help us to find the root cause.

    Please find the below exception and the codes also:


    20:43:53:507 IST] 0000002e SystemErr R 62 [WebContainer : 0] INFO org.apache.bval.jsr303.ConfigurationImpl - ignoreXmlConfiguration == true
    [6/26/13 20:43:53:726 IST] 0000002e SystemErr R java.io.FileNotFoundException: SRVE0190E: File not found: /j_spring_security_check
    [6/26/13 20:43:53:726 IST] 0000002e SystemErr R at com.ibm.ws.webcontainer.extension.DefaultExtension Processor._processEDR(DefaultExtensionProcessor.ja va:874)

    Code:
    ApplicationContext-security.xml
    ------------------------------
    
    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:security="http://www.springframework.org/schema/security"
    	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.1.xsd
              http://www.springframework.org/schema/security
              http://www.springframework.org/schema/security/spring-security-3.1.xsd">
    
    <!-- 	<security:global-method-security
    		secured-annotations="enabled">
    	</security:global-method-security> -->
    
    	<security:http auto-config="true" access-denied-page="/loginfailed.xhtml">
    		<security:intercept-url pattern="/loginpage*" access="IS_AUTHENTICATED_ANONYMOUSLY" />
    		
    <!-- 		 <security:intercept-url pattern="/j_spring_security_check" access="IS_AUTHENTICATED_ANONYMOUSLY" />
    login-page='/loginpage.xhtml'
     -->		<security:intercept-url pattern="/**" access="ROLE_USER,ROLE_ADMIN"></security:intercept-url>
    		<security:form-login 
    		default-target-url="/pages/User/edit.xhtml"
    		authentication-failure-url="/loginpage.xhtml" />
    		 <security:http-basic/> 
    	</security:http >
    	<security:authentication-manager alias="authenticationManager">
    		<security:authentication-provider>
    			<security:user-service>
    				<security:user name="jp" password="jp" authorities="ROLE_USER,ROLE_ADMIN" />
    				<security:user name="lak" password="lak" authorities="ROLE_ADMIN" />
    				<security:user name="dd" password="ddd" authorities="ROLE_ADMIN" />
    			</security:user-service>
    
    		</security:authentication-provider>
    	</security:authentication-manager>
    	     
    </beans>
    Code:
    faces-config.xml
    -----------------
    <?xml version="1.0" encoding="UTF-8"?>
    
    <faces-config
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
        version="2.0">
        
        <application>
          <!-- <variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver> -->
          <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
       </application>
       <managed-bean>
          <managed-bean-name>provider</managed-bean-name>
          <managed-bean-class>com.wellpoint.enterprise.modeler.business.ProviderBean</managed-bean-class>
          <managed-bean-scope>session</managed-bean-scope>
          <managed-property>
             <property-name>provjdbc</property-name>
             <value>#{providerJDBCTemplate}</value>
          </managed-property>
       </managed-bean> 
      
    </faces-config>
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    	version="3.0">
    	<display-name>EnterpriseModeler</display-name>
    	
    	<listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
    	<listener>
    		<listener-class>
    			org.springframework.web.context.request.RequestContextListener</listener-class>
    	</listener>
    	<listener>
    		<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
    	</listener>
    	
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>/WEB-INF/application-context.xml, /WEB-INF/applicationContext-security.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>
        </filter-mapping> 
    	<servlet>
    		<servlet-name>Faces Servlet</servlet-name>
    		<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    		<load-on-startup>-1</load-on-startup>
    		<enabled>true</enabled>
    		<async-supported>false</async-supported>
    	</servlet>
    	<servlet-mapping>
    		<servlet-name>Faces Servlet</servlet-name>
    		<url-pattern>*.faces</url-pattern>
    	</servlet-mapping>
    	<servlet-mapping>
    		<servlet-name>Faces Servlet</servlet-name>
    		<url-pattern>/faces/*</url-pattern>
    	</servlet-mapping>
    	<welcome-file-list>
    		<welcome-file>sample.xhtml</welcome-file>
    	</welcome-file-list>
    </web-app>

    Code:
    loginpage.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"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:ui="http://java.sun.com/jsf/facelets"
          xmlns:p="http://primefaces.org/ui">
        <h:head>
        <title>PROVIDER FORM </title>
        	 
        </h:head>
      
        
       
        <h:body>
           <link rel="stylesheet" type="text/css"  href="../../theme/primefaces_new.css" />
            <h1>Login</h1>
        	<h:form prependId="false">
        	<h3>Enter the Details</h3>
        	 
        	
        		<table>
        		
        		<tr>
        			<td><h:outputText for="j_username" value="User Name :"/></td>
        			<td><p:inputText id="j_username" name="j_username" value="#{user.userName}" required="true" label="User Name"/></td>
        		</tr>
        		<tr>
        			<td><h:outputText for="j_password" value="Password :"/></td>
        			<td><p:inputText  id="j_password" name="j_password" value="#{user.password}" required="true" label="Password"/></td>
        		</tr>
       	
        		</table>
    
           <h:commandButton type="submit" value="ADD"  action="#{user.authenticate}"  />
    
    			<h:commandButton value="reset" type="reset" />
    			
             	</h:form>
             	</h:body>
        		</html>
    Code:
    User.java
    
    ----------------
    
    import java.io.IOException;
    
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;
    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;
    
    @ManagedBean(name="user")
    @SessionScoped
    public class User {
    	private String userName;
    	private String password;
    	
    	public String getUserName() {
    		return userName;
    	}
    	public void setUserName(String userName) {
    		this.userName = userName;
    	}
    	public String getPassword() {
    		return password;
    	}
    	public void setPassword(String password) {
    		this.password = password;
    	}
    	public String authenticate(){
    		
    		System.out.println("authenticate: ROLE_ADMIN");
    		
    		System.out.println("<<<doLogin()");
    
          /* ExternalContext cont =FacesContext.getCurrentInstance().getExternalContext();
    
            //RequestDispatcher dispatch = ((ServletRequest)cont.getRequest()).getRequestDispatcher("/pages/User/sample.xhtml");
            RequestDispatcher dispatch = ((ServletRequest)cont.getRequest()).getRequestDispatcher("/j_spring_security_check");
    
            try {
    			dispatch.forward((ServletRequest) cont.getRequest(), (ServletResponse) cont.getResponse());
    			
    		} catch (ServletException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}   
    
        //FacesContext.getCurrentInstance().responseComplete();   
    
    //		return "sample";
    		FacesContext.getCurrentInstance().responseComplete();
    	    try {
    			redirect("/pages/User/sample.xhtml");
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	    return "edit";*/
    		
    		//do any job with the associated values that you've got from the user, like persisting attempted login, etc.
    	    FacesContext facesContext = FacesContext.getCurrentInstance();
    	    ExternalContext extenalContext = facesContext.getExternalContext();
    	    RequestDispatcher dispatcher = ((ServletRequest)extenalContext.getRequest()).getRequestDispatcher("/j_spring_security_check");
    	    try {
    			dispatcher.forward((ServletRequest)extenalContext.getRequest(), (ServletResponse)extenalContext.getResponse());
    		    facesContext.responseComplete();
    
    		} catch (ServletException e) {
    			System.out.println("Exception :"+e.getMessage());
    			System.out.println("Exception :"+e.getStackTrace());
    			e.printStackTrace();
    		} catch (IOException e) {
    			System.out.println("Exception :"+e.getMessage());
    			e.printStackTrace();
    			System.out.println("Exception :"+e.getStackTrace());
    		}
    	    return null;
    
    
    	}
    	
    	public static void redirect(String url) throws IOException {
    	    ExternalContext ctx = FacesContext.getCurrentInstance()
    	        .getExternalContext();
    	    if (url.startsWith("http://") || url.startsWith("https://")
    	            || url.startsWith("/")) {
    	        ctx.redirect(url);
    	    } else {
    	        ctx.redirect(ctx.getRequestContextPath() + "/" + url);
    	    }
    	}
    
    }
Working...
X