title: Spring security How redirect after succes login page with user role?
body: <p>Hi i need to know how can i redirect my application after succes login page by verifying the user role just as:
  if ROLE_USER redirect to accountUser.xhtml
  else if ROLE_ADMIN redirect to accountAdmin.xtml</p>
  
  <p>using spring security</p>
  
  <p>my web.xml page is</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
      version="2.5"&gt;
      &lt;display-name&gt;iTubibe&lt;/display-name&gt;
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
  
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/applicationContext.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;context-param&gt;
          &lt;param-name&gt;javax.faces.DEFAULT_SUFFIX&lt;/param-name&gt;
          &lt;param-value&gt;.xhtml&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;context-param&gt;
          &lt;param-name&gt;facelets.DEVELOPMENT&lt;/param-name&gt;
          &lt;param-value&gt;true&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;context-param&gt;
          &lt;param-name&gt;javax.faces.FACELETS_REFRESH_PERIOD&lt;/param-name&gt;
          &lt;param-value&gt;1&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;servlet&gt;
          &lt;servlet-name&gt;Resources Servlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.js.resource.ResourceServlet&lt;/servlet-class&gt;
          &lt;load-on-startup&gt;0&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;Resources Servlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/resources/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;Spring MVC Dispatcher Servlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
              &lt;param-value&gt;&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;Spring MVC Dispatcher Servlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/app/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&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;*.jsf&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
      &lt;filter&gt;
          &lt;filter-name&gt;charEncodingFilter&lt;/filter-name&gt;
          &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;encoding&lt;/param-name&gt;
              &lt;param-value&gt;UTF-8&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;forceEncoding&lt;/param-name&gt;
              &lt;param-value&gt;true&lt;/param-value&gt;
          &lt;/init-param&gt;
      &lt;/filter&gt;
  
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;charEncodingFilter&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;primefaces.THEME&lt;/param-name&gt;
          &lt;param-value&gt;excite-bike&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;primefaces.FONT_AWESOME&lt;/param-name&gt;
          &lt;param-value&gt;true&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;!-- Spring security filters --&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;/filter-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>and my main-flow.xml is</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;flow xmlns="http://www.springframework.org/schema/webflow"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/webflow
          http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd"&gt;
  
      &lt;var name="user" class="com.ismart.itubibe.entities.UserEntity"/&gt;
  
      &lt;view-state id="welcome" view="welcome.xhtml" model="user"&gt;
          &lt;transition on="newUser" to="signUp"/&gt;
          &lt;transition on="signIn" to="finish"&gt;
              &lt;evaluate expression="userAuthenticationProviderService.processUserAuthentication(user)"/&gt;
          &lt;/transition&gt;
      &lt;/view-state&gt;
  
      &lt;view-state id="signUp" view="signUp.xhtml" model="user"&gt;
          &lt;transition on="backToSignIn" to="welcome"/&gt;
          &lt;transition on="signUp" to="authentication"&gt;
              &lt;evaluate expression="userServices.createUser(user)"/&gt;
          &lt;/transition&gt;
      &lt;/view-state&gt;
  
      &lt;action-state id="authentication"&gt;
          &lt;evaluate expression="userAuthenticationProviderService.processUserAuthentication(user)"/&gt;
          &lt;transition on="yes" to="finish" /&gt;
          &lt;transition on="no" to="welcome" /&gt;
      &lt;/action-state&gt;
  
      &lt;end-state id="finish" view="externalRedirect:account" /&gt;
  
  &lt;/flow&gt;
  </code></pre>
  
  <p>my spring security-config.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-4.0.xsd
              http://www.springframework.org/schema/security
              http://www.springframework.org/schema/security/spring-security.xsd"&gt;
  
      &lt;security:http auto-config="true"&gt;
          &lt;security:form-login login-page="/app/main" default-target-url="/app/account" /&gt;
          &lt;security:logout logout-url="/app/logout" logout-success-url="/app/main" /&gt;
      &lt;/security:http&gt;
  
      &lt;security:authentication-manager&gt;
          &lt;security:authentication-provider user-service-ref="userServices"&gt;
              &lt;security:password-encoder hash="md5" /&gt;
          &lt;/security:authentication-provider&gt;
      &lt;/security:authentication-manager&gt;
  
      &lt;bean id="daoAuthenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider"&gt;
          &lt;property name="userDetailsService" ref="userServices" /&gt;
          &lt;property name="hideUserNotFoundExceptions" value="false" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="authenticationManager" class="org.springframework.security.authentication.ProviderManager"&gt;
          &lt;constructor-arg&gt;
              &lt;ref bean="daoAuthenticationProvider" /&gt;
          &lt;/constructor-arg&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>and this is my load user detail methode:</p>
  
  <pre><code> public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
  
                  UserEntity user = userDao.loadUserByUserName(userName);
  
                  if(user == null){
                      throw new UsernameNotFoundException(String.format("Etulisateur introuvable '%s'", userName)); 
                  }
  
                  Collection&lt;GrantedAuthority&gt; authorities = new ArrayList&lt;GrantedAuthority&gt;();
  
                  authorities.add(new SimpleGrantedAuthority(user.getUserType()));
  
                  User userDetails = new User(user.getUserName(), user.getPassWord(), authorities);
  
                  return userDetails;
              }
  </code></pre>
  
  <p>and the authentication methode</p>
  
  <pre><code>public boolean processUserAuthentication(UserEntity user) {
          try {
              Authentication request = new UsernamePasswordAuthenticationToken(user.getUserName(), user.getPassWord());
              Authentication result = authenticationManager.authenticate(request);
              SecurityContextHolder.getContext().setAuthentication(result);
              return true;
          } catch (AuthenticationException e) {
  
              FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), "Sorry!!"));
              return false;
          }
  
  
  
      }
  </code></pre>
  
  <p>and this is my account-flow:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;flow xmlns="http://www.springframework.org/schema/webflow"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/webflow
          http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd"&gt;
  
      &lt;secured attributes="ROLE_USER, ROLE_ADMIN" match="any"/&gt;
  
      &lt;view-state id="admin" view="accountAdmin.xhtml"&gt;
      &lt;/view-state&gt;
  
      &lt;view-state id="user" view="accountUser.xhtml"&gt;
      &lt;/view-state&gt;
  
  
  
  
  &lt;/flow&gt;
  </code></pre>