Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring Security cannot redirect the page after authentication
    primarykey
    data
    text
    <p>I have a JSF project with spring security installed, I use the forward method to do the login. But I cannot redirect the page to secured.xhtml in AuthenticationBean.java. Seems like the "forward()" method causes this. But the authentication is successfully done without exception, I just can't redirect the page. Without the forward method, I'm able to successfully redirect the page. So, help?</p> <p>web.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;web-app version="3.0" 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"&gt; &lt;context-param&gt; &lt;param-name&gt;javax.faces.PROJECT_STAGE&lt;/param-name&gt; &lt;param-value&gt;Development&lt;/param-value&gt; &lt;/context-param&gt; &lt;servlet&gt; &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt; &lt;servlet-class&gt;javax.faces.webapp.FacesServlet&lt;/servlet-class&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt; &lt;url-pattern&gt;*.xhtml&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;session-config&gt; &lt;session-timeout&gt; 30 &lt;/session-timeout&gt; &lt;/session-config&gt; &lt;welcome-file-list&gt; &lt;welcome-file&gt;index.xhtml&lt;/welcome-file&gt; &lt;/welcome-file-list&gt; &lt;filter&gt; &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt; &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;dispatcher&gt;REQUEST&lt;/dispatcher&gt; &lt;dispatcher&gt;FORWARD&lt;/dispatcher&gt; &lt;/filter-mapping&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt; /WEB-INF/spring-security.xml &lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;/web-app&gt; </code></pre> <p>spring-security.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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.1.xsd"&gt; &lt;security:http auto-config="true" use-expressions="true" access-denied-page="/denied.xhtml"&gt; &lt;security:intercept-url pattern="/login.xhtml" access="permitAll"/&gt; &lt;security:intercept-url pattern="/secured.xhtml" access="hasRole('ROLE_ADMIN')"/&gt; &lt;security:form-login login-page="/login.xhtml" authentication-failure-url="/login.xhtml?error=true" default-target-url="/"/&gt; &lt;security:logout invalidate-session="true" logout-success-url="/index.xhtml" logout-url="/logout.xhtml"/&gt; &lt;/security:http&gt; &lt;security:authentication-manager&gt; &lt;security:authentication-provider user-service-ref="customUserDetailsService"&gt; &lt;security:password-encoder ref="passwordEncoder"/&gt; &lt;/security:authentication-provider&gt; &lt;/security:authentication-manager&gt; &lt;bean class="org.springframework.security.authentication.encoding.Md5PasswordEncoder" id="passwordEncoder"/&gt; &lt;bean id="customUserDetailsService" class="com.triune.services.CustomUserDetailsService"/&gt; &lt;/beans&gt; </code></pre> <p>CustomUserDetailService.java (This is never called)</p> <pre><code>package com.triune.services; import java.util.ArrayList; import java.util.Collection; import java.util.List; import com.triune.dao.UserDAO; import com.triune.entities.LoginCredential; import org.springframework.dao.DataAccessException; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.transaction.annotation.Transactional; @Transactional(readOnly = true) public class CustomUserDetailsService implements UserDetailsService { private UserDAO userDAO = new UserDAO(); @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException { UserDetails user = null; try { LoginCredential dbUser = userDAO.searchDatabase(username); user = new User( dbUser.getUsername(), dbUser.getPassword().toLowerCase(), true, true, true, true, getAuthorities(dbUser.getAccess()) ); } catch (Exception e) { throw new UsernameNotFoundException("Error in retrieving user"); } return user; } public Collection&lt;GrantedAuthority&gt; getAuthorities(Integer access) { List&lt;GrantedAuthority&gt; authList = new ArrayList&lt;GrantedAuthority&gt;(2); authList.add(new SimpleGrantedAuthority("ROLE_USER")); if ( access.compareTo(1) == 0) { authList.add(new SimpleGrantedAuthority("ROLE_ADMIN")); } return authList; } } </code></pre> <p>AuthenticationBean.java (this is called by the login page)</p> <pre><code>package com.triune.beans; import java.io.IOException; import java.io.Serializable; import javax.enterprise.context.SessionScoped; import javax.faces.context.ExternalContext; import javax.faces.context.FacesContext; import javax.inject.Named; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; @Named("authenticationBean") @SessionScoped public class AuthenticationBean implements Serializable { public String doLogin() throws IOException, ServletException { 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(); return "secured.xhtml"; } public String doLogout() { FacesContext.getCurrentInstance().getExternalContext().invalidateSession(); return "index.xhtml"; } } </code></pre> <p>login.xhtml (simple login page)</p> <pre><code>&lt;?xml version='1.0' encoding='UTF-8' ?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:p="http://primefaces.org/ui"&gt; &lt;h:head&gt; &lt;title&gt;Login Page&lt;/title&gt; &lt;/h:head&gt; &lt;h:body&gt; &lt;h1&gt;Login Please&lt;/h1&gt; &lt;p&gt;This is a really simple login page.&lt;/p&gt; &lt;h:form id="loginForm" prependId="false"&gt; &lt;h:panelGroup&gt; Username : &lt;p:inputText id="j_username" required="true"/&gt; &lt;/h:panelGroup&gt; &lt;br/&gt; &lt;h:panelGroup&gt; Password : &lt;p:inputText id="j_password" required="true"/&gt; &lt;/h:panelGroup&gt; &lt;br/&gt;&lt;br/&gt; &lt;h:panelGroup&gt; &lt;p:commandButton type="submit" id="login" action="#{authenticationBean.doLogin()}" value="login"/&gt; &lt;/h:panelGroup&gt; &lt;/h:form&gt; &lt;/h:body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload