Hi, I am making a project based on the spring web flow sample (about the books in the hotel) but I have a problem in the security part. The project has JSF2.0, Primefaces, Spring security and Spring webflow.
I use Spring security with a ldap provider and it's working, but I would like to do another step, I want to check the login and password with my database too.
this is my login.xhtml:
and this is my security-config.xml:Code:<form name="f" action="${request.contextPath}/j_spring_security_check" method="post"> <fieldset> <legend>Login Information</legend> <p> User: <br /> <c:if test="${not empty param.login_error}"> <c:set var="username" value="${sessionScope.SPRING_SECURITY_LAST_USERNAME}"/> </c:if> <input type="text" name="j_username" value="#{loginBean.username}"/> </p> <p> Password: <br /> <input type="password" name="j_password" /> </p> <p> <input type="checkbox" name="_spring_security_remember_me"/> Don't ask for my password for two weeks: </p> <p> <input name="submit" type="submit" value="Login" /> </p> </fieldset> </form>
This code is working with the ldap provider, webflow... etc, but now I want to add the customUserContextMapper.Code:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:security="http://www.springframework.org/schema/security" 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.0.xsd"> <security:http access-denied-page="/spring/main/accessDenied" auto-config="true"> <security:intercept-url access="ROLE_ADMIN" pattern="/flows/admin/**"/> <security:intercept-url access="ROLE_USER" pattern="/flows/admin/**"/> <security:intercept-url access="IS_AUTHENTICATED_ANONYMOUSLY" pattern="/*"/> <security:form-login authentication-failure-url="/spring/login?login_error=1" default-target-url="/spring/admin" login-processing-url="/j_spring_security_check" login-page="/spring/login"/> <security:logout logout-success-url="/spring/logoutSuccess" logout-url="/spring/logout"/> </security:http> <security:authentication-manager> <security:ldap-authentication-provider user-search-filter="(uid={0})" user-search-base="ou=users,ou=Internal,o=company"/> </security:authentication-manager> <security:ldap-server manager-password="PWD" manager-dn="uid=Uname,ou=Users,ou=Internal,o=ericsson" url="ldap://mycompany.com"/> </beans>
I have these functions :
mapUserFromContext (the class extends LdapUserDetailsMapper):
The loginBean is the following:Code:@Override public UserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection<GrantedAuthority> authority) { Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>(); UserDetails originalUser = super.mapUserFromContext(ctx, username, authority); System.out.println("loginService " + loginService); boatUser = loginService.getUser(username); Set<Role> hs = new HashSet<Role>(); LoginBean loginBean = (LoginBean) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("loginBean"); loginBean.setBoatUser(boatUser); if (boatUser != null) { hs = boatUser.getRoles(); } for (Role role : hs) { authorities.add(new GrantedAuthorityImpl(role.getRole_name())); } User newUser = new User(originalUser.getUsername(), "na", originalUser.isEnabled(), originalUser.isAccountNonExpired(), originalUser.isCredentialsNonExpired(), originalUser.isAccountNonLocked(), authorities); return newUser; }
and I changed the login.xhtml withCode:@Component @Scope("session") @ManagedBean(name = "loginBean") public class LoginBean { private String username = ""; private String password = ""; private boolean rememberMe = false; private boolean loggedIn = false; private User boatUser; public String doLogin() throws IOException, ServletException { System.out.println("DoLogin Method"); 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(); // It's OK to return null here because Faces is just going to exit. return "succcess"; }
and I changed security-config.xml:Code:<h:form id="loginForm" prependId="false"> ... <h:commandButton type="submit" id="login" action="#{loginBean.doLogin}" value="Login" /> ... </h:form>
But it doesn't work, when I push the submit button, I have the following error:Code:... <bean class="com.ericsson.boat.login.service.CustomUserDetailsMapper" id="customUserContextMapper"/> <security:authentication-manager> <security:ldap-authentication-provider user-search-base="ou=users,ou=Internal,o=company" user-search-filter="(uid={0})" user-context-mapper-ref="customUserContextMapper" /> </security:authentication-manager> ...
And the web page goes to:Code:com.sun.faces.context.FacesFileNotFoundException at com.sun.faces.facelets.impl.DefaultFaceletFactory.resolveURL(DefaultFaceletFactory.java:232) at com.sun.faces.facelets.impl.DefaultFaceletFactory.resolveURL(DefaultFaceletFactory.java:273) at com.sun.faces.facelets.impl.DefaultFaceletFactory.getFacelet(DefaultFaceletFactory.java:201) at com.sun.faces.application.view.FaceletViewHandlingStrategy.buildView(FaceletViewHandlingStrategy.java:764) at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:100) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139) ... at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) at java.lang.Thread.run(Thread.java:722)
http://localhost:8080/BOAT/spring/WEB-INF/login.xhtml
The system never executes doLogin. In another version I got to execute the mapUserFromContext, but FacesContext was null because the system doesn't execute doLogin...
Do I need something else? Is possible to do what I am trying??


Reply With Quote

